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);
Juan Giorello
  • 333
  • 2
  • 13

2 Answers2

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.

keag
  • 299
  • 2
  • 15
  • 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. – keag Mar 30 '17 at 15:02
  • 1
    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! – Juan Giorello Mar 31 '17 at 16:29
  • How are you declaring your gridLayoutManager, can you update your answer with some of your existing code? – keag Mar 31 '17 at 20:30
  • Updated. Remember that I want an horizontal grid! – Juan Giorello Apr 03 '17 at 13:22
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)
DoruChidean
  • 7,941
  • 1
  • 29
  • 33