populate GridLayoutManager first by row and then by column
Asked Answered
B

2

10

I'm having an issue while populating a recycler view with a GridLayoutManager.

I need to fulfill the first row, and after this is complete, go to the second row. I did an algorithm that reorders the list so it's being shown properly and I can get the item clicked without doing any mapping-unmapping thing. This is working fine, but the trouble appears when all the items can be shown on the screen without scrolling.

For example, I have 4 items and I need to show them in a 2-row grid layout (horizontal layout). The screen has space for 3 items, so I need them to be in the first row, while the 4th item should be the only one in the second row.

To give you a picture of what I have by default:

0 2
1 3 

And what I need is:

0 1 2
3

Any ideas?

Edit: As requested, here is the code I'm using:

    GridLayoutManager layoutManager = new GridLayoutManager(getContext(),2,LinearLayoutManager.HORIZONTAL, false);
Boraginaceous answered 6/2, 2017 at 20:12 Comment(1)
did you find a sloutionElectromotive
B
0

When you are defining your gridLayoutManager, you specify the number of columns

RecyclerView rView = new RecyclerView(this);
rView.setLayoutManager(new GridLayoutManager(context, <your number of columns>));

Setting this value to 3 (in your case) would result in three columns, with two rows. Your array adapter populates the fields from left to right, then top to bottom.

Buckskins answered 30/3, 2017 at 15:0 Comment(4)
Failing this, you could add "null" or " " to your array until it is evenly divisible by the number of columns you want (in this instance, a total of 6 elements). This would populate two additional columns in the second row with nothing, but still adding them to the layout.Buckskins
The first approach was the one I tried and didn't work. By default, it's not displaying the items as you mentioned (check at the question how it was ordering the items). The second approach shouldn't work! I ended reordering the whole list to match the expected result, but without a clear solution to solve this. Thanks anyway!Boraginaceous
How are you declaring your gridLayoutManager, can you update your answer with some of your existing code?Buckskins
Updated. Remember that I want an horizontal grid!Boraginaceous
L
0

Not sure if this will solve your issue but you can have dynamic columns based on items type or position (or similar logic)

val layoutManager = GridLayoutManager(requireContext(), columnsCount)
layoutManager.spanSizeLookup = object: GridLayoutManager.SpanSizeLookup() {
    override fun getSpanSize(position: Int): Int {
        return adapter.getColumnsCount(position)
    }
}
rvList.layoutManager = layoutManager

and in your adapter a method that returns the columns

fun getColumnsCount(position: Int): Int {
    return when (getItem(position).type) {
        ItemType.FIRST -> 1
        ItemType.SECOND -> 3
    }
}

====== If all you need is to fit the items on screen, you can have fixed width items and calculate how many fit the screen:

val columnCount = recyclerView.width / resources.getDimensionPixelSize(R.dimen.grid_item_width)
Leela answered 13/9, 2022 at 10:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.