public class CustomDividerItemDecoration extends DividerItemDecoration {
private int showDividerPosition; // 要显示下划线的item位置
private int dividerColor; // 下划线的颜色
public CustomDividerItemDecoration(Context context, int orientation, int showDividerPosition, int dividerColor) {
super(context, orientation);
this.showDividerPosition = showDividerPosition;
this.dividerColor = dividerColor;
}
@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
// super.onDraw(c, parent, state);
// 获取RecyclerView的布局管理器
RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
if (layoutManager instanceof LinearLayoutManager) {
LinearLayoutManager linearLayoutManager = (LinearLayoutManager) layoutManager;
// 获取RecyclerView的子view数量
int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
View child = parent.getChildAt(i);
int position = parent.getChildAdapterPosition(child);
// 判断是否是要显示下划线的item位置
if (position == showDividerPosition) {
// 绘制下划线
drawDivider(c, parent, child);
}
}
}
}
private void drawDivider(Canvas c, RecyclerView parent, View child) {
// 获取下划线的左边界和右边界
int left = parent.getPaddingLeft() + 10;
int right = parent.getWidth() - parent.getPaddingRight() - 10;
int top = child.getBottom() + 5;
int bottom = top + 2;
// 设置下划线的颜色
Paint paint = new Paint();
paint.setColor(dividerColor);
// 绘制下划线
c.drawRect(left, top, right, bottom, paint);
}
}
05-06
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交