Skip to content

Instantly share code, notes, and snippets.

@xingstarx
Forked from polbins/README.md
Last active December 13, 2016 05:58
Show Gist options
  • Save xingstarx/48189037912138117e4103a3a534cd88 to your computer and use it in GitHub Desktop.
Save xingstarx/48189037912138117e4103a3a534cd88 to your computer and use it in GitHub Desktop.
Simple RecyclerView Divider

Simple RecyclerView Divider

Simple Horizontal Divider Item Decoration for RecyclerView

    mRecyclerView.addItemDecoration(new SimpleDividerItemDecoration(
            getApplicationContext()
    	));

NOTE: Add item decoration prior to setting the adapter

public class SimpleDividerItemDecoration extends RecyclerView.ItemDecoration {
private Drawable mDivider;
public SimpleDividerItemDecoration(Context context) {
final TypedArray a = context.obtainStyledAttributes(new int[]{android.R.attr.listDivider});
mDivider = a.getDrawable(0);
a.recycle();
}
@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
int left = parent.getPaddingLeft();
int right = parent.getWidth() - parent.getPaddingRight();
int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
View child = parent.getChildAt(i);
RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
int top = child.getBottom() + params.bottomMargin;
int bottom = top + mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment