Android-studio-material-template icon indicating copy to clipboard operation
Android-studio-material-template copied to clipboard

Item Dividers

Open vleango opened this issue 9 years ago • 1 comments

Does anyone know a way to add a horizontal divider between items?

Thank you.

vleango avatar Jul 13 '15 10:07 vleango

You can use mDrawerList.addItemDecoration(new DividerItemDecoration(getActivity(), R.drawable.drawer_line)); //mDrawerList is RecyclerView

//Divider class public class DividerItemDecoration extends RecyclerView.ItemDecoration { private static final int[] ATTRS = new int[]{android.R.attr.listDivider}; private Drawable mDivider;

/**
 * Custom divider will be used
 */
public DividerItemDecoration(Context context, int resId) {
    mDivider = ContextCompat.getDrawable(context, resId);
}

@Override
public void onDraw(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);
    }
}

}

// drawable file

rajeshct avatar Jan 05 '16 19:01 rajeshct