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)
Asked
Active
Viewed 343 times
3

anna
- 433
- 7
- 18
2 Answers
1
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
}
}
}
}

dbm
- 10,376
- 6
- 44
- 56
0
- To get the Column from the position: You can use the remainder/modulus of the position by 3.
- To get the Row from the position: You can divide it by 3.
0 | 1 | 2
3 | 4 | 5
6 | 7 | 8
9 | 10 | 11
12 | 13 | 14
15 | 16 | 17
18 | 19 | 20
Example
for (int position = 0; position <= 20; position++) {
Log.d("LOG_TAG", "Item: " + position + " In Row: " + position / 3 + " In Col: " + position % 3);
}
Logs:
2021-04-21 00:33:41.634 14288-14288/.. D/LOG_TAG: Item: 0 In Row: 0 In Col: 0
2021-04-21 00:33:41.634 14288-14288/.. D/LOG_TAG: Item: 1 In Row: 0 In Col: 1
2021-04-21 00:33:41.634 14288-14288/.. D/LOG_TAG: Item: 2 In Row: 0 In Col: 2
2021-04-21 00:33:41.634 14288-14288/.. D/LOG_TAG: Item: 3 In Row: 1 In Col: 0
2021-04-21 00:33:41.634 14288-14288/.. D/LOG_TAG: Item: 4 In Row: 1 In Col: 1
2021-04-21 00:33:41.634 14288-14288/.. D/LOG_TAG: Item: 5 In Row: 1 In Col: 2
2021-04-21 00:33:41.634 14288-14288/.. D/LOG_TAG: Item: 6 In Row: 2 In Col: 0
2021-04-21 00:33:41.634 14288-14288/.. D/LOG_TAG: Item: 7 In Row: 2 In Col: 1
2021-04-21 00:33:41.634 14288-14288/.. D/LOG_TAG: Item: 8 In Row: 2 In Col: 2
2021-04-21 00:33:41.634 14288-14288/.. D/LOG_TAG: Item: 9 In Row: 3 In Col: 0
2021-04-21 00:33:41.634 14288-14288/.. D/LOG_TAG: Item: 10 In Row: 3 In Col: 1
2021-04-21 00:33:41.635 14288-14288/.. D/LOG_TAG: Item: 11 In Row: 3 In Col: 2
2021-04-21 00:33:41.635 14288-14288/.. D/LOG_TAG: Item: 12 In Row: 4 In Col: 0
2021-04-21 00:33:41.635 14288-14288/.. D/LOG_TAG: Item: 13 In Row: 4 In Col: 1
2021-04-21 00:33:41.635 14288-14288/.. D/LOG_TAG: Item: 14 In Row: 4 In Col: 2
2021-04-21 00:33:41.635 14288-14288/.. D/LOG_TAG: Item: 15 In Row: 5 In Col: 0
2021-04-21 00:33:41.635 14288-14288/.. D/LOG_TAG: Item: 16 In Row: 5 In Col: 1
2021-04-21 00:33:41.635 14288-14288/.. D/LOG_TAG: Item: 17 In Row: 5 In Col: 2
2021-04-21 00:33:41.635 14288-14288/.. D/LOG_TAG: Item: 18 In Row: 6 In Col: 0
2021-04-21 00:33:41.635 14288-14288/.. D/LOG_TAG: Item: 19 In Row: 6 In Col: 1
2021-04-21 00:33:41.635 14288-14288/.. D/LOG_TAG: Item: 20 In Row: 6 In Col: 2

Zain
- 37,492
- 7
- 60
- 84
-
Thank you for answer, but my cells can have different height (square or rectangle) – anna Apr 21 '21 at 18:13
-
@anna I would say that it's hard to one without sharing how you design the height of these squares and rectangles – Zain Apr 22 '21 at 21:48