原文件经过加了个drawDividerIndex和relativeLayoutId后是这样的:
public class CustomDivider extends RecyclerView.ItemDecoration {
private Paint mPaint;
private int mDividerHeight = 1;//分割线高度,默认为1px
private int marginLeft;
private int marginRight;
private int drawDividerIndex = -1;//第几项以后开始画线
private static int relativeLayoutId = R.id.rlay_index;
/**
* 默认分割线:高度为1px,颜色为灰色
*
* @param context
*/
public CustomDivider(Context context) {
this(context, 0);
}
public CustomDivider(Context context, int marginLeftDp) {
this(context, marginLeftDp, 0);
}
public CustomDivider(Context context, int marginLeftDp, String dividerIndex, int relativeLayoutId) {
this(context, marginLeftDp, 0);
this.drawDividerIndex = Integer.parseInt(dividerIndex);
this.relativeLayoutId = relativeLayoutId;
}
//最后一项传string是为了区别于下面的构造方法
public CustomDivider(Context context, int marginLeftDp, String dividerIndex) {
this(context, marginLeftDp, 0);
this.drawDividerIndex = Integer.parseInt(dividerIndex);
}
public CustomDivider(Context context, int marginLeftDp, int marginRightDp) {
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setColor(context.getResources().getColor(R.color.app_mid_gray));
mPaint.setStyle(Paint.Style.FILL);
this.marginLeft = DimenUtils.dp2px(context, marginLeftDp);
this.marginRight = DimenUtils.dp2px(context, marginRightDp);
}
//获取分割线尺寸
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
View classifyView = view.findViewById(relativeLayoutId);
if (classifyView != null) {
if (classifyView.getVisibility() == View.GONE) {
outRect.set(0, mDividerHeight, 0, 0);
}
int childCount = parent.getChildCount();
int position = parent.indexOfChild(view);
boolean isLastView = (position - 1 == childCount);
if (isLastView) {
outRect.bottom = mDividerHeight;
}
} else {
super.getItemOffsets(outRect, view, parent, state);
}
}
@Override
public void onDraw(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
int left = parent.getPaddingLeft();
int right = parent.getMeasuredWidth() - parent.getPaddingRight();
int childSize = parent.getChildCount();
View lastView = parent.getChildAt(childSize - 1);
if (lastView != null) {
RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) lastView.getLayoutParams();
int top = lastView.getBottom() + layoutParams.bottomMargin;
int bottom = top + mDividerHeight;
canvas.drawRect(left, top, right, bottom, mPaint);
}
}
//绘制分割线
@Override
public void onDrawOver(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
super.onDraw(canvas, parent, state);
int left = parent.getPaddingLeft() + marginLeft;
int right = parent.getMeasuredWidth() - parent.getPaddingRight() - marginRight;
int childSize = parent.getChildCount();
int rows = childSize;
for (int i = 0; i < rows; i++) {
if (i > this.drawDividerIndex) {
View child = parent.getChildAt(i);
View classifyView = child.findViewById(relativeLayoutId);
if (classifyView != null && classifyView.getVisibility() == View.GONE) {
RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams();
int top = child.getTop() + layoutParams.topMargin;
int bottom = top + mDividerHeight;
canvas.drawRect(left, top, right, bottom, mPaint);
}
}
}
}
}
默认找寻的是 R.id.rlay_index这个RelativeLayout,可以在构造方法传入指定需要找寻的RelativeLayout
因为有的recyclerView会设置headView,如果顶部划分割线的话则会把headView的顶部也画线,所以加一个drawDividerIndex,从指定position并且classifyView(分割view)隐藏的item开始画线。
实际测试getItemOffsets和onDraw方法中对lastView的bottom线实际页面上不显示,里面处理的逻辑代码有问题,不符合项目最后一项底部分割线需求,方法写的实在多余,可以直接在onDrawOver方法中对lastview底部贴最下方画一条分割线即可。
因现在项目运行正常,所有用到此分割线的recyclerView最后一项底部都没画线,所以先把底部画线方法去掉。
把getItemOffsets方法也去掉,里面设置顶部padding是1的方法实在没啥用,直接在onDrawOver里顶部画条线即可。
public class CustomDivider extends RecyclerView.ItemDecoration {
private Paint mPaint;
private int mDividerHeight = 1;//分割线高度,默认为1px
private int marginLeft;
private int marginRight;
private int drawDividerIndex = -1;//第几项以后开始画线
private static int relativeLayoutId = R.id.rlay_index;
/**
* 默认分割线:高度为1px,颜色为灰色
*
* @param context
*/
public CustomDivider(Context context) {
this(context, 0);
}
public CustomDivider(Context context, int marginLeftDp) {
this(context, marginLeftDp, 0);
}
public CustomDivider(Context context, int marginLeftDp, String dividerIndex, int relativeLayoutId) {
this(context, marginLeftDp, 0);
this.drawDividerIndex = Integer.parseInt(dividerIndex);
this.relativeLayoutId = relativeLayoutId;
}
//最后一项传string是为了区别于下面的构造方法
public CustomDivider(Context context, int marginLeftDp, String dividerIndex) {
this(context, marginLeftDp, 0);
this.drawDividerIndex = Integer.parseInt(dividerIndex);
}
public CustomDivider(Context context, int marginLeftDp, int marginRightDp) {
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setColor(context.getResources().getColor(R.color.app_mid_gray));
mPaint.setStyle(Paint.Style.FILL);
this.marginLeft = DimenUtils.dp2px(context, marginLeftDp);
this.marginRight = DimenUtils.dp2px(context, marginRightDp);
}
//在最上层 绘制分割线
@Override
public void onDrawOver(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
super.onDraw(canvas, parent, state);
int left = parent.getPaddingLeft() + marginLeft;
int right = parent.getMeasuredWidth() - parent.getPaddingRight() - marginRight;
int childSize = parent.getChildCount();
int rows = childSize;
for (int i = 0; i < rows; i++) {
if (i > this.drawDividerIndex) {
View child = parent.getChildAt(i);
View classifyView = child.findViewById(relativeLayoutId);
if (classifyView != null && classifyView.getVisibility() == View.GONE) {
RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams();
int top = child.getTop() + layoutParams.topMargin;
int bottom = top + mDividerHeight;
canvas.drawRect(left, top, right, bottom, mPaint);
}
}
}
}
}
代码多清爽。