为了账号安全,请及时绑定邮箱和手机立即绑定

RecyclerView存储/还原活动之间的状态

RecyclerView存储/还原活动之间的状态

慕姐4208626 2019-11-25 15:30:06
我正在将ListViews迁移到RecyclerViews。对于列表视图,我使用了此处介绍的通用技术来存储和恢复活动之间的滚动位置。如何使用RecyclerViews进行相同操作?在RecyclerView.onSaveInstanceState()似乎有protected访问权限,因此无法直接使用。
查看完整描述

3 回答

?
人到中年有点甜

TA贡献1895条经验 获得超7个赞

好吧,所以回答我自己的问题。据我了解,由于它们已经将布局代码和视图回收代码(即名称)分离了,因此负责保持布局状态(并恢复其状态)的组件现在已LayoutManager在您的recyclerview中使用。


因此,要存储状态,请使用相同的模式,但要在布局管理器而不是recyclerview上:


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);

    }

}


查看完整回答
反对 回复 2019-11-25
?
侃侃尔雅

TA贡献1801条经验 获得超16个赞

我找到了一个更好的解决方案 -该解决方案具有以下优点:


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);

        }

    }

}


查看完整回答
反对 回复 2019-11-25
?
慕仙森

TA贡献1827条经验 获得超7个赞

那是我的解决方案,它可以还原项目和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 (savedInstanceState==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);

}


查看完整回答
反对 回复 2019-11-25
  • 3 回答
  • 0 关注
  • 1033 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信