Skip to content

Instantly share code, notes, and snippets.

@iamdeveloper-lopez
Created November 14, 2019 07:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iamdeveloper-lopez/59318caa93f1c590f85977e0eab48982 to your computer and use it in GitHub Desktop.
Save iamdeveloper-lopez/59318caa93f1c590f85977e0eab48982 to your computer and use it in GitHub Desktop.
Equally spacing recyclerView items with the provided space.
public class SpacingItemDecoration extends RecyclerView.ItemDecoration {
private static final String TAG = "SPACE";
private int spacing;
public SpacingItemDecoration(int spacing) {
this.spacing = spacing;
}
@Override
public void getItemOffsets(@NotNull Rect layout, @NotNull View view, RecyclerView parent, @NotNull RecyclerView.State state) {
RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
RecyclerView.Adapter adapter = parent.getAdapter();
int position = parent.getChildAdapterPosition(view);
if (layoutManager instanceof GridLayoutManager) {
GridLayoutManager manager = (GridLayoutManager) parent.getLayoutManager();
if (manager != null && adapter != null) {
int spanCount = manager.getSpanCount();
if (spanCount >= 2) {
int numOfRows = adapter.getItemCount() / spanCount;
int numOfColumns = spanCount;
int row = (position / spanCount) + 1;
int column = (position % spanCount) + 1;
if (column == 1) {
layout.left = spacing;
layout.right = spacing / 2;
} else if (column == numOfColumns) {
layout.left = spacing / 2;
layout.right = spacing;
} else {
layout.left = spacing / 2;
layout.right = spacing / 2;
}
if (row == 1) {
layout.top = spacing;
layout.bottom = spacing / 2;
} else if (row == numOfRows) {
layout.top = spacing / 2;
layout.bottom = spacing;
} else {
layout.top = spacing / 2;
layout.bottom = spacing / 2;
}
} else {
//GridLayoutManager with 1 span
int numOfItems = adapter.getItemCount();
int row = position + 1;
if (row == 1) {
layout.top = spacing;
layout.left = spacing;
layout.right = spacing;
layout.bottom = spacing / 2;
} else if (row == numOfItems) {
layout.top = spacing / 2;
layout.left = spacing;
layout.right = spacing;
layout.bottom = spacing;
} else {
layout.top = spacing / 2;
layout.left = spacing;
layout.right = spacing;
layout.bottom = spacing / 2;
}
}
}
} else if (layoutManager instanceof LinearLayoutManager) {
//Supporting LinearLayoutManager with Horizontal and Vertical Orientation
LinearLayoutManager manager = (LinearLayoutManager) parent.getLayoutManager();
if (manager != null && adapter != null) {
if (manager.getOrientation() == LinearLayoutManager.HORIZONTAL) {
int numOfItems = adapter.getItemCount();
int column = position + 1;
if (column == 1) {
layout.top = spacing;
layout.left = spacing;
layout.right = spacing / 2;
layout.bottom = spacing;
} else if (column == numOfItems) {
layout.top = spacing;
layout.left = spacing / 2;
layout.right = spacing;
layout.bottom = spacing;
} else {
layout.top = spacing;
layout.left = spacing / 2;
layout.right = spacing / 2;
layout.bottom = spacing;
}
} else {
int numOfItems = adapter.getItemCount();
int row = position + 1;
if (row == 1) {
layout.top = spacing;
layout.left = spacing;
layout.right = spacing;
layout.bottom = spacing / 2;
} else if (row == numOfItems) {
layout.top = spacing / 2;
layout.left = spacing;
layout.right = spacing;
layout.bottom = spacing;
} else {
layout.top = spacing / 2;
layout.left = spacing;
layout.right = spacing;
layout.bottom = spacing / 2;
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment