kanytu / Android-studio-material-template

A template for Android Studio to create applications with material design and Navigation Drawer.
http://androidshenanigans.blogspot.pt/2015/03/material-design-template.html
Apache License 2.0
591 stars 145 forks source link

Item Dividers #27

Open vleango opened 9 years ago

vleango commented 9 years ago

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

Thank you.

rajeshct commented 8 years ago

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 <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">

<solid android:color="#d3d3d3" />