Search code examples
androidandroid-recyclerviewgridlayoutmanager

GridLayoutmanager make item take 2 cells space, but have one cell's size


The question is a bit weird. I have a GridLayoutmanager linked with a Recyclerview, this Grid has two 5 rows, and 2 columns, every second row has the size of 2 columns.

CELL1-CELL2
C  E L L  3
CELL4-CELL5

The second row, has a cell with a size of two cells. This step, i have done it without any issues, now to my request.

I have a square Linearlayout, with the following code:

public class SquareLinearLayout extends LinearLayout {


public SquareLinearLayout(Context context) {
    super(context);
}

public SquareLinearLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public SquareLinearLayout(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    //noinspection SuspiciousNameCombination
    super.onMeasure(widthMeasureSpec, widthMeasureSpec);
}

}

I have applied this layout to the Grid's items, so every cell has a perfect width and height EXCEPT for the 3rd cell, because it has the size of two cells. Here's my problem, i want it to take the space of two cells, but not the size of two cells.


Solution

  • So I've done a naught trick to do it. I've created two custom views, one for the LinearLayout and one for the ImageView. in the LinearLayout, I've saved the widthMeasureSpec in a static constant in another class called Constants, and added a boolean to only save the value one time.

    in the ImageView, i took that value from Constants and applied it to the ImageView's onMeasure.

    A bit tricky, but did the work. 2 Cell space, but one cell size and most important, in the center.