Can't Restore Scroll Position After Rotation (recyclerview)
Can't restore the scroll posiotn of RecyclerView after rotation and after I close the app and open it again. In the first case I could use android:configChanges in Manifest, but it
Solution 1:
You need to follow this answer on stackoverflow.Change your code
publicclassMainActivityextendsAppCompatActivity {
privatefinalStringKEY_RECYCLER_STATE="recycler_state";
private MainAdapter adapter;
private LinearLayoutManager layoutManager;
private ImageView ivRefresh;
private ProgressBar progressBar;
private RecyclerView rvUsers;
privatestatic Bundle mBundleRecyclerViewState;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ivRefresh = (ImageView) findViewById(R.id.ivRefresh);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
rvUsers = (RecyclerView) findViewById(R.id.rvUsers);
layoutManager = newLinearLayoutManager(this);
rvUsers.setLayoutManager(layoutManager);
adapter = newMainAdapter(newArrayList<>(), this);
rvUsers.setAdapter(adapter);
Toolbartoolbar= (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
@OverrideprotectedvoidonPause()
{
super.onPause();
// save RecyclerView state
mBundleRecyclerViewState = newBundle();
ParcelablelistState= rvUsers.getLayoutManager().onSaveInstanceState();
mBundleRecyclerViewState.putParcelable(KEY_RECYCLER_STATE, listState);
}
@OverrideprotectedvoidonResume()
{
super.onResume();
// restore RecyclerView state if (mBundleRecyclerViewState != null) {
ParcelablelistState= mBundleRecyclerViewState.getParcelable(KEY_RECYCLER_STATE);
rvUsers.getLayoutManager().onRestoreInstanceState(listState);
}
}
}
Solution 2:
You need to override the onRestoreInstanceState()
and set recyclerView.getLayoutManager()
with the saved bundle
Parcelable savedRecyclerLayoutState ;
@OverridepublicvoidonRestoreInstanceState(@Nullable Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
if(savedInstanceState != null)
{
savedRecyclerLayoutState = savedInstanceState.getParcelable(BUNDLE_RECYCLER_LAYOUT);
rvUsers.getLayoutManager().onRestoreInstanceState(savedRecyclerLayoutState);
}
}
Also you need to override onResume()
in your activity
@OverrideprotectedvoidonResume() {
super.onResume();
if (savedRecyclerLayoutState != null) {
layoutManager.onRestoreInstanceState(savedRecyclerLayoutState );
}
}
Solution 3:
Initially you have to get the current scrolling position of the RecyclerView by calling:
intindex = rvUsers.getFirstVisiblePosition();
Then you have to save this value while orientation changes and when the RecyclerView is created again you have to move your RecyclerView to this index.
I suppose this could work:
rvUsers.smoothScrollToPosition(intindex)
if you're using older versions use
setSelection(int position)
and
getFirstVisiblePosition()
methods
Solution 4:
It's important to set adapter with already filled data. In this case the position is restored automatically.
Post a Comment for "Can't Restore Scroll Position After Rotation (recyclerview)"