我有RecyclerView哪些视图是从互联网填充的(页码是参数),这就是我使用EndlessRecyclerViewScrollListener的原因。唯一的问题是,当我旋转设备时,它不会恢复相同的状态。
这是我在 onCreate()中的ScrollListener启动:
mScrollListener = new EndlessRecyclerViewScrollListener(mGridLayoutManager) {
@Override
public void onLoadMore(int page, int totalItemsCount, RecyclerView view) {
mPage++;
new TheMovieRequestTask().execute();
}
};
mRecyclerView.addOnScrollListener(mScrollListener);
的onSaveInstanceState():
protected void onSaveInstanceState(Bundle outState) {
outState.putInt("mPageKey", mPage);
mListState = mRecyclerView.getLayoutManager().onSaveInstanceState();
outState.putParcelable("mListStateKey", mListState);
super.onSaveInstanceState(outState);
}
在 onCreate()的开头,我有:
if (savedInstanceState != null) {
mPage = savedInstanceState.getInt("mPageKey");
mBundleRecyclerViewState = new Bundle();
mListState = savedInstanceState.getParcelable("mListStateKey");
mBundleRecyclerViewState.putParcelable("mListStateBundleKey", mListState);
} else {
mPage = 1;
}
当我从TheMovieRequestTask()(它是AsyncTask)加载数据时,在 onPostExecute()中我有:
if (mBundleRecyclerViewState != null) {
Parcelable state = mBundleRecyclerViewState.getParcelable("mListStateBundleKey");
mRecyclerView.getLayoutManager().onRestoreInstanceState(state);
}
我该怎么办?当我从另一项活动回来时,它是完美的,但在轮换时,它只是不起作用。
此代码加载例如第5页,然后正确恢复它,但然后我只能向下滚动(6,7,8,9 ...页面),但不向上(不是4,3,2,1,页面)。