自定义简单的分割线,可以设置大小颜色和左右边距

package com.android.view;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.ColorInt;
import androidx.recyclerview.widget.RecyclerView;


/**

    简单分割线,可以设置大小颜色,和左右边距 ,只能用于 LinearLayoutManager
 *  ps:当设置了 分割线的左右边距之后 可能空出来位置会带有颜色,
 *  如果空出的位置的背景颜色 不是你想要的可以调用setMarginBgColor()方法手动设置
 */
public class SimpleItemDecoration extends RecyclerView.ItemDecoration {
    public static final int HORIZONTAL = LinearLayout.HORIZONTAL;
    public static final int VERTICAL = LinearLayout.VERTICAL;


    //分割线距离左右两边的边距
    private int mStartMargin = 0;
    private int mEndMargin = 0;
    //分割线的大小
    private int mSize = 0;
    //分割线的颜色
    private int mColor = Color.TRANSPARENT;
    //是否需要绘制最后一行的分割线
    private boolean mIsNeedLast = false;
    //当设置分割线距离左右两边的边距时,此时空出来的位置的背景颜色,一般设置成跟item的背景颜色一致就行了
    private int mMarginBgColor = Color.TRANSPARENT;
    private int mOrientation;

    private final Paint mPaint = new Paint();
    private final Paint mMarginBgPaint = new Paint();

    public SimpleItemDecoration(int size, int color) {
        this(VERTICAL, 0, 0, size, color, false);
    }

    public SimpleItemDecoration(int orientation, int size, int color) {
        this(orientation, 0, 0, size, color, false);
    }

    public SimpleItemDecoration(int orientation, int startMargin, int endMargin, int size, @ColorInt int color) {
        this(orientation, startMargin, endMargin, size, color, false);
    }

    public SimpleItemDecoration(int orientation, int startMargin, int endMargin, int size, @ColorInt int color, boolean isNeedLast) {
        setOrientation(orientation);
        this.mStartMargin = startMargin;
        this.mEndMargin = endMargin;
        this.mSize = size;
        this.mColor = color;
        mPaint.setColor(mColor);
        this.mIsNeedLast = isNeedLast;
        mMarginBgPaint.setColor(mMarginBgColor);
    }


    public void setOrientation(int orientation) {
        if (orientation != HORIZONTAL && orientation != VERTICAL) {
            throw new IllegalArgumentException(
                    "Invalid orientation. It should be either HORIZONTAL or VERTICAL");
        }
        mOrientation = orientation;
    }

    public void setMarginBgColor(@ColorInt int marginBgColor) {
        this.mMarginBgColor = marginBgColor;
        mMarginBgPaint.setColor(mMarginBgColor);
    }

    public void setStartMargin(int startMargin) {
        this.mStartMargin = startMargin;
    }

    public void setEndMargin(int endMargin) {
        this.mEndMargin = endMargin;
    }

    public void setSize(int size) {
        this.mSize = size;
    }

    public void setColor(int color) {
        this.mColor = color;
        this.mPaint.setColor(mColor);
    }

    public void setIsNeedLast(boolean isNeedLast) {
        this.mIsNeedLast = isNeedLast;
    }


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

        drawVertical(c, parent);
        if (parent.getLayoutManager() == null) {
            return;
        }
        if (mOrientation == VERTICAL) {
            drawVertical(c, parent);
        } else {
            drawHorizontal(c, parent);
        }
    }

    private void drawVertical(Canvas canvas, RecyclerView parent) {
        canvas.save();
        final int left;
        final int right;
        if (parent.getClipToPadding()) {
            left = parent.getPaddingLeft();
            right = parent.getWidth() - parent.getPaddingRight();
            canvas.clipRect(left, parent.getPaddingTop(), right,
                    parent.getHeight() - parent.getPaddingBottom());
        } else {
            left = 0;
            right = parent.getWidth();
        }

        int childCount = parent.getChildCount();
        if (!mIsNeedLast) {
            childCount--;
        }
        for (int i = 0; i < childCount; i++) {
            final View child = parent.getChildAt(i);
            final int top = child.getBottom();
            final int bottom = child.getBottom() + mSize;
            canvas.drawRect(left + mStartMargin, top, right - mEndMargin, bottom, mPaint);

            //修正左边margin的颜色
            if (mStartMargin > 0) {
                canvas.drawRect(left, top, left + mStartMargin, bottom, mMarginBgPaint);
            }

            //修正右边margin的颜色
            if (mEndMargin > 0) {
                canvas.drawRect(right - mEndMargin, top, right, bottom, mMarginBgPaint);
            }
        }
        canvas.restore();
    }

    private void drawHorizontal(Canvas canvas, RecyclerView parent) {
        canvas.save();
        final int top;
        final int bottom;
        if (parent.getClipToPadding()) {
            top = parent.getPaddingTop();
            bottom = parent.getHeight() - parent.getPaddingBottom();
            canvas.clipRect(parent.getPaddingLeft(), top,
                    parent.getWidth() - parent.getPaddingRight(), bottom);
        } else {
            top = 0;
            bottom = parent.getHeight();
        }

        int childCount = parent.getChildCount();
        if (!mIsNeedLast) {
            childCount--;
        }
        for (int i = 0; i < childCount; i++) {
            final View child = parent.getChildAt(i);
            final int left = child.getRight();
            final int right = left + mSize;
            canvas.drawRect(left, top + mStartMargin, right, bottom - mEndMargin, mPaint);

            //修正上边边margin的颜色
            if (mStartMargin > 0) {
                canvas.drawRect(left, top, right, top + mStartMargin, mMarginBgPaint);
            }

            //修正下边margin的颜色
            if (mEndMargin > 0) {
                canvas.drawRect(left, bottom - mEndMargin, right, bottom, mMarginBgPaint);
            }

        }
        canvas.restore();
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent,
                               RecyclerView.State state) {
        if (mOrientation == VERTICAL) {
            outRect.set(0, 0, 0, mSize);
        } else {
            outRect.set(0, 0, mSize, 0);
        }

    }
}

如果分割线两端要圆角啥的 可以直接修改画布 绘制的形状就行

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值