For the RecycleView layouts in a grid form with different cell spans can be achieved by using the GridLayoutManager. You will find many tutorials in the internet about how to implement it.

GridLayoutManager  layoutManager = new GridLayoutManager(this, 6);

layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
    public int getSpanSize(int position) {
        // Use whatever logic you need here to decide how 
        //  many columns to span for any given row position
        if (position % 2)
            return 3;
            return 6;

Then in onBindViewHolder update the LayoutParams.width of each view as you may require.

  • 1
Reply Report
      • 2
    • Then for such case is better to use a GridLayoutManager, and update the views metrics when binding them. So use the lookup to specify how many columns a row must span, and the onBindViewHolder to update the individual views metrics to render accordingly the required columns area.
      • 1
    • I also tried this earlier. This layout manager also required spanCount which problematic is because I do not know the count of both coloumns and rows.
    • getting Cannot resolve method 'setSpanSizeLookup(anonymous` error.

Warm tip !!!

This article is reproduced from Stack Exchange / Stack Overflow, please click

Trending Tags

Related Questions