I have a fragment that contains a RecyclerView. When I return to the fragment from somewhere else, I do
if(null!=savedInstanceState) {
mScrollPosition = savedInstanceState.getInt(ITEM_POSITION);
mRecyclerView.scrollToPosition(mScrollPosition);
}
but now I don't know what to put inside onSaveInstanceState
:
@Override
public void onSaveInstanceState(Bundle bundle){
super.onSaveInstanceState(bundle);
Log.d(TAG, "onSaveInstanceState");
mScrollPosition=mRecyclerView.getScrollPosition();//???
bundle.putInt(NITEM_POSITION,mScrollPosition);
}
There is no mRecyclerView.getScrollPosition()
.
update
after testing it turns out that using mRecyclerView.scrollToPosition
is not fine enough to ensure a smooth user experience. In cases where the item views are tall enough, scrollToPosition(ofFirstVisibleItem)
can be way off. So is there a finer solution to this?