RecyclerView's GridLayoutManager dynamic span count
In that case you should make your grid layout have more than 3 cells. You'd need to pick a number that works for all three types of cells, a 6 is good because to have 3 cells by row you'd return a 2. To have 2 cells by row you'd return a 3 and to have 1 cell by row you'd return a 6:
val layoutManager = GridLayoutManager(this, 6)
layoutManager.spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
override fun getSpanSize(position: Int): Int {
when (position) {
0, 1, 2 -> return 2
3, 4 -> return 3
5 -> return 6
else -> return 2
}
}
}
![Gunaseelan](https://i.stack.imgur.com/0J39C.jpg?s=256&g=1)
Gunaseelan
As a Android developer I have Play store account https://play.google.com/store/apps/developer?id=Gunaseelan+Arumaikkannu And I have a blog with sample tutorials in http://v4all123.blogspot.in/ And also some video samples in https://www.youtube.com/watch?v=MZxVIE9-_G8&list=PLZZqikn3YE-PSKfB8dHFqzn0wr62tIqCR
Updated on June 09, 2022Comments
-
Gunaseelan about 2 years
I am using following code dynamically change span count.
val layoutManager = GridLayoutManager(this, 3) layoutManager.spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() { override fun getSpanSize(position: Int): Int { when (position) { 0, 1, 2 -> return 1 3, 4 -> return 2 5 -> return 3 else -> return 1 } } }
And I got following output. But I want D and E should horizontally equally aligned. I don't how to do it.
Actually I have 3 types in adapter, HEADER, TYPE_A, TYPE_B. HEADER should have only one row, and TYPE_A is 3 rows, TYPE_B is 2 rows.
So may I get help to make some columns should have one 1 row, and some have only 2 rows(horizontally equally aligned) and some have 3 rows.
-
coding.cat3 over 3 yearsHey , what if i want to display three items in the first row, but only two in the second one, since we cant give spansize as 1.5 since it only allows INT , what is the best approach to do that, so i want 3 items in top row , and two items in bottom row equally spaced
-
Zubair Akber over 2 years@coding.cat3 did you get any solution, if yes kindly post your solution I am also looking for the same problem