RecyclerView的分割线自定义宽度,颜色

垂直方向:CommItemDecoration.createVertical( Color.YELLOW,30)

水平方向:CommItemDecoration.createHorizontal( Color.YELLOW,30)



```

public class CommItemDecorationextends RecyclerView.ItemDecoration {

private static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL;

    private static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL;

    private int mSpace =1;

    private RectmRect =new Rect(0,0,0,0);

    private PaintmPaint =new Paint();

    private int mOrientation;

    private CommItemDecoration(int orientation, @ColorInt int color, int space) {

mOrientation = orientation;

        if(space>0){

mSpace = space;

        }

mPaint.setColor(color);

    }

@Override

    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {

super.onDraw(c, parent, state);

        if (mOrientation ==VERTICAL_LIST) {

drawVertical(c, parent);

        }else {

drawHorizontal(c, parent);

        }

}

public void drawVertical(Canvas c, RecyclerView parent) {

final int left = parent.getPaddingLeft();

        final int right = parent.getWidth() - parent.getPaddingRight();

        final int childCount = parent.getChildCount();

        for (int i =0; i < childCount; i++) {

final View child = parent.getChildAt(i);

            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child

.getLayoutParams();

            final int top = child.getBottom() + params.bottomMargin;

            final int bottom = top +mSpace;

            mRect.set(left,top,right,bottom);

            c.drawRect(mRect,mPaint);

        }

}

public void drawHorizontal(Canvas c, RecyclerView parent) {

final int top = parent.getPaddingTop();

        final int bottom = parent.getHeight() - parent.getPaddingBottom();

        final int childCount = parent.getChildCount();

        for (int i =0; i < childCount; i++) {

final View child = parent.getChildAt(i);

            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child

.getLayoutParams();

            final int left = child.getRight() + params.rightMargin;

            final int right = left +mSpace;

            mRect.set(left, top, right, bottom);

            c.drawRect(mRect,mPaint);

        }

}

@Override

    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {

super.getItemOffsets(outRect, view, parent, state);

        if (mOrientation ==VERTICAL_LIST) {

outRect.set(0, 0, 0,mSpace);

        }else {

outRect.set(0, 0, mSpace, 0);

        }

}

public static CommItemDecorationcreateVertical(@ColorInt int color, int height){

return new CommItemDecoration(VERTICAL_LIST,color,height);

    }

public static CommItemDecorationcreateHorizontal(@ColorInt int color, int width){

return new CommItemDecoration(HORIZONTAL_LIST,color,width);

    }

}

```

你可能感兴趣的:(RecyclerView的分割线自定义宽度,颜色)