[android] Refreshing data in RecyclerView and keeping its scroll position

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?

This question is related to android android-recyclerview

The answer is

I use this one.^_^

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

// Restore state

It is simpler, hope it will help you!

Similar questions with android tag:

Similar questions with android-recyclerview tag: