how to maintain scroll bar position in android listview?
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 b开发者_开发百科ar 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.
精彩评论