anna
anna

Reputation: 433

Android. StaggeredGridLayoutManager determine span index

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)

Upvotes: 3

Views: 538

Answers (2)

dbm
dbm

Reputation: 10485

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
            }
        }
    }
}

Upvotes: 1

Zain
Zain

Reputation: 40830

  • 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

Upvotes: 0

Related Questions