How to keep scrollbar position in android list?

Friends,

I am showing an array in a ListView.

I change the data of the array dynamically at the click of a button and call

adapter.notifyDataSetInvalidated();

It does not support the position of the scrollbar of the list. (the length of the data source for the list is always the same). Can someone tell me how to save the last state of a ListView?

Any help would be appreciated.

+5
source share
2 answers

Try the following:

//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);
+10
source

If you want to update some changes to the data, use

notifyDataSetChanged();

Instead. And the scroll will be in the same position.

+3
source

All Articles