Android. StaggeredGridLayoutManager places the cells in the wrong position

455 Views Asked by At

I use a recyclerView with StaggeredGridLayoutManager. I have two columns, cells in columns can be of different heights. The items are positioned fine, however on some devices I noticed the following problem. Suppose I have a list of elements: A,B,C,D,E,F,G,H. Visually, it looks like this:

My columns image

I expected that the G element would be in the left column, since the elements are located from left to right, tell me what might be the reason for this behavior. I need the element to be in the left column. The G element should fit into the free position of the left column, but it is located on the right, which is incorrect for me This is only playable on some devices. I expect this order of element, in the left column: A,C,E,G, in the right column: B,D,F,H. I try to set gapStrategy to GAP_HANDLING_NONE, but after that my items are not showing:

sglm.gapStrategy = StaggeredGridLayoutManager.GAP_HANDLING_NONE
1

There are 1 best solutions below

1
On

If you don't want your LayoutManager to fill up the gaps, you can change the gapStrategy like this

    val lm = StaggeredGridLayoutManager(...)

    lm.gapStrategy = StaggeredGridLayoutManager.GAP_HANDLING_NONE

default handling is

lm.gapStrategy = StaggeredGridLayoutManager.GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS