RecyclerView存储/恢复活动之间的状态
我将我的ListView迁移到RecyclerViews。 通过列表视图,我使用了这里描述的常用技术来存储和恢复活动之间的滚动位置。
如何做与RecyclerViews相同? RecyclerView.onSaveInstanceState()
似乎具有protected
访问权限,因此不能直接使用。
好的,回答我自己的问题。 据我了解,因为他们已经分离了布局代码和视图回收代码(因此名称),负责布局状态(和恢复)的组件现在是您的回收站视图中使用的LayoutManager
器。
因此,要存储状态您使用相同的模式,但在布局pipe理器,而不是回收站:
protected void onSaveInstanceState(Bundle state) { super.onSaveInstanceState(state); // Save list state mListState = mLayoutManager.onSaveInstanceState(); state.putParcelable(LIST_STATE_KEY, mListState); }
在onRestoreInstanceState()
恢复状态:
protected void onRestoreInstanceState(Bundle state) { super.onRestoreInstanceState(state); // Retrieve list state and list/item positions if(state != null) mListState = state.getParcelable(LIST_STATE_KEY); }
然后更新LayoutManager(我在onResume()
):
@Override protected void onResume() { super.onResume(); if (mListState != null) { mLayoutManager.onRestoreInstanceState(mListState); } }
我find了一个更好的解决scheme – 这个解决scheme有以下好处:
- RecyclerView状态保存并在手机旋转时恢复
- RecyclerView状态被保存并且在恢复到 RecyclerView的活动时被恢复(在另一个活动显示的时候没有被销毁 – 这意味着
onRestoreInstanceState()
不被调用!!)
码
public class ActivityItemList extends AppCompatActivity { private final String KEY_RECYCLER_STATE = "recycler_state"; private RecyclerView mRecyclerView; private static Bundle mBundleRecyclerViewState; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_list);//set to whatever layout name you have mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);//set to whatever view id you use // don't forget to set your adapter } @Override protected void onPause() { super.onPause(); // save RecyclerView state mBundleRecyclerViewState = new Bundle(); Parcelable listState = mRecyclerView.getLayoutManager().onSaveInstanceState(); mBundleRecyclerViewState.putParcelable(KEY_RECYCLER_STATE, listState); } @Override protected void onResume() { super.onResume(); // restore RecyclerView state if (mBundleRecyclerViewState != null) { Parcelable listState = mBundleRecyclerViewState.getParcelable(KEY_RECYCLER_STATE); mRecyclerView.getLayoutManager().onRestoreInstanceState(listState); } } }
在onPause()
和onResume()
使用这个代码来保存和恢复滚动位置 –
private Parcelable recyclerViewState; recyclerViewState = mrecyclerView.getLayoutManager().onSaveInstanceState();//save mrecyclerView.getLayoutManager().onRestoreInstanceState(recyclerViewState);//restore
这是我的解决scheme,它恢复了物品和RecyclerView的位置
1)在onSaveInstanceState方法中保存回收视图状态
@Override protected void onSaveInstanceState(Bundle outState) { Parcelable listState = myRecyclerView.getLayoutManager().onSaveInstanceState(); // putting recyclerview position outState.putParcelable(SAVED_RECYCLER_VIEW_STATUS_ID, listState); // putting recyclerview items outState.putParcelableArrayList(SAVED_RECYCLER_VIEW_DATASET_ID,mDataset); super.onSaveInstanceState(outState); }
2)在onCreate方法中检查savedInstanceState Bundle
@Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (mSavedInstanceState==null){ getRemoteData(); // No saved data, get data from remote }else{ restorePreviousState(); // Restore data found in the Bundle } }
3)如果屏幕已经旋转,则恢复回收商视图数据
public void restorePreviousState(){ // getting recyclerview position mListState = mSavedInstanceState.getParcelable(SAVED_RECYCLER_VIEW_STATUS_ID); // getting recyclerview items mDataset = mSavedInstanceState.getParcelableArrayList(SAVED_RECYCLER_VIEW_DATASET_ID); // Restoring adapter items mAdapter.setItems(mDataset); // Restoring recycler view position mRvMedia.getLayoutManager().onRestoreInstanceState(mListState); }