Skip to content

Instantly share code, notes, and snippets.

@yqritc
Last active July 25, 2023 07:14
Show Gist options
  • Save yqritc/ccca77dc42f2364777e1 to your computer and use it in GitHub Desktop.
Save yqritc/ccca77dc42f2364777e1 to your computer and use it in GitHub Desktop.
Equal column spacing for Android RecyclerView GridLayoutManager by using custom ItemDecoration

ItemOffsetDecoration

public class ItemOffsetDecoration extends RecyclerView.ItemDecoration {

    private int mItemOffset;

    public ItemOffsetDecoration(int itemOffset) {
        mItemOffset = itemOffset;
    }

    public ItemOffsetDecoration(@NonNull Context context, @DimenRes int itemOffsetId) {
        this(context.getResources().getDimensionPixelSize(itemOffsetId));
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent,
            RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        outRect.set(mItemOffset, mItemOffset, mItemOffset, mItemOffset);
    }
}

Implementation

In your source code, add ItemOffsetDecoration to your recyclerview.
Item offset value should be half size of the actual value you want to add as space between items.

mRecyclerView.setLayoutManager(new GridLayoutManager(context, NUM_COLUMNS);
ItemOffsetDecoration itemDecoration = new ItemOffsetDecoration(context, R.dimen.item_offset);
mRecyclerView.addItemDecoration(itemDecoration);

Also, set item offset value as padding for its recyclerview, and specify android:clipToPadding=false.

<android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerview_grid"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clipToPadding="false"
    android:padding="@dimen/item_offset"/>

DONE. You will get an equal spaces around items.

@SanjaGrgurevic
Copy link

This worked like a charm! Thank you 👍

@EduMaxwell
Copy link

U rock!

@hellaandrew
Copy link

What a nice class you made. Good work!

@ranjansingh1991
Copy link

hey just i want to leave space below end of recyclerview with grid view.

@ShahoodulHassan
Copy link

God bless u! Perfect solution....works in case of LinearLayout as well.
@ygritc Can u please explain the whole code a bit more? We might be able to customize it then according to our needs.
Thanks a million!

@AlexSuvorov2k
Copy link

So simple.. Thank you!

@Ravslee
Copy link

Ravslee commented Jan 3, 2019

When the orientation changes, things are not getting changed? help@

@Syjgin
Copy link

Syjgin commented Mar 13, 2019

Don't works: GridLayout still adds default spacing (30dp) between elements

@jihadkahil
Copy link

Thank You! Works Like a charm!

@sidthakkar12
Copy link

I have spanCount = 2, it still takes right spacing of left-side item and left spacing of right-side item which increases space between the two !!!

@GerganaT
Copy link

Awesome-worked perfect!

@alok8bb
Copy link

alok8bb commented Jul 21, 2021

Thanks!

@manju1375
Copy link

manju1375 commented Jan 29, 2022

Perfect with (itemoffest = 10dp) !! Thank you !!

@xinpengfei520
Copy link

Worked-perfect! Thank you very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment