GridLayoutManager with different column count per row

后端 未结 1 628
孤独总比滥情好
孤独总比滥情好 2021-01-18 22:56

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

The sum of the width of all ite

相关标签:
1条回答
  • 2021-01-18 23:06

    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.

    0 讨论(0)
提交回复
热议问题