3

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.

Siraj Sumra
  • 934
  • 1
  • 11
  • 28
  • Possible duplicate of [RecyclerView GridLayoutManager: how to auto-detect span count?](https://stackoverflow.com/questions/26666143/recyclerview-gridlayoutmanager-how-to-auto-detect-span-count) – Umar Ata Feb 13 '18 at 09:49
  • also check https://stackoverflow.com/questions/36218787/staggered-grid-layout-manager-dynamic-number-of-columns – Umar Ata Feb 13 '18 at 09:50
  • You could try a FlexBoxLayout https://github.com/google/flexbox-layout – David Medenjak Feb 13 '18 at 10:51

3 Answers3

1

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

Jaymin
  • 2,879
  • 3
  • 19
  • 35
1

I came across such a requirement and ideally, a Flexbox should be a good option to try.

-3

layoutManager=new StaggeredGridLayoutManager(4, LinearLayoutManager.HORIZONTAL);