Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GridLayoutManager with different column count per row

I'm trying to build a RecyclerView with a GridLayoutManager which has a variable column count per row, something like this:

enter image description here

The sum of the width of all items in the same row will always be the screen width.

I tried to re-organize the list of items, grouping them by list of rows, and then inflating a LinearLayout per row. It didn't work quite well.

So I'm stuck and out of ideas. Any help would be really appreciated

like image 620
moyo Avatar asked Mar 06 '17 15:03

moyo


1 Answers

You can use GridLayoutManager. To have different column count in row you have to override setSpanSizeLookup.

Example:

//spanCount = 3 (just for example)
GridLayoutManager gridLayoutManager = new GridLayoutManager(getAppContext(), spanCount);
gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
    @Override
    public int getSpanSize(int position) {
        //define span size for this position
        //some example for your first three items
        if(position == item1) {
            return 1; //item will take 1/3 space of row
        } else if(position == item2) {
            return 2; //you will have 2/3 space of row
        } else if(position == item3) {
            return 3; //you will have full row size item
        }
     }
});

I code sample above I just show have you can change item size. Pay attention that spanSize <= spanCount.

like image 185
Volodymyr Yatsykiv Avatar answered Nov 19 '22 16:11

Volodymyr Yatsykiv