Search code examples
androidandroid-layoutandroid-recyclerviewstaggered-gridviewstaggeredgridlayout

Display tag like structure in Staggered Recyclerview


I am working on a tag structure currently and to display that I have used Staggeredgrid layout manager of Recyclerview to get the required result as below image:

enter image description here

But when I used StaggeredGrid, It is providing result as below:

enter image description here

My code to display adapter in Recyclerview is as given below:

val staggeredGridLayoutManager = StaggeredGridLayoutManager(2, LinearLayoutManager.VERTICAL)
rvTags.layoutManager = staggeredGridLayoutManager
rvTags.adapter = AllergyAdapter(tags)

On increasing the span count to 3, all the items are contracting. I want that if "Peanut butter" is big enough then only 1 item should come in 1 row and if "neem, garlic and soy" can fit in 1 row, then it should fit.

Basically, I need a way to dynamic span count according to the content size.

Any help will be appreciated.

The suggested answer is not working because it is counting span count for whole of Recyclerview and Not particular row wise.


Solution

  • Either you can go with the dynamic spinning or what you can do is get the help of libraries.

    Please check URL for tags libraries.

    Tags Views

    Here is a most suitable view: ChipView

    With custom layouts : ChipView

    And for dynamic column binding, you can use AsymmetricGridView.

    URL : AsymmetricGridView