Skip to content

Instantly share code, notes, and snippets.

@yeongjoshua
Last active August 29, 2015 14:21
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 yeongjoshua/6c0a8cce63584c05811e to your computer and use it in GitHub Desktop.
Save yeongjoshua/6c0a8cce63584c05811e to your computer and use it in GitHub Desktop.
SimpleRecyclerViewDecoration for adding divider / custom drawable divider in RecyclerView
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
public class SimpleRecyclerViewDecoration extends RecyclerView.ItemDecoration {
private Drawable mDivider;
private boolean mShowFirstDivider = false;
private boolean mShowLastDivider = false;
public SimpleRecyclerViewDecoration(Context context){
final TypedArray a = context.obtainStyledAttributes(null, new int[]{android.R.attr.listDivider});
mDivider = a.getDrawable(0);
a.recycle();
}
public SimpleRecyclerViewDecoration(Context context, boolean showFirstDivider, boolean showLastDivider) {
this(context);
mShowFirstDivider = showFirstDivider;
mShowLastDivider = showLastDivider;
}
public SimpleRecyclerViewDecoration(Drawable divider) {
mDivider = divider;
}
public SimpleRecyclerViewDecoration(Drawable divider, boolean showFirstDivider, boolean showLastDivider) {
this(divider);
mShowFirstDivider = showFirstDivider;
mShowLastDivider = showLastDivider;
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
int currentItem = parent.getChildAdapterPosition(view);
int totalItem = parent.getAdapter().getItemCount();
int orientation = getOrientation(parent);
if (orientation == LinearLayoutManager.VERTICAL) {
int mDividerHeight = mDivider.getIntrinsicHeight();
if(totalItem == 1){
if(mShowFirstDivider) outRect.top = mDividerHeight;
if(mShowLastDivider) outRect.bottom = mDividerHeight;
}else if(currentItem == 0 && mShowFirstDivider){
outRect.set(0, mDividerHeight, 0, mDividerHeight);
}else if(currentItem == totalItem-1 && mShowLastDivider) {
outRect.bottom = mDividerHeight;
}else if(currentItem != totalItem-1 ){
outRect.bottom = mDividerHeight;
}
} else {
int mDividerWidth = mDivider.getIntrinsicWidth();
if(totalItem == 1){
if(mShowFirstDivider) outRect.left = mDividerWidth;
if(mShowLastDivider) outRect.right = mDividerWidth;
}else if(currentItem == 0 && mShowFirstDivider){
outRect.set(mDividerWidth, 0, mDividerWidth, 0);
}else if(currentItem == totalItem-1 && mShowLastDivider) {
outRect.right = mDividerWidth;
}else if(currentItem != totalItem-1 ){
outRect.right = mDividerWidth;
}
}
}
@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
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(orientation == LinearLayoutManager.VERTICAL){
if(childCount == 1){
if(mShowFirstDivider) drawVerticalLayoutDivider_Top(parent, child, c, params);
if(mShowLastDivider) drawVerticalLayoutDivider_Bottom(parent, child, c, params);
}
else if(i == 0 && mShowFirstDivider) {
drawVerticalLayoutDivider_Top(parent, child, c, params);
drawVerticalLayoutDivider_Bottom(parent, child, c, params);
}
else if(i == childCount-1 && mShowLastDivider) {
drawVerticalLayoutDivider_Bottom(parent, child, c, params);
}
else if(i != childCount-1){
drawVerticalLayoutDivider_Bottom(parent, child, c, params);
}
}else {
if(childCount == 1){
if(mShowFirstDivider) drawHorizontalLayoutDivider_Left(parent, child, c, params);
if(mShowLastDivider) drawHorizontalLayoutDivider_Right(parent, child, c, params);
}
else if(i == 0 && mShowFirstDivider) {
drawHorizontalLayoutDivider_Left(parent, child, c, params);
drawHorizontalLayoutDivider_Right(parent, child, c, params);
}
else if(i == childCount-1 && mShowLastDivider) {
drawHorizontalLayoutDivider_Right(parent, child, c, params);
}
else if(i != childCount-1){
drawHorizontalLayoutDivider_Right(parent, child, c, params);
}
}
}
}
private void drawVerticalLayoutDivider_Top(RecyclerView parent, View child, Canvas c, RecyclerView.LayoutParams params){
int left = parent.getPaddingLeft();
int right = parent.getWidth() - parent.getPaddingRight();
int top = child.getTop() - params.topMargin - mDivider.getIntrinsicHeight();
int bottom = top + mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
private void drawVerticalLayoutDivider_Bottom(RecyclerView parent, View child, Canvas c, RecyclerView.LayoutParams params){
int left = parent.getPaddingLeft();
int right = parent.getWidth() - parent.getPaddingRight();
int top = child.getBottom() + params.topMargin;
int bottom = top + mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
private void drawHorizontalLayoutDivider_Left(RecyclerView parent, View child, Canvas c, RecyclerView.LayoutParams params){
int left = child.getLeft() - params.leftMargin - mDivider.getIntrinsicWidth();
int right = left + mDivider.getIntrinsicWidth();
int top = parent.getPaddingTop();
int bottom = parent.getHeight() - parent.getPaddingBottom();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
private void drawHorizontalLayoutDivider_Right(RecyclerView parent, View child, Canvas c, RecyclerView.LayoutParams params){
int left = child.getRight() + params.rightMargin;
int right = left + mDivider.getIntrinsicWidth();
int top = parent.getPaddingTop();
int bottom = parent.getHeight() - parent.getPaddingBottom();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
private int getOrientation(RecyclerView parent) {
if (parent.getLayoutManager() instanceof LinearLayoutManager) {
LinearLayoutManager layoutManager = (LinearLayoutManager) parent.getLayoutManager();
return layoutManager.getOrientation();
} else {
throw new IllegalStateException("SimpleRecyclerViewDecoration can only be used with a LinearLayoutManager.");
}
}
}

SimpleRecyclerViewDecoration

Simple RecyclerView ItemDecoration for divider

Features

  • Define own drawable as divider
  • Able to use android default divider if were not define
  • Custom setting on divider to show before first item or last item
  • Only works with LinearLayoutManager

Example

Default android divider

mRecyclerView.addItemDecoration(
  new SimpleRecyclerViewDecoration(context));

Default android divider with first item top divider or last item bottom divider

mRecyclerView.addItemDecoration(
  new SimpleRecyclerViewDecoration(context, true, true));

Custom divider with no first item top divider and last item bottom divider

mRecyclerView.addItemDecoration(
  new SimpleRecyclerViewDecoration(context.getDrawable(R.drawable.custom_drawable)));

Custom divider with first also end and last dividers

mRecyclerView.addItemDecoration(
  new SimpleRecyclerViewDecoration(context.getDrawable(R.drawable.custom_drawable), true, true));
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<size
android:width="2dp"
android:height="2dp" />
<solid android:color="#FF000000" />
</shape>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment