首页 热点资讯 义务教育 高等教育 出国留学 考研考公
您的当前位置:首页正文

ReyclerView 添加分割线

2024-12-17 来源:花图问答

方式一 :
添加系统默认分割线

 mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));

效果:


image.png

更改为自想要的颜色,修改styles的AppTheme,添加内容

   <item name="android:listDivider">@color/colorAccent</item>

其中的内容可以是自定义的drawable也可使颜色。
效果:


image.png

如果需要修改高度可以通过drawable中进行设置

要实现GridView 表格效果的话

        mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.HORIZONTAL));
        mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));

效果:


image.png

如需要代码中设置颜色或者drawable

        DividerItemDecoration divider = new DividerItemDecoration(this,DividerItemDecoration.VERTICAL);
        divider.setDrawable(ContextCompat.getDrawable(this,R.drawable.custom_divider));
        mRecyclerView.addItemDecoration(divider);

方式二
自定义ItemDecoration
代码(仿照百度各种结果修改的)

public class ItemDivider extends RecyclerView.ItemDecoration {

    private int dividerWith = 1;
    private Paint paint;
    private RecyclerView.LayoutManager layoutManager;
    private Drawable dividerDrawable;

    // 构造方法,可以在这里做一些初始化,比如指定画笔颜色什么的
    public ItemDivider() {
        initPaint();
        paint.setColor(0xffff0000);
    }

    private void initPaint() {
        if (paint == null) {
            paint = new Paint(Paint.ANTI_ALIAS_FLAG);
            paint.setStyle(Paint.Style.FILL);
        }
    }

    public ItemDivider setDividerWith(int dividerWith) {
        this.dividerWith = dividerWith;
        return this;

    }

    public ItemDivider setDividerColor(int color) {
        initPaint();
        paint.setColor(color);
        return this;
    }

    public ItemDivider setDivider(Drawable divider) {
        dividerDrawable = divider;
        return this;
    }


    /**
     * 指定item之间的间距(就是指定分割线的宽度)   回调顺序 1
     *
     * @param outRect Rect to receive the output.
     * @param view    The child view to decorate
     * @param parent  RecyclerView this ItemDecoration is decorating
     * @param state   The current state of RecyclerView.
     */
    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        if (layoutManager == null) {
            layoutManager = parent.getLayoutManager();
        }
        // 适用 LinearLayoutManager 和 GridLayoutManager
        if (layoutManager instanceof LinearLayoutManager) {
            int orientation = ((LinearLayoutManager) layoutManager).getOrientation();
            if (orientation == LinearLayoutManager.VERTICAL) {
                // 水平分割线将绘制在item底部
                outRect.bottom = dividerWith;
            } else if (orientation == LinearLayoutManager.HORIZONTAL) {
                // 垂直分割线将绘制在item右侧
                outRect.right = dividerWith;
            }
            if (layoutManager instanceof GridLayoutManager) {
                GridLayoutManager.LayoutParams lp = (GridLayoutManager.LayoutParams) view.getLayoutParams();
                // 如果是 GridLayoutManager 则需要绘制另一个方向上的分割线
                if (orientation == LinearLayoutManager.VERTICAL && lp != null && lp.getSpanIndex() > 0) {
                    // 如果列表是垂直方向,则最左边的一列略过
                    outRect.left = dividerWith;
                } else if (orientation == LinearLayoutManager.HORIZONTAL && lp != null && lp.getSpanIndex() > 0) {
                    // 如果列表是水平方向,则最上边的一列略过
                    outRect.top = dividerWith;
                }
            }
        }
    }

    /**
     * 在item 绘制之前调用(就是绘制在 item 的底层)  回调顺序 2
     * 一般分割线在这里绘制
     * 看到canvas,对自定义控件有一定了解的话,就能想到为什么说给RecyclerView设置分割线更灵活了
     *
     * @param c      Canvas to draw into
     * @param parent RecyclerView this ItemDecoration is drawing into
     * @param state  The current state of RecyclerView
     */
    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDraw(c, parent, state);
        // 这个值是为了补偿横竖方向上分割线交叉处间隙
        int offSet = (int) Math.ceil(dividerWith * 1f / 2);
        for (int i = 0; i < parent.getChildCount(); i++) {
            View child = parent.getChildAt(i);
            RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
            int left1 = child.getRight() + params.rightMargin;
            int right1 = left1 + dividerWith;
            int top1 = child.getTop() - offSet - params.topMargin;
            int bottom1 = child.getBottom() + offSet + params.bottomMargin;
            //绘制分割线(矩形)
            if (dividerDrawable != null) {
                dividerDrawable.setBounds(left1, top1, right1, bottom1);
                dividerDrawable.draw(c);
            } else {
                c.drawRect(left1, top1, right1, bottom1, paint);
            }

            int left2 = child.getLeft() - offSet - params.leftMargin;
            int right2 = child.getRight() + offSet + params.rightMargin;
            int top2 = child.getBottom() + params.bottomMargin;
            int bottom2 = top2 + dividerWith;
            //绘制分割线(矩形)

            if (dividerDrawable != null) {
                dividerDrawable.setBounds(left2, top2, right2, bottom2);
                dividerDrawable.draw(c);
            } else {
                c.drawRect(left2, top2, right2, bottom2, paint);
            }
        }
    }

    /**
     * 在item 绘制之后调用(就是绘制在 item 的上层)  回调顺序 3
     * 也可以在这里绘制分割线,和上面的方法 二选一
     *
     * @param c      Canvas to draw into
     * @param parent RecyclerView this ItemDecoration is drawing into
     * @param state  The current state of RecyclerView
     */
    @Override
    public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDrawOver(c, parent, state);
    }
}

使用方法

mRecyclerView.addItemDecoration(new ItemDivider().setDivider(ContextCompat.getDrawable(this, R.drawable.custom_divider)));

or

 mRecyclerView.addItemDecoration(new ItemDivider().setDividerColor(Color.BLUE));

效果


image.png
显示全文