Implemented restoring list position for AbstractListFragment
This commit is contained in:
parent
df8ebcd3b5
commit
9672ea6cf1
|
@ -47,6 +47,8 @@ public abstract class AbstractListFragment extends Fragment {
|
||||||
private static final String TAG = LogUtils.makeLogTag(AbstractListFragment.class);
|
private static final String TAG = LogUtils.makeLogTag(AbstractListFragment.class);
|
||||||
private BaseAdapter adapter;
|
private BaseAdapter adapter;
|
||||||
|
|
||||||
|
private final String BUNDLE_SAVEDINSTANCE_LISTPOSITION = "lposition";
|
||||||
|
|
||||||
private boolean gridViewUsesMultipleColumns;
|
private boolean gridViewUsesMultipleColumns;
|
||||||
|
|
||||||
@InjectView(R.id.swipe_refresh_layout) SwipeRefreshLayout swipeRefreshLayout;
|
@InjectView(R.id.swipe_refresh_layout) SwipeRefreshLayout swipeRefreshLayout;
|
||||||
|
@ -72,6 +74,16 @@ public abstract class AbstractListFragment extends Fragment {
|
||||||
adapter = createAdapter();
|
adapter = createAdapter();
|
||||||
gridView.setAdapter(adapter);
|
gridView.setAdapter(adapter);
|
||||||
|
|
||||||
|
if (savedInstanceState != null) {
|
||||||
|
final int listPosition = savedInstanceState.getInt(BUNDLE_SAVEDINSTANCE_LISTPOSITION);
|
||||||
|
gridView.post(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
gridView.setSelection(listPosition);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
//Listener added to be able to determine if multiple-columns is at all possible for the current grid
|
//Listener added to be able to determine if multiple-columns is at all possible for the current grid
|
||||||
//We use this information to enable/disable the menu item to switch between multiple and single columns
|
//We use this information to enable/disable the menu item to switch between multiple and single columns
|
||||||
gridView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
|
gridView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
|
||||||
|
@ -92,12 +104,19 @@ public abstract class AbstractListFragment extends Fragment {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
setHasOptionsMenu(true);
|
setHasOptionsMenu(true);
|
||||||
|
|
||||||
return root;
|
return root;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onSaveInstanceState(Bundle outState) {
|
||||||
|
super.onSaveInstanceState(outState);
|
||||||
|
if (gridView != null) {
|
||||||
|
outState.putInt(BUNDLE_SAVEDINSTANCE_LISTPOSITION, gridView.getFirstVisiblePosition());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
|
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
|
||||||
inflater.inflate(R.menu.abstractlistfragment, menu);
|
inflater.inflate(R.menu.abstractlistfragment, menu);
|
||||||
|
|
Loading…
Reference in New Issue