How can I implement this kind of UI, where we have two recyclerViews. One scrolls horizontally and the second one vertically. when the second one scrolls first one also scrolls top together.
I tried to implement using NestedScrollView, but I had to make second recyclerView height wrap content which causes recyclerView not recycle.
The second way that I tried was having one recyclerView. And adding horizontal recyclerview as a header. The problem was to save header recyclerview scroll state when navigation. And there had been crashes when loading next page (paging 3) in header recyclerView.
The question is: Is there any optimal solution for this kind of ui?
CodePudding user response:
In cases, Like this, you don't have to use 2 RecyclerView
and you also have to avoid using RecyclerView
insideScrollView
. instead of this you have to use one vertical RecyclerView
with multitype view Adapter
.
in this way, you are going to have 2 different ViewHolder
one of them is a horizontal recyclerView (your top item) and the other one is your other items.
for learning multitype adapter you can see this:
I could not understand the meaning of "header" where you said "adding horizontal recyclerview as a header" but if you did what I told and the problem is the state of inner Horizontal recyclerView, I think probably you are calling setAdapter
method of horizontal RecyclerView in OnBind() method of your vertical recycler view, it is a common mistake that I have seen in many tutorials.
if you have done this mistake , try to call setAdapter of your inner recyclerView in the constructor of its viewHolder
and just update the list using yourHorizontalAdapter.notifyDataSetChanged()
in onBind() method of VerticalRecylerView
,
and if its not the case and your recyclerView is completely destroying see this link :How to save RecyclerView's scroll position using RecyclerView.State?