Android ListView - scrolls back to top on update - android-listview

I have a listview that gets additional views added on request, that's maintained by a BaseAdapter. How can I maintain scroll position after the update?
I know this has been asked a few times, but each time, the same solution has been put forward, which I have tried, which is to call adapter.notifyDataSetChanged(); after updating the ArrayList that contains the list contents.
How can I ensure that scroll position is maintained?

Implement an OnScrollListener in your activity class and then use the following code:
int currentFirstVisibleItem, currentVisibleItemCount, currentTotalItemCount;
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
this.currentFirstVisibleItem = firstVisibleItem;
this.currentVisibleItemCount = visibleItemCount;
this.currentTotalItemCount = totalItemCount;
}
public void onScrollStateChanged(AbsListView view, int scrollState) {
this.currentScrollState = scrollState;
this.isScrollCompleted();
}
private void isScrollCompleted() {
if (currentFirstVisibleItem + currentVisibleItemCount >= currentTotalItemCount) {
if (this.currentVisibleItemCount > 0
&& this.currentScrollState == SCROLL_STATE_IDLE) {
//Do your work
}
}
}
If you are using AsyncTask for updating your data, then you can include the following in your PostExecute() in order to maintain the Scroll position:
list.setAdapter(adapter);
list.setSelectionFromTop(currentFirstVisibleItem, 0);
I hope this helps.

The approach I take is to call ListView.setSelection(position) to scroll to the desired position after the update.
Depending on where you're calling it from, you might need to call requestFocusFromTouch before calling setSelection in order to ensure the item gets positioned appropriately.

This code will help you out....
// get position of listview
int index = listView.getFirstVisiblePosition();
View v = listView.getChildAt(0);
int top = (v == null) ? 0 : v.getTop();
// notify dataset changed or re-assign adapter here
//re-assign the position of listview after setting the adapter again
listView.setSelectionFromTop(index, top);
Cheers :)

You can try this:
//Get the top position from the first visible element
int fVisible = list.getFirstVisiblePosition();
View vFirst = list.getChildAt(0);
int pos = 0;
if (vFirst != null) pos = vFirst.getTop();
//Restore the position
list.setSelectionFromTop(fVisible, pos);

You can do as below. If you want to load more data with scroll to bottom it will have some change.
int currentFirstVisibleItem, currentScrollState;
int countChange = 0;
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
Log.d("Scroll", "Scrolling");
this.currentFirstVisibleItem = firstVisibleItem;
countChange = 0;//during touching you can decide not to refresh by scroll
}
public void onScrollStateChanged(AbsListView view, int scrollState) {
Log.d("Scroll", "Changed");
this.currentScrollState = scrollState;
if (scrollState == SCROLL_STATE_TOUCH_SCROLL) {
countChange = 0;
}
if (++countChange > 1) {
//scroll to top and release touch change=2. Simple scroll and release touch change=1
this.isScrollCompleted();
countChange = 0;
}
}
private void isScrollCompleted() {
Log.d("Load", "Reload");
if (currentFirstVisibleItem == 0) {
//Your loading go here
}
}

Related

How to save boolean properly on Playerprefs?

I want to make a shop system if I buy the items, the items will be saved on Playerprefs, no need to buy again when I restart or re-open the game, for that I already make it to setInt but I don't know when I call getInt. I tried to call it in the start method with a new int index but it doesn't work. This my script
for store the itemsType
[System.Serializable]
public class m_ShopItem
{
public Sprite theIcon;
public int price;
public bool isBuyed;
}
Then I make a list
public List<m_ShopItem> ShopItemsList = new List<m_ShopItem>();
[SerializeField] GameObject ShopPanel;
[SerializeField] Transform theContent;
GameObject g;
Button buyBtn;
public GameObject itemTemplate;
private void Start()
{
for (int i = 0; i < ShopItemsList.Count; i++)
{
g = Instantiate(itemTemplate, theContent);
g.transform.GetChild(0).GetComponent<Image>().sprite = ShopItemsList[i].theIcon;
g.transform.GetChild(1).GetComponent<TextMeshProUGUI>().text = ShopItemsList[i].price.ToString();
buyBtn = g.transform.GetChild(2).GetComponent<Button>();
if (ShopItemsList[i].isBuyed)
{
DisableBuyButton();
}
ShopItemsList[i].isBuyed = PlayerPrefs.GetInt("isBuyed") == 1 ? true : false;// i call it Get here
Debug.Log(ShopItemsList[i].isBuyed);
buyBtn.AddEventListener(i, OnShopItemBtnClicked);
}
}
void OnShopItemBtnClicked(int itemIndex)
{
if (m_shopManager.Instance.HasEnoughCoins(ShopItemsList[itemIndex].price))
{
m_shopManager.Instance.UseCoins(ShopItemsList[itemIndex].price);
//purchase Item
ShopItemsList[itemIndex].isBuyed = true;
PlayerPrefs.SetInt("isBuyed", ShopItemsList[itemIndex].isBuyed ? 1 : 0); // SetThe Int here when button clicked and buy the item
//disable the button
buyBtn = theContent.GetChild(itemIndex).GetChild(2).GetComponent<Button>();
DisableBuyButton();
//add avatar
m_Profile.Instance.AddBallType(ShopItemsList[itemIndex].theIcon);
}
else
{
//NoCoinsAnim.SetTrigger("NoCoins");
Debug.Log("You don't have enough coins!!");
}
}
void DisableBuyButton()
{
buyBtn.interactable = false;
buyBtn.transform.GetChild(0).GetComponent<Text>().text = "PURCHASED";
}
but the result is the same I have to rebuy when going to the main menu or restart the game.
Can anyone tell me what's wrong with my script?
Your current problem is that you create only one PlayerPrefs Object with the value being equal to 1 or 0.
So if you buy only one of your shop items all of them get unlocked. Because you only Check if the PlayerPrefs is true(1) or false (0).
To fix that we would need to create a List of unique PlayerPrefs equal to the amount of shop items. We can do that if we add the index in the ShopItemsList to the isBuyed name.
Create initial Playerprefs:
// Called before the Start Function
private void Awake() {
// Loop through all shopping Items
for (int i = 0; i < ShopItemsList.Count; i++) {
// Check if the Item was already bought
bool bought = PlayerPrefs.GetInt("isBuyed"+i) == 1 ? true : false;
// If it was we can't reset the score.
if (!bought) {
// Create a different PlayerPrefs for each Item in the Shop.
PlayerPrefs.SetInt("isBuyed"+i, ShopItemsList[i].isBuyed ? 1 : 0);
}
}
}
...
Now we can check for that PlayerPrefs instead of checking for just once in Start().
Check if Items were bought:
private void Start() {
// Loop through all shopping Items
for (int i = 0; i < ShopItemsList.Count; i++) {
...
// Actualize our values so already bought items can't be bought again
ShopItemsList[i].isBuyed = PlayerPrefs.GetInt("isBuyed"+i) == 1 ? true : false;
// Check if the item was bought
if (ShopItemsList[i].isBuyed) {
DisableBuyButton();
}
...
}
}
We also need to make sure that we set the right PlayerPrefs when we buy a Product from the Shop. To ensure that we can just add the itemIndex parameter we get from the function and add that to the isBuyed name.
Buying Items:
void OnShopItemBtnClicked(int itemIndex) {
...
// Buy the Item.
ShopItemsList[itemIndex].isBuyed = true;
PlayerPrefs.SetInt("isBuyed"+itemIndex, ShopItemsList[itemIndex].isBuyed ? 1 : 0);
...
}

Foreach loop not working in android for custom object

I am trying to store the count of number of times an item from a list view is clicked in a custom array list but in my code the quantity of only the first clicked item is increasing and if i click on any other item then it gets added to the list view instead of increasing the quantity. Please tell me if you know any other method to get the count. Thanks
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Flowers item = (Flowers) parent.getAdapter().getItem(position);
if(item == null)
{
Toast.makeText(getBaseContext(),"item is null",Toast.LENGTH_LONG).show();
}else {
selectedItems items = new selectedItems();
items.setName(item.getName());
items.setCategory(item.getCategory());
Counter counter = new Counter();
if(selectedItemsArrayList.isEmpty())
{
counter.CartItemPosition = 0;
counter.MenuItemPosition = position;
counter.quantity = 1;
items.setQuantity(counter.quantity);
count.add(counter);
selectedItemsArrayList.add(items);
}
else
{
for(Counter c : count)
{
if(c.MenuItemPosition == position)
{
c.quantity = c.quantity + 1;
int i = c.CartItemPosition;
selectedItemsArrayList.get(i).setQuantity(c.quantity);
break;
}
else
{
Counter obj = new Counter();
obj.MenuItemPosition = position;
obj.CartItemPosition++;
obj.quantity=1;
count.add(obj);
items.setQuantity(obj.quantity);
selectedItemsArrayList.add(items);
return;
}
}
}
}
}
});
and my Counter class is
public class Counter {
public int CartItemPosition;
public int MenuItemPosition;
public int quantity;}

Is there any way to find a string's position and click on it using robotium?

I have the following list appearing on UI of an android application which tells about the date and the temperature on that date.
temp degree
11/01 --.-- c
11/02 21.7 c
11/03 22.5 c
Here I want to find the position of string "--.--" and then click on it, so that I can update the list. Is there any way to find the position of string? I know that solo.searchText() will tell whether the string is present or not, but what about the position?
// i think it will help you
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, final View view,
int position, long id) {
// TODO Auto-generated method stub
if(yourliststring.trim().equals("--.--"))
{
//your condition
}
else
{
//your condition
}
}
});
I got a simple way to find and click on the string
if(solo.searchText("--.--")){
solo.clickLongOnText("--.--");
}
All I need is to click on the string "--.--".
I'm not sure what position you need, however if you need position on screen you can simple do:
TextView textView = solo.getText("--.--"); // or solo.getText(Pattern.quote("--.--")); if needed
int[] xy = new int[2];
textView.getLocationOnScreen(xy);
final int viewWidth = textView.getWidth();
final int viewHeight = textView.getHeight();
final float x = xy[0] + (viewWidth / 2.0f);
final float y = xy[1] + (viewHeight / 2.0f);
this way you have central point of view (x, y).
If you need position of text in list, you can do:
private int getPosition(ListView listView, String text) {
for (int i = 0; i < listView.getAdapter().getCount(); i++) {
Object o = listView.getAdapter.getItemAtPosition(i);
if (o instanceof TextView) {
if (((TextView)o).getText().toString().equals(text)) {
return i;
}
}
}
return -1; // not found
}
you can call it for instance this way (set proper parameters):
int position = getPosition((ListView) solo.getView(ListView.class, 0), "--.--");
Everything written without testing and even compiling, however main idea should be fine.

How to get the index of an item at a specific offset from the top in Android ListView

Is there a simple way for getting the index of a list item which is in a specific offset from the top?
For example, get the index of an item which appears 150 pixels from the top.
Since your goal is to find which list item is in the center of the screen, you could try something like the following:
(Make a custom class that extends ListView, such as MyListView.java)
public class MyListView extends ListView implements OnScrollListener {
public MyListView(Context context, AttributeSet attrs) {
super(context, attrs);
setOnScrollListener(this);
}
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// Center the child currently closest to the center of the ListView when
// the ListView stops scrolling.
if (scrollState == OnScrollListener.SCROLL_STATE_IDLE) {
int listViewCenterX = getWidth() / 2;
int listViewCenterY = getHeight() / 2;
Rect rect = new Rect();
// Iterate the children and find which one is currently the most
// centered.
for (int i = 0; i < getChildCount(); ++i) {
View child = getChildAt(i);
child.getHitRect(rect);
if (rect.contains(listViewCenterX, listViewCenterY)) {
// this listitem is in the "center" of the listview
// do what you want with it.
final int position = getPositionForView(child);
final int offset = listViewCenterY - (child.getHeight() / 2);
break;
}
}
}
}
#Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
}
}
You can iterate through children and check each ones hit rectangle against the point you have.

Custom ProgressBar widget

I am trying to do something similar to this but in Android.
In Android I can extend the ProgressBar but I am doubting of how to add the TextViews on top. In iphone it was easy because I can use absolute positions, but not here.
Any ideas?
EDIT:
I decided to use SeekBar instead of ProgressBar to add the thumb drawable. I commented below. Some points to notice:
I am using hardcoded values, actually three but it can be more or less.
When the thumb is moved it moves to 50 but it should move to the different options.
I am using pixels instead of dpi. I should fix that.
I need to solve the lack of animation when the thumb moves.
My progress so far:
public class SliderFrameLayout extends FrameLayout implements OnSeekBarChangeListener {
private SeekBar mSlider;
private Context mContext;
private int mSize = 3;
private TextView[] mTextViews;
private String[] mTexts = {"Nafta", "Gas", "Gasoil"};
public SliderFrameLayout(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
setWillNotDraw(false);
mTextViews = new TextView[mSize];
addSlider();
addTextViews();
}
private void addTextViews() {
for ( int i=0 ; i < mSize ; i++ ) {
TextView tv;
tv = new TextView(mContext);
tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
tv.setText(mTexts[i]);
mTextViews[i] = tv;
addView(tv);
}
}
private void addSlider() {
FrameLayout fl = new FrameLayout(mContext, null);
LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
fl.setLayoutParams(params);
fl.setPadding(30, 30, 30, 0);
mSlider = new SeekBar(mContext, null);
LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
//lp.setMargins(30, 30, 30, 0);
//mSlider.setPadding(30, 30, 30, 0);
mSlider.setLayoutParams(lp);
//mSlider.setMax(mSize-1);
mSlider.setThumbOffset(30);
//mSlider.setProgressDrawable(mContext.getResources().getDrawable(R.drawable.slider_track));
//mSlider.setThumb(mContext.getResources().getDrawable(R.drawable.slider_thumb));
mSlider.setOnSeekBarChangeListener(this);
//addView(mSlider);
fl.addView(mSlider);
addView(fl);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Rect rectf = new Rect();
mSlider.getLocalVisibleRect(rectf);
Log.d("WIDTH :",String.valueOf(rectf.width()));
Log.d("HEIGHT :",String.valueOf(rectf.height()));
Log.d("left :",String.valueOf(rectf.left));
Log.d("right :",String.valueOf(rectf.right));
Log.d("top :",String.valueOf(rectf.top));
Log.d("bottom :",String.valueOf(rectf.bottom));
int sliderWidth = mSlider.getWidth();
int padding = sliderWidth / (mSize-1);
for ( int i=0 ; i < mSize ; i++ ) {
TextView tv = mTextViews[i];
tv.setPadding(i* padding, 0, 0, 0);
}
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
Log.d("SEEK", "value: " + seekBar.getProgress());
seekBar.setProgress(50);
}
}
You're already overriding onDraw, why not just draw the text strings yourself? Rather than go through the overhead of adding TextViews and messing with the padding, just use canvas.drawText to physically draw the text strings in the right place.
You can specify the style and color of the text using a Paint object:
Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
textPaint.setColor(r.getColor(R.color.text_color));
textPaint.setFakeBoldText(true);
textPaint.setSubpixelText(true);
textPaint.setTextAlign(Align.LEFT);
And get the exact positioning by using the measureText method on that Paint object to find what width a particular string would be when drawn on a canvas:
textWidth = (int)textPaint.measureText(mTexts[i]);
Then you can iterate over your array of text strings and draw each string in the right place.
#Override
protected void onDraw(Canvas canvas) {
int myWidth = getMeasuredWidth()-LEFT_PADDING-RIGHT_PADDING;
int separation = myWidth / (mSize-1);
for (int i = 0; i++; i < mSize) {
int textWidth = (int)textPaint.measureText(mTexts[i]);
canvas.drawText(mTexts[i],
LEFT_PADDING+(i*separation)-(int)(textWidth/2),
TOP_PADDING,
textPaint);
}
}
You'll probably want to do the measurements in onMeasure instead of onDraw, and you should probably only measure the width of each string when you change the text or the paint, but I've put it all in one place to (hopefully) make it easier to follow.
Just a try. You could put your custom progressBar inside a FrameLayout then, inside this layout, you have to add three TextViews with fill_parent as width.
Then you can align the three texts in this way: left, center and right. Your texts shouldn't overwrite and you can adjust a little their position using margins.
You can use this... not exact what you looking for... but really easy to set up and customize...
You can set the TextViews to have:
android:layout_width="0dp"
android:layout_weight="1"
This will let the android framework take care of the placing of the TextViews and save you from manually calculating the padding of each one.