Skip to content

Instantly share code, notes, and snippets.

@hector6872
Last active May 28, 2017 15:04
Show Gist options
  • Save hector6872/40bea787e739fc7c3ab0 to your computer and use it in GitHub Desktop.
Save hector6872/40bea787e739fc7c3ab0 to your computer and use it in GitHub Desktop.
ColorDividerItemDecoration for RecyclerView
public final class ColorDividerItemDecoration extends RecyclerView.ItemDecoration {
private static final int DEFAULT_DIVIDER_COLOR = Color.GRAY;
private static final int DEFAULT_DIVIDER_SIZE_PX = 1;
private static final boolean DEFAULT_DIVIDER_SHOW_FIRST = false;
private static final boolean DEFAULT_DIVIDER_SHOW_LAST = false;
private final boolean showFirstDivider;
private final boolean showLastDivider;
private final Paint paint;
public ColorDividerItemDecoration() {
this(DEFAULT_DIVIDER_COLOR, DEFAULT_DIVIDER_SIZE_PX, DEFAULT_DIVIDER_SHOW_FIRST, DEFAULT_DIVIDER_SHOW_LAST);
}
public ColorDividerItemDecoration(@ColorInt int color) {
this(color, DEFAULT_DIVIDER_SIZE_PX, DEFAULT_DIVIDER_SHOW_FIRST, DEFAULT_DIVIDER_SHOW_LAST);
}
public ColorDividerItemDecoration(@ColorInt int color, int sizePx) {
this(color, sizePx, DEFAULT_DIVIDER_SHOW_FIRST, DEFAULT_DIVIDER_SHOW_LAST);
}
public ColorDividerItemDecoration(@ColorInt int color, int sizePx, boolean showFirstDivider, boolean showLastDivider) {
paint = new Paint();
paint.setColor(color);
paint.setStrokeWidth(sizePx);
this.showFirstDivider = showFirstDivider;
this.showLastDivider = showLastDivider;
}
@Override public void onDrawOver(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
float left = parent.getPaddingLeft();
float right = parent.getWidth() - parent.getPaddingRight();
float top = parent.getPaddingTop();
float bottom = parent.getHeight() - parent.getPaddingBottom();
int orientation = getOrientation(parent);
int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
View child = parent.getChildAt(i);
RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
if ((showFirstDivider && i == 0) || i > 0) {
if (orientation == LinearLayoutManager.VERTICAL) {
top = child.getTop() - params.topMargin;
bottom = top - (paint.getStrokeWidth() / 2);
canvas.drawLine(left, bottom, right, bottom, paint);
} else {
left = child.getLeft() - params.leftMargin;
right = left - (paint.getStrokeWidth() / 2);
canvas.drawLine(right, top, right, bottom, paint);
}
}
if (showLastDivider && i == childCount - 1) {
if (orientation == LinearLayoutManager.VERTICAL) {
top = child.getBottom() - params.bottomMargin;
bottom = top + (paint.getStrokeWidth() / 2);
canvas.drawLine(left, bottom, right, bottom, paint);
} else {
left = child.getRight() + params.rightMargin;
right = left + (paint.getStrokeWidth() / 2);
canvas.drawLine(right, top, right, bottom, paint);
}
}
}
}
private int getOrientation(RecyclerView parent) {
if (parent.getLayoutManager() instanceof LinearLayoutManager) {
LinearLayoutManager layoutManager = (LinearLayoutManager) parent.getLayoutManager();
return layoutManager.getOrientation();
} else {
throw new IllegalStateException("ColorDividerItemDecoration can only be used with a LinearLayoutManager.");
}
}
}
@hector6872
Copy link
Author

RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.addItemDecoration(new ColorDividerItemDecoration());

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