修改遗留代码的分割线RecyclerView.ItemDecoration问题

本文介绍了一种针对RecyclerView的自定义分割线实现方案,通过调整构造方法参数,灵活控制分割线的绘制位置和样式。优化后的代码更简洁,提高了绘制效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原文件经过加了个drawDividerIndex和relativeLayoutId后是这样的:

public class CustomDivider extends RecyclerView.ItemDecoration {

    private Paint mPaint;
    private int mDividerHeight = 1;//分割线高度,默认为1px
    private int marginLeft;
    private int marginRight;
    private int drawDividerIndex = -1;//第几项以后开始画线
    private static int relativeLayoutId = R.id.rlay_index;

    /**
     * 默认分割线:高度为1px,颜色为灰色
     *
     * @param context
     */
    public CustomDivider(Context context) {
        this(context, 0);

    }

    public CustomDivider(Context context, int marginLeftDp) {
        this(context, marginLeftDp, 0);
    }

    public CustomDivider(Context context, int marginLeftDp, String dividerIndex, int relativeLayoutId) {
        this(context, marginLeftDp, 0);
        this.drawDividerIndex = Integer.parseInt(dividerIndex);
        this.relativeLayoutId = relativeLayoutId;
    }

    //最后一项传string是为了区别于下面的构造方法
    public CustomDivider(Context context, int marginLeftDp, String dividerIndex) {
        this(context, marginLeftDp, 0);
        this.drawDividerIndex = Integer.parseInt(dividerIndex);
    }

    public CustomDivider(Context context, int marginLeftDp, int marginRightDp) {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setColor(context.getResources().getColor(R.color.app_mid_gray));
        mPaint.setStyle(Paint.Style.FILL);

        this.marginLeft = DimenUtils.dp2px(context, marginLeftDp);
        this.marginRight = DimenUtils.dp2px(context, marginRightDp);
    }

    //获取分割线尺寸
    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        View classifyView = view.findViewById(relativeLayoutId);
        if (classifyView != null) {
            if (classifyView.getVisibility() == View.GONE) {
                outRect.set(0, mDividerHeight, 0, 0);
            }
            int childCount = parent.getChildCount();
            int position = parent.indexOfChild(view);
            boolean isLastView = (position - 1 == childCount);
            if (isLastView) {
                outRect.bottom = mDividerHeight;
            }
        } else {
            super.getItemOffsets(outRect, view, parent, state);
        }
    }

    @Override
    public void onDraw(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
        int left = parent.getPaddingLeft();
        int right = parent.getMeasuredWidth() - parent.getPaddingRight();
        int childSize = parent.getChildCount();
        View lastView = parent.getChildAt(childSize - 1);
        if (lastView != null) {
            RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) lastView.getLayoutParams();
            int top = lastView.getBottom() + layoutParams.bottomMargin;
            int bottom = top + mDividerHeight;
            canvas.drawRect(left, top, right, bottom, mPaint);
        }
    }

    //绘制分割线
    @Override
    public void onDrawOver(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
        super.onDraw(canvas, parent, state);
        int left = parent.getPaddingLeft() + marginLeft;
        int right = parent.getMeasuredWidth() - parent.getPaddingRight() - marginRight;
        int childSize = parent.getChildCount();
        int rows = childSize;
        for (int i = 0; i < rows; i++) {
            if (i > this.drawDividerIndex) {
                View child = parent.getChildAt(i);
                View classifyView = child.findViewById(relativeLayoutId);
                if (classifyView != null && classifyView.getVisibility() == View.GONE) {
                    RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams();
                    int top = child.getTop() + layoutParams.topMargin;
                    int bottom = top + mDividerHeight;
                    canvas.drawRect(left, top, right, bottom, mPaint);
                }
            }
        }
    }
}

默认找寻的是 R.id.rlay_index这个RelativeLayout,可以在构造方法传入指定需要找寻的RelativeLayout

因为有的recyclerView会设置headView,如果顶部划分割线的话则会把headView的顶部也画线,所以加一个drawDividerIndex,从指定position并且classifyView(分割view)隐藏的item开始画线。

实际测试getItemOffsets和onDraw方法中对lastView的bottom线实际页面上不显示,里面处理的逻辑代码有问题,不符合项目最后一项底部分割线需求,方法写的实在多余,可以直接在onDrawOver方法中对lastview底部贴最下方画一条分割线即可。

因现在项目运行正常,所有用到此分割线的recyclerView最后一项底部都没画线,所以先把底部画线方法去掉。

把getItemOffsets方法也去掉,里面设置顶部padding是1的方法实在没啥用,直接在onDrawOver里顶部画条线即可。

public class CustomDivider extends RecyclerView.ItemDecoration {

    private Paint mPaint;
    private int mDividerHeight = 1;//分割线高度,默认为1px
    private int marginLeft;
    private int marginRight;
    private int drawDividerIndex = -1;//第几项以后开始画线
    private static int relativeLayoutId = R.id.rlay_index;

    /**
     * 默认分割线:高度为1px,颜色为灰色
     *
     * @param context
     */
    public CustomDivider(Context context) {
        this(context, 0);

    }

    public CustomDivider(Context context, int marginLeftDp) {
        this(context, marginLeftDp, 0);
    }

    public CustomDivider(Context context, int marginLeftDp, String dividerIndex, int relativeLayoutId) {
        this(context, marginLeftDp, 0);
        this.drawDividerIndex = Integer.parseInt(dividerIndex);
        this.relativeLayoutId = relativeLayoutId;
    }

    //最后一项传string是为了区别于下面的构造方法
    public CustomDivider(Context context, int marginLeftDp, String dividerIndex) {
        this(context, marginLeftDp, 0);
        this.drawDividerIndex = Integer.parseInt(dividerIndex);
    }

    public CustomDivider(Context context, int marginLeftDp, int marginRightDp) {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setColor(context.getResources().getColor(R.color.app_mid_gray));
        mPaint.setStyle(Paint.Style.FILL);

        this.marginLeft = DimenUtils.dp2px(context, marginLeftDp);
        this.marginRight = DimenUtils.dp2px(context, marginRightDp);
    }

    //在最上层 绘制分割线
    @Override
    public void onDrawOver(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
        super.onDraw(canvas, parent, state);
        int left = parent.getPaddingLeft() + marginLeft;
        int right = parent.getMeasuredWidth() - parent.getPaddingRight() - marginRight;
        int childSize = parent.getChildCount();
        int rows = childSize;
        for (int i = 0; i < rows; i++) {
            if (i > this.drawDividerIndex) {
                View child = parent.getChildAt(i);
                View classifyView = child.findViewById(relativeLayoutId);
                if (classifyView != null && classifyView.getVisibility() == View.GONE) {
                    RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams();
                    int top = child.getTop() + layoutParams.topMargin;
                    int bottom = top + mDividerHeight;
                    canvas.drawRect(left, top, right, bottom, mPaint);
                }
            }
        }
    }
}

代码多清爽。

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值