2

我有在 Fragment 中使用 GridLayoutManger 的 RecyclerView,如下所示:

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
//some code
mGridLayoutManager = new GridLayoutManager(getActivity(), 2, GridLayoutManager.VERTICAL, false);
mRecyclerView.setLayoutManager(mGridLayoutManager);
//some code
}

RecyclerView 由从 CursorLoader 加载数据的适配器填充,我的问题是我正在尝试恢复保存的 GridLayoutManger 滚动位置以在设备旋转时使用,如下所示:

    @Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    mPosition=mGridLayoutManager.findFirstVisibleItemPosition();
    outState.putInt(GRID_LAYOUT_SCROLL_KEY,mPosition);
}


    @Override
public void onViewStateRestored(@Nullable Bundle savedInstanceState) {
    super.onViewStateRestored(savedInstanceState);
    if (savedInstanceState != null) {
        mPosition=savedInstanceState.getInt(GRID_LAYOUT_SCROLL_KEY);
        mGridLayoutManager.scrollToPosition(mPosition);
    }
}

但是当旋转发生时没有任何变化,好像 scrollToPosition() 方法根本不起作用。我试着把“mGridLayoutManager.scrollToPosition(mPosition);” 在“onLoadFinished”内,但我仍然得到相同的结果!问题是什么?

4

0 回答 0