Hey guys, how can I add a header to a recycle view...
# android
n
Hey guys, how can I add a header to a recycle view, that is another recycle view? Something like stories on Instagram. Where if you're all the way up, you can see them as you scroll they disappear.
👍 2
google 5
stackoverflow 2
n
@David Miguel thank you, but stories are horizontal while the posts are vertical. And there seems to be no way of merging the two to keep the horizontal scrolling of the stories...
d
your
HeaderAdapter
can inflate another horizontal recycler view
n
@David Miguel Oh wow, that is actually perfect! Could you please give me a brief explanation on how to achieve this?
z
You really just need to create 2 adapters (if you go the MergeAdapter route), where one displays
1
item of a horizontal recycler view, and the other displays
n
items of the vertical list. Then put the two adapters in a MergeAdapter, now you don't even need to worry about item view types. That's what's thrown people off for almost a decade.
n
@zhuinden i am not following. If I have like 10 items that are scrollable likes stories on instagram and have 10 posts that are vertical, if I merge them in a single recycle view using the mergeadapter, I get the layout of the recycleview same for both adapters. I basically need the header adapter to be horizontal with multiple items scrollable to the side while still having the ability to scroll downwards. So somehow inflating a new recycle view with linearlayout horizontal seems the way to go. Just dont quite get how to do it. Sorry and thanks for the answer
z
it should be really as simple as having an item in the RecyclerView where the layout contains a RecyclerView that has a horizontal linear layout manager.
n
But wait, how can I use 2 recycleviews with merge adapter? I am not having trouble with horizontal layout but rather I have a grid layout recycleview with merge adapter. And now I have to get 1 adapter to be horizontal while the other one is vertical, but they use the same recycleview right?
@zhuinden
z
you would use 1 vertical recyclerview, that combines 2 adapters with mergeadapter, where the first adapter defines an item that inflates a RecyclerView that is horizontal.
n
Ohhhh, now I get it, the concept at least, but in reality how would I go about this. And how would I keep track of this recycleview and use it to attach other items of the first adapter. sorry for bothering you, but this is a breakthrough for me
@zhuinden any input on how to achieve this? I couldnt word it properly and havent found anything online
z
And how would I keep track of this recycleview and use it to attach other items of the first adapter. sorry for bothering you, but this is a breakthrough for me
now that is a bit tricky question indeed, but you should not overthink it: you just have to get the data into the adapter of the horizontal recycler view. You have only 1 horizontal recyclerview, so that should be easy. I had to implement a vertical RecyclerView where each item had a RecyclerView, and I had to track in a
Map<String, LiveData<List<T>>
which item to show in which row, and manage the observers in
onAttachedToRecyclerView
,
onDetachedFromRecyclerView
(i think that's what it's called) and
onBindViewHolder
. That was tough, but it worked reliably in the end.
hopefully that helps. technically it really is in theory "as simple as" inflating a layout that contains a RecyclerView as an item, and setting it up as an item view type. and getting the list to show into that item.
n
Thank you so much! I have a direction now.