recyclerview的分割线设置

本文介绍了一个自定义的RecyclerView ItemDecoration实现,该实现用于在每个列表项底部绘制一条红线,通过调整strokeWidth和kongbai变量可以改变线的宽度和空白区域大小。
摘要由CSDN通过智能技术生成
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.v7.widget.RecyclerView;
import android.view.View;

/**
 * Created by dream on 2017/12/27.
 */

public class itemDecoration extends RecyclerView.ItemDecoration {

    private final Paint paint;
    private int bottom;
    int strokeWidth=40;
    int kongbai=20;

    public itemDecoration(){
        paint = new Paint();
        paint.setColor(Color.RED);
        paint.setStrokeWidth(strokeWidth);
    }

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

    @Override
    public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDrawOver(c, parent, state);
        drawLine(c,parent);
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
           outRect.set(0,0 , 0,strokeWidth+kongbai);
    }

    public void drawLine(Canvas c,RecyclerView recyclerView){
        for (int i=0;i<recyclerView.getChildCount();i++){
            View childAt = recyclerView.getChildAt(i);
            if (childAt!=null){
                int left = childAt.getLeft();
                int right = childAt.getRight();
                bottom = childAt.getBottom()+strokeWidth/2;
                c.drawLine(left, bottom+kongbai/2,right, bottom+kongbai/2,paint);
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值