Friends,
I am displaying an array in ListView.
I change data of array dynamically on button click and call
adapter.notifyDataSetInvalidated();
It does not maintain scroll bar position of list. (the lenght of data source to list is always same). Could any one guide me how to keep the last state of ListView ?
Any help would be appreciated.
Try this:
//Get the top position from the first visible element
int idx = list.getFirstVisiblePosition();
View vfirst = list.getChildAt(0);
int pos = 0;
if (vfirst != null) pos = vfirst.getTop();
//Restore the position
list.setSelectionFromTop(idx, pos);
If you only want to refresh some changes in data, use
notifyDataSetChanged();
Instead. And scroll will be in the same position.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With