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); } } } }
recyclerview的分割线设置
最新推荐文章于 2022-04-01 09:35:33 发布
本文介绍了一个自定义的RecyclerView ItemDecoration实现,该实现用于在每个列表项底部绘制一条红线,通过调整strokeWidth和kongbai变量可以改变线的宽度和空白区域大小。
摘要由CSDN通过智能技术生成