Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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.

like image 542
UMAR-MOBITSOLUTIONS Avatar asked Jan 10 '11 09:01

UMAR-MOBITSOLUTIONS


2 Answers

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);
like image 58
Francesco Laurita Avatar answered Nov 13 '22 07:11

Francesco Laurita


If you only want to refresh some changes in data, use

notifyDataSetChanged();

Instead. And scroll will be in the same position.

like image 36
forlayo Avatar answered Nov 13 '22 07:11

forlayo