Android:RecyclerView GridLayoutManager - 恢复滚动位置无法正常工作

时间:2016-11-14 07:37:34

标签: android android-recyclerview onsaveinstancestate

我有3项活动: A - > B - > ç

在活动B中,我使用RecyclerView的GridlayoutManager进行填充。我想在导航到活动C时保存滚动状态,并在从活动C返回活动B时恢复滚动状态。

private RecyclerView mImgList;
private GridLayoutManager mRecyclerGridMan;
private final String KEY_RECYCLER_STATE = "recycler_state";
private Parcelable mListState = null;
private static Bundle mBundleRecyclerViewState;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

@Override
protected void onPause()
{
    super.onPause();
    mBundleRecyclerViewState = new Bundle();
    mListState = mImgList.getLayoutManager().onSaveInstanceState();
    mBundleRecyclerViewState.putParcelable(KEY_RECYCLER_STATE, mListState);
}

@Override
protected void onResume()
{
    super.onResume();
    if (mBundleRecyclerViewState != null) {
        mListState = mBundleRecyclerViewState.getParcelable(KEY_RECYCLER_STATE);
        mImgList.getLayoutManager().onRestoreInstanceState(mListState);
    }
}

但是当我按下活动B的后退按钮并转到活动A,并从活动A导航回活动B时,这种方法有效。

1 个答案:

答案 0 :(得分:0)

恢复onRestoreInstanceState()

中的状态
k = reverse_d[30]
# k = '2'