How can I save the ListView
\'s scroll position when the ListView
is situated within a ListFragment
?
I think your solution is fine for touch mode but for me it wasn't enough. I needed to get the selector on the same selected item, not the first visible:
@Override
public void onStop() {
super.onStop();
ListView listView = this.getListView();
lastPosition = listView.getSelectedItemPosition();
int lastPositionInGroup = lastPosition - listView.getFirstVisiblePosition();
lastTop = listView.getChildAt( lastPositionInGroup ).getTop();
}
@Override
public void onLoadFinished(Loader loader, Cursor data) {
/* cursor adapter stuff here */
if (lastPosition != AdapterView.INVALID_POSITION) {
listView.setSelectionFromTop(
lastPosition,
lastTop != AdapterView.INVALID_POSITION ? lastTop : 0
);
}
}