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 BaseAdapter adapter;
|
||||
|
||||
private final String BUNDLE_SAVEDINSTANCE_LISTPOSITION = "lposition";
|
||||
|
||||
private boolean gridViewUsesMultipleColumns;
|
||||
|
||||
@InjectView(R.id.swipe_refresh_layout) SwipeRefreshLayout swipeRefreshLayout;
|
||||
|
@ -72,6 +74,16 @@ public abstract class AbstractListFragment extends Fragment {
|
|||
adapter = createAdapter();
|
||||
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
|
||||
//We use this information to enable/disable the menu item to switch between multiple and single columns
|
||||
gridView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
|
||||
|
@ -92,12 +104,19 @@ public abstract class AbstractListFragment extends Fragment {
|
|||
}
|
||||
});
|
||||
|
||||
|
||||
setHasOptionsMenu(true);
|
||||
|
||||
return root;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSaveInstanceState(Bundle outState) {
|
||||
super.onSaveInstanceState(outState);
|
||||
if (gridView != null) {
|
||||
outState.putInt(BUNDLE_SAVEDINSTANCE_LISTPOSITION, gridView.getFirstVisiblePosition());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
|
||||
inflater.inflate(R.menu.abstractlistfragment, menu);
|
||||
|
|
Loading…
Reference in New Issue