I am really curious why there is no good and optimized solution for this general problem. As I understand it, everyone decided in their own way. but I want to introduce my own path by trying them all:
Inside the ListFragment, declare two int variables to hold the scroll position:
public static class MyListFragment extends ListFragment { private int listIndex = -1; private int listTop = 0;
Then override onPause () and onResume () to save and restore the scroll positions of the ListView as follows:
@Override public void onResume() { super.onResume(); //after setting adapter and your arbitrary codes if(listIndex!=-1){ this.getListView().setSelectionFromTop(index, top); } } @Override public void onPause() { super.onPause(); try { listIndex = getListView().getFirstVisiblePosition(); View v = getListView().getChildAt(0); listTop = (v != null) ? v.getTop() : 0; } catch (Exception e) { e.printStackTrace(); } }
The desire to reduce pain !: Blush:
SHi ON
source share