RecyclerViw分割线
public class SectionDecoration extends RecyclerView.ItemDecoration {
private int mHeight = 5;
private Paint mPaint;
public SectionDecoration() {
mPaint = new Paint();
mPaint.setColor(Color.BLACK);
mPaint.setStyle(Paint.Style.FILL);
}
@Override
public void onDraw(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
super.onDraw(c, parent, state);
int left = parent.getLeft();// 获得recyclerView left值
int right = parent.getRight();// 获得recyclerView left值
int childCount = parent.getChildCount();// 获取item数量
for (int i = 0; i < childCount; i++) {// 遍历
View childView = parent.getChildAt(i);// 获取子view
int bottom = childView.getTop();// 获取每个item距离recyclerView顶部距离,如果不设置padding那么第一次为0
Log.d("SectionDecoration", "bottom:" + bottom);
int top = bottom - mHeight;// 计算出分割线要绘制的位置
if (top > 0) {// 如果计算出的距离为负数则不进行绘制,该情况只有在recyclerView第一条item加载时并且没有设置padding时出现
c.drawRect(left, top, right, bottom, mPaint);// 绘制分割线
}
}
}
}