Skip to content

Instantly share code, notes, and snippets.

@FoxIvan
Last active May 25, 2017 11:42
Show Gist options
  • Save FoxIvan/cefe28220d9aee67c3dbac006079b8d3 to your computer and use it in GitHub Desktop.
Save FoxIvan/cefe28220d9aee67c3dbac006079b8d3 to your computer and use it in GitHub Desktop.
ItemDecoration that draws an divider between items in a RecyclerView
public class DividerItemDecoration extends RecyclerView.ItemDecoration {

    private Drawable mDivider;

    public DividerItemDecoration(Context context) {
        mDivider = context.getDrawable(R.drawable.divider);
    }

    @Override
    public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
        int left = parent.getPaddingLeft();
        int right = parent.getWidth() - parent.getPaddingRight();
        for (int i = 0; i < parent.getChildCount(); 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