recyclerview的分割线设置

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);
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/mydtudysy/article/details/78918684