Save loaded RecyclerView inside another RecyclerView after Scroll

Viewed 42

I have a RecyclerView inside another RecyclerView, the problem is that after I scroll the first RecyclerView, then when I return to the top, the inner RecyclerView is recreated and accordingly resets the scrolled position.

How to make it so that when the main RecyclerView is scrolled, the secondary RecyclerView are saved and not reset?

This is implemented in the Play market, I need to do the same.

As it looks to me

How it looks in the Play store

1 Answers

You can do this by changing the code in onBindViewHolder() of outer recycler view I believe this is where you must be adding the adapter to your inner RecyclerView.

Once you have added the recycler view you can save the inner recycler view's state by checking the findFirstVisibleItemPosition() method of LayoutManager of inner RecyclerView when the inner view is done scrolling. To listen to inner RecyclerView's scrolling you must use a addOnScrollListener() on inner RecyclerView. The best place to save this state would be in the initial list you provide in your outer RecyclerView.

Lastly when your onBindViewHolder() is called just set the adapter and call scrollToPosition() to scroll to the last scrolled state

Related