Refreshing data in RecyclerView and keeping its scroll position

android, android-recyclerview

Solution

I use this one.^_^

// Save state
private Parcelable recyclerViewState;
recyclerViewState = recyclerView.getLayoutManager().onSaveInstanceState();

// Restore state
recyclerView.getLayoutManager().onRestoreInstanceState(recyclerViewState);

It is simpler, hope it will help you!

Problem

How does one refresh the data displayed in `RecyclerView` (calling `notifyDataSetChanged` on its adapter) and make sure that the scroll position is reset to exactly where it was? In case of good ol' `ListView` all it takes is retrieving `getChildAt(0)`, checking its `getTop()` and calling `setSelectionFromTop` with the same exact data afterwards. It doesn't seem to be possible in case of `RecyclerView`. I guess I'm supposed to use its `LayoutManager` which indeed provides `scrollToPositionWithOffset(int position, int offset)`, but what's the proper way to retrieve the position and the offset? `layoutManager.findFirstVisibleItemPosition()` and `layoutManager.getChildAt(0).getTop()`? Or is there a more elegant way to get the job done?

Original source

Related problems