Search code examples
androidandroid-recyclerviewgridlayoutmanager

Android Recycler view using Grid Layout Manager equal spacing not working


Recycler view item decoration not equally spaced when span count is odd. When I set even span, the spacing looks equal but when set odd span count the middle column spacing is little bit disturbed. How to resolve this issue? The code is:

final int spacing = getResources().getDimensionPixelSize(R.dimen.recycler_spacing);
// apply spacing
recyclerView.setPadding(spacing, spacing, spacing, spacing);
recyclerView.setClipToPadding(false);
recyclerView.setClipChildren(false);
recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
    outRect.set(spacing, spacing, spacing, spacing);
}
});

Screenshot


Solution

  • You can add margin in your row xml for equal spacing in your recyclerview. Its the easy way to add equal spacing between your list items.