Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove default spacing in recycler view grid layout

I'm using StaggeredGridLayout manager for recycler view

mStaggerGridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager
                .VERTICAL);

Now, I want to remove default spacing there is between columns and rows. Something like in this image but with only 2 columns.

enter image description here

like image 804
crysis Avatar asked Nov 09 '22 18:11

crysis


1 Answers

You have to play around with margin. Not the padding.

The StaggeredGridLayoutManager sets a default margin of "30dp" for each grid item.

It can be changed as follows,

class StaggeredListDecoration extends RecyclerView.ItemDecoration {

    public StaggeredListDecoration() {

    }
    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        BaseCard.CARD_TYPE viewType = (BaseCard.CARD_TYPE)view.getTag();
            ((StaggeredGridLayoutManager.LayoutParams) view.getLayoutParams()).leftMargin = 0;
            ((StaggeredGridLayoutManager.LayoutParams) view.getLayoutParams()).rightMargin = 0;
            ((StaggeredGridLayoutManager.LayoutParams) view.getLayoutParams()).topMargin = 0;
            ((StaggeredGridLayoutManager.LayoutParams) view.getLayoutParams()).bottomMargin= 0;
    }
}
like image 81
Praga Avatar answered Nov 23 '22 11:11

Praga