RecyclerView分割线:颜色和高度

最基本的分割线,包括垂直或者水平两个方向,可以设定颜色和高度(宽度)。

使用:

  1. 垂直方向:CommItemDecoration.createVertical(context, Color.BLUE,30)
  2. 水平方向:CommItemDecoration.createHorizontal(context, Color.BLUE,30)

代码:

public class CommItemDecoration extends RecyclerView.ItemDecoration {

    private static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL;
    private static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL;

    private int mSpace = 1;     //间隔
    private Rect mRect = new Rect(0,0,0,0);
    private Paint mPaint = new Paint();

    private int mOrientation;

    private CommItemDecoration(Context context, 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 CommItemDecoration createVertical(Context context, @ColorInt int color, int height){
        return new CommItemDecoration(context,VERTICAL_LIST,color,height);
    }

    public static CommItemDecoration createHorizontal(Context context, @ColorInt int color, int width){
        return new CommItemDecoration(context,HORIZONTAL_LIST,color,width);
    }
}

    原文作者:蒸汽飞船
    原文地址: https://www.jianshu.com/p/ff0332ae1a59
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞