I wanted to display variable number of columns in a row of GridLayoutManager while using RecyclerView. The number of columns to be displayed depends on the size of the column's TextView.
I don't know the column width as the text is being dynamically put in it.
Can anyone help? StaggeredGridLayoutManager is not solving my purpose as it customizes the height but takes fixed number of columns.
Take a look at the
setSpanSizeLookup
method of theGridLayoutManager
. It lets you specify the span size for specific positions of yourRecyclerView
. So maybe you could use it to fit with your requirements for the variable column number.Edit:
When using this sort of layout
manager
yourRecyclerView
should look like this:(only boxes with numbers represent items of your
RecyclerView
, other boxes are just empty spaces)If you want to make a variation like: 4 columns, 5 columns, 6 columns... You can get the MMC (minimum multiple common) between this numbers (60) and set the GridLayoutManager:
Then you could return 10, 12 or 15 for 6, 5 and 4 columns on getSpanSize()