crysis
crysis

Reputation: 1584

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

Upvotes: 5

Views: 3268

Answers (1)

Praga
Praga

Reputation: 86

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;
    }
}

Upvotes: 1

Related Questions