I use a recycleView
with StaggeredGridLayoutManager
(number Of column is 3). I need to determine in which column the current cell is located (by position). Is it possible to do this? Please help me.
Note: the height of my cells is different (square or rectangle)
Android. StaggeredGridLayoutManager determine span index
449 Views Asked by anna At
2
There are 2 best solutions below
0

I think you could use the LayoutParams
of the child view to determine the span index. An example with an ItemDecoration
could then look something like so (Kotlin):
class MyItemDecoration : RecyclerView.ItemDecoration() {
override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
super.getItemOffsets(outRect, view, parent, state)
val params = view.layoutParams as StaggeredGridLayoutManager.LayoutParams
when(params.spanIndex) {
0 -> outRect.apply {
left = 20
right = 10
}
1 -> outRect.apply {
left = 10
right = 10
}
2 -> outRect.apply {
left = 10
right = 20
}
}
}
}
Example
Logs: