Search code examples
androidandroid-recyclerviewandroid-support-librarystaggeredgridlayout

RecyclerView/StaggeredGridLayoutManager: Force group of consecutive views into the same span/column


Currently, I'm using a RecyclerView to display a set of views with different view types, where a group of consecutive items belongs together: a header, a variable set of content items, and a separator/footer. Current situation Now I also want my UI to look good on bigger devices, aka use multiple panes, so I switched to a StaggeredGridLayoutManager. The problem here is that it spreads views evenly between all spans, means one group gets spread over multiple columns. Situation with staggered LayoutManager Thus, I would like to specify that the layout manager puts a consecutive set of views into the same span, and only then put the next group in the next one. What I want Is this somehow possible with the LayoutManager I use? Can I modify it to work like that without just duplicating and editing it? Support lib devs: are there any plans to introduce such behaviour?

Please tell me whether I should provide any other information, and when something isn't clear enough.


Solution

  • You can achieve your desired layout with the Support Library's StaggeredGridLayoutManager; you need to make each 'group' of items (e.g. header1 + 1-3 + footer) a single view returned by your adapter. This view could itself be a RecyclerView.

    That is you'll have one outer RecyclerView with a StaggeredGridLayoutManager and multiple inner RecyclerViews with LinearLayoutManagers. You can likely share a single RecycledViewPool between the inner RVs. You'll have to do some gymnastics to create appropriate adapters for the inner RVs but this is likely simpler than writing your own LayoutManager!!