How to save RecyclerView's scroll position using RecyclerView.State?
How do you plan to save last saved position with RecyclerView.State
?
You can always rely on ol' good save state. Extend RecyclerView
and override onSaveInstanceState() and onRestoreInstanceState()
:
@Override
protected Parcelable onSaveInstanceState() {
Parcelable superState = super.onSaveInstanceState();
LayoutManager layoutManager = getLayoutManager();
if(layoutManager != null && layoutManager instanceof LinearLayoutManager){
mScrollPosition = ((LinearLayoutManager) layoutManager).findFirstVisibleItemPosition();
}
SavedState newState = new SavedState(superState);
newState.mScrollPosition = mScrollPosition;
return newState;
}
@Override
protected void onRestoreInstanceState(Parcelable state) {
super.onRestoreInstanceState(state);
if(state != null && state instanceof SavedState){
mScrollPosition = ((SavedState) state).mScrollPosition;
LayoutManager layoutManager = getLayoutManager();
if(layoutManager != null){
int count = layoutManager.getItemCount();
if(mScrollPosition != RecyclerView.NO_POSITION && mScrollPosition < count){
layoutManager.scrollToPosition(mScrollPosition);
}
}
}
}
static class SavedState extends android.view.View.BaseSavedState {
public int mScrollPosition;
SavedState(Parcel in) {
super(in);
mScrollPosition = in.readInt();
}
SavedState(Parcelable superState) {
super(superState);
}
@Override
public void writeToParcel(Parcel dest, int flags) {
super.writeToParcel(dest, flags);
dest.writeInt(mScrollPosition);
}
public static final Parcelable.Creator<SavedState> CREATOR
= new Parcelable.Creator<SavedState>() {
@Override
public SavedState createFromParcel(Parcel in) {
return new SavedState(in);
}
@Override
public SavedState[] newArray(int size) {
return new SavedState[size];
}
};
}
How to Maintain Scroll position of recyclerView on Screen Rotation
I've tried everything suggested by everyone but finally, this gist work for me
https://gist.github.com/FrantisekGazo/a9cc4e18cee42199a287
I just import this in my project and replace the recyclerView provided by default with this
"StateFulRecyclerView" and it solved my problem and handle the scroll position automatically during screen rotation or any configuration change. No need to use onSaveInstanceState to maintain scroll positon.
How to save scroll position of RecyclerView in Android?
Save the current state of recycle view position @onPause:
positionIndex= llManager.findFirstVisibleItemPosition();
View startView = rv.getChildAt(0);
topView = (startView == null) ? 0 : (startView.getTop() - rv.getPaddingTop());
Restore the scroll position @onResume:
if (positionIndex!= -1) {
llManager.scrollToPositionWithOffset(positionIndex, topView);
}
or another way can be @onPause:
long currentVisiblePosition = 0;
currentVisiblePosition = ((LinearLayoutManager)rv.getLayoutManager()).findFirstCompletelyVisibleItemPosition();
restore @onResume:
((LinearLayoutManager) rv.getLayoutManager()).scrollToPosition(currentVisiblePosition);
currentVisiblePosition = 0;
How to save and restore scrolling position of the RecyclerView in a fragment when coming back from other activity to the same fragment?
After paying so many days, after trying many many solutions, finally found a working solution for my problem, thanks to https://stackoverflow.com/a/42563804/2128364
I've moved the working solution to another methods to make it work in the fragment.
Maybe this can save someone's day, Here is how it worked:
- onConfigurationChanged (as suggested in original solution), onSaveInstanceState or onViewRestoredState methods doesn't work, So you need onPause and onResume methods to save and restore the state respectively.
- You have to save the state in onPause method:
mBundleRecyclerViewState = new Bundle();
mListState = mRecyclerView.getLayoutManager().onSaveInstanceState();
mBundleRecyclerViewState.putParcelable(KEY_RECYCLER_STATE, mListState);
- And then Restore it in onResume Method:
if (mBundleRecyclerViewState != null) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
mListState = mBundleRecyclerViewState.getParcelable(KEY_RECYCLER_STATE);
mRecyclerView.getLayoutManager().onRestoreInstanceState(mListState);
}
}, 50);
}
mRecyclerView.setLayoutManager(staggeredGridLayoutManager);
Related Topics
How to Parse the Androidmanifest.Xml File Inside an .Apk Package
How to Set Ringtone in Android from My Activity
Set Up Device For Development (? No Permissions)
How to Open the "Front Camera" on the Android Platform
No Resource Found That Matches the Given Name '@Style/Theme.Appcompat.Light'
...Have You Declared This Activity in Your Androidmanifest.Xml
Change Value of R.String Programmatically
Low Picture/Image Quality When Capture from Camera
Using Alarmmanager to Start a Service at Specific Time
How to Run a Method Every X Seconds
Sending Message Through Whatsapp
How to Disable Copy/Paste From/To Edittext
How to Read Mms Data in Android
How to Handle Dynamic Json in Retrofit
How to Customize Listview Using Baseadapter
Lollipop:Draw Behind Statusbar with Its Color Set to Transparent
Android- Going Back to Previous Activity with Different Intent Value