Fix for false restore where swipe to dismiss scrolls to the top of the list

This commit is contained in:
Martin Fietz 2015-03-06 16:41:28 +01:00
parent 006f793fae
commit d3e1fcdcba
1 changed files with 26 additions and 11 deletions

View File

@ -116,13 +116,7 @@ public class QueueFragment extends Fragment {
@Override @Override
public void onPause() { public void onPause() {
super.onPause(); super.onPause();
SharedPreferences prefs = getActivity().getSharedPreferences(PREFS, Context.MODE_PRIVATE); saveScrollPosition();
SharedPreferences.Editor editor = prefs.edit();
View v = listView.getChildAt(0);
int top = (v == null) ? 0 : (v.getTop() - listView.getPaddingTop());
editor.putInt(PREF_KEY_LIST_SELECTION, listView.getFirstVisiblePosition());
editor.putInt(PREF_KEY_LIST_TOP, top);
editor.commit();
} }
@Override @Override
@ -138,6 +132,30 @@ public class QueueFragment extends Fragment {
this.activity.set((MainActivity) activity); this.activity.set((MainActivity) activity);
} }
private void saveScrollPosition() {
SharedPreferences prefs = getActivity().getSharedPreferences(PREFS, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
View v = listView.getChildAt(0);
int top = (v == null) ? 0 : (v.getTop() - listView.getPaddingTop());
editor.putInt(PREF_KEY_LIST_SELECTION, listView.getFirstVisiblePosition());
editor.putInt(PREF_KEY_LIST_TOP, top);
editor.commit();
}
private void restoreScrollPosition() {
SharedPreferences prefs = getActivity().getSharedPreferences(PREFS, Context.MODE_PRIVATE);
int listSelection = prefs.getInt(PREF_KEY_LIST_SELECTION, 0);
int top = prefs.getInt(PREF_KEY_LIST_TOP, 0);
if(listSelection > 0 || top > 0) {
listView.setSelectionFromTop(listSelection, top);
// restore once, then forget
SharedPreferences.Editor editor = prefs.edit();
editor.putInt(PREF_KEY_LIST_SELECTION, 0);
editor.putInt(PREF_KEY_LIST_TOP, 0);
editor.commit();
}
}
private void resetViewState() { private void resetViewState() {
unregisterForContextMenu(listView); unregisterForContextMenu(listView);
listAdapter = null; listAdapter = null;
@ -374,10 +392,7 @@ public class QueueFragment extends Fragment {
} }
listAdapter.notifyDataSetChanged(); listAdapter.notifyDataSetChanged();
SharedPreferences prefs = getActivity().getSharedPreferences(PREFS, Context.MODE_PRIVATE); restoreScrollPosition();
int listSelection = prefs.getInt(PREF_KEY_LIST_SELECTION, 0);
int top = prefs.getInt(PREF_KEY_LIST_TOP, 0);
listView.setSelectionFromTop(listSelection, top);
// we need to refresh the options menu because it sometimes // we need to refresh the options menu because it sometimes
// needs data that may have just been loaded. // needs data that may have just been loaded.