Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Retain & restore recycler view scroll position
package eu.f3rog.ui.custom;
import android.content.Context;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
/**
* Class {@link StatefulRecyclerView} extends {@link RecyclerView} and adds position management on configuration changes.
*
* @author FrantisekGazo
* @version 2016-03-15
*/
public final class StatefulRecyclerView
extends RecyclerView {
private static final String SAVED_SUPER_STATE = "super-state";
private static final String SAVED_LAYOUT_MANAGER = "layout-manager-state";
private Parcelable mLayoutManagerSavedState;
public StatefulRecyclerView(Context context) {
super(context);
}
public StatefulRecyclerView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public StatefulRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected Parcelable onSaveInstanceState() {
Bundle bundle = new Bundle();
bundle.putParcelable(SAVED_SUPER_STATE, super.onSaveInstanceState());
bundle.putParcelable(SAVED_LAYOUT_MANAGER, this.getLayoutManager().onSaveInstanceState());
return bundle;
}
@Override
protected void onRestoreInstanceState(Parcelable state) {
if (state instanceof Bundle) {
Bundle bundle = (Bundle) state;
mLayoutManagerSavedState = bundle.getParcelable(SAVED_LAYOUT_MANAGER);
state = bundle.getParcelable(SAVED_SUPER_STATE);
}
super.onRestoreInstanceState(state);
}
/**
* Restores scroll position after configuration change.
* <p>
* <b>NOTE:</b> Must be called after adapter has been set.
*/
private void restorePosition() {
if (mLayoutManagerSavedState != null) {
this.getLayoutManager().onRestoreInstanceState(mLayoutManagerSavedState);
mLayoutManagerSavedState = null;
}
}
@Override
public void setAdapter(Adapter adapter) {
super.setAdapter(adapter);
restorePosition();
}
}
@abeemukthees

This comment has been minimized.

Copy link

commented Sep 23, 2016

Thank you for the code, this works inside fragment 👍 , but not working inside activity 👎

@timbaev

This comment has been minimized.

Copy link

commented Oct 3, 2016

How can I use it?

@ByErdem

This comment has been minimized.

Copy link

commented Oct 14, 2016

For use:

Parcelable parce;

Save the recyclerview state:
parce = recyclerview.onSaveInstanceState();

set the recyclerview state
recyclerview.onRestoreState(parce);

You can pray for me to thank.

@Miha-x64

This comment has been minimized.

Copy link

commented Jan 17, 2017

@ByErdem why do you think these methods should be called manually?

@DevangiChhatbar119

This comment has been minimized.

Copy link

commented Jan 8, 2018

How can I use it..? please give some instructions.

@valterh4ck3r

This comment has been minimized.

Copy link

commented Mar 17, 2018

Make a Readme.MD

@jrm-d

This comment has been minimized.

Copy link

commented Apr 11, 2018

Hello ! It works perfectly, thanks a lot.
For people asking how to use it: import the file in your project, and just declare your StatefulRecyclerview in your layout files instead of the native "android.support.v7.widget.RecyclerView"

@jwhijazi

This comment has been minimized.

Copy link

commented Oct 29, 2018

Error inflating class StatefulRecyclerView

@tusharpingale04

This comment has been minimized.

Copy link

commented Feb 18, 2019

Thanks for the code. It's Working!

@MahmoudiOussama

This comment has been minimized.

Copy link

commented Apr 8, 2019

Thanks a lot.
Working for me too, tested it on Android 6, 8 and 9 devices.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.