Restore scroll position on return to listview

Thanks again to stackoverflow. Wanted to position listview at same place it was left when the user clicked on an item to update/view it (in separate activity).

To save position – not just of top record but of the offset ie precise position relative to top:

int index = listview.fetFirstVisiblePosition();
View v = listview.getChildAt(0);
int offset = (v==null) ? 0 : (v.getTop() - listview.getPaddingTop();

Then when return (eg in onActivityResult):

listview.setSelectionFromTop(index, offset);

 

This entry was posted in Android. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *