RecyclerView怎么修改分割线
2个回答
展开全部
应为 RecyclerView 和 LayoutManager 是分离的,所以没有分割线属性,可以用 RecyclerView.ItemDecoration 进行分割线的绘制。
提供一个简单的装饰
/**
* 分隔线装饰
*
* @author youmingdot
*/
public class DividerLine extends RecyclerView.ItemDecoration {
/**
* 水平方向
*/
public static final int HORIZONTAL = LinearLayoutManager.HORIZONTAL;
/**
* 垂直方向
*/
public static final int VERTICAL = LinearLayoutManager.VERTICAL;
// 画笔
private Paint paint;
// 布局方向
private int orientation;
// 分割线颜色
private int color;
// 分割线尺寸
private int size;
public DividerLine() {
this(VERTICAL);
}
public DividerLine(int orientation) {
this.orientation = orientation;
paint = new Paint();
}
@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
super.onDrawOver(c, parent, state);
if (orientation == VERTICAL) {
drawHorizontal(c, parent);
} else {
drawVertical(c, parent);
}
}
/**
* 设置分割线颜色
*
* @param color 颜色
*/
public void setColor(int color) {
this.color = color;
paint.setColor(color);
}
/**
* 设置分割线尺寸
*
* @param size 尺寸
*/
public void setSize(int size) {
this.size = size;
}
// 绘制垂直分割线
protected void drawVertical(Canvas c, RecyclerView parent) {
final int top = parent.getPaddingTop();
final int bottom = parent.getHeight() - parent.getPaddingBottom();
final int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
final View child = parent.getChildAt(i);
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
final int left = child.getRight() + params.rightMargin;
final int right = left + size;
c.drawRect(left, top, right, bottom, paint);
}
}
// 绘制水平分割线
protected void drawHorizontal(Canvas c, RecyclerView parent) {
final int left = parent.getPaddingLeft();
final int right = parent.getWidth() - parent.getPaddingRight();
final int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
final View child = parent.getChildAt(i);
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
final int top = child.getBottom() + params.bottomMargin;
final int bottom = top + size;
c.drawRect(left, top, right, bottom, paint);
}
}
}
使用方法
DividerLine dividerLine = new DividerLine(DividerLine.VERTICAL);
dividerLine.setSize(1);
dividerLine.setColor(0xFFDDDDDD);
recyclerView.addItemDecoration(dividerLine);
提供一个简单的装饰
/**
* 分隔线装饰
*
* @author youmingdot
*/
public class DividerLine extends RecyclerView.ItemDecoration {
/**
* 水平方向
*/
public static final int HORIZONTAL = LinearLayoutManager.HORIZONTAL;
/**
* 垂直方向
*/
public static final int VERTICAL = LinearLayoutManager.VERTICAL;
// 画笔
private Paint paint;
// 布局方向
private int orientation;
// 分割线颜色
private int color;
// 分割线尺寸
private int size;
public DividerLine() {
this(VERTICAL);
}
public DividerLine(int orientation) {
this.orientation = orientation;
paint = new Paint();
}
@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
super.onDrawOver(c, parent, state);
if (orientation == VERTICAL) {
drawHorizontal(c, parent);
} else {
drawVertical(c, parent);
}
}
/**
* 设置分割线颜色
*
* @param color 颜色
*/
public void setColor(int color) {
this.color = color;
paint.setColor(color);
}
/**
* 设置分割线尺寸
*
* @param size 尺寸
*/
public void setSize(int size) {
this.size = size;
}
// 绘制垂直分割线
protected void drawVertical(Canvas c, RecyclerView parent) {
final int top = parent.getPaddingTop();
final int bottom = parent.getHeight() - parent.getPaddingBottom();
final int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
final View child = parent.getChildAt(i);
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
final int left = child.getRight() + params.rightMargin;
final int right = left + size;
c.drawRect(left, top, right, bottom, paint);
}
}
// 绘制水平分割线
protected void drawHorizontal(Canvas c, RecyclerView parent) {
final int left = parent.getPaddingLeft();
final int right = parent.getWidth() - parent.getPaddingRight();
final int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
final View child = parent.getChildAt(i);
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
final int top = child.getBottom() + params.bottomMargin;
final int bottom = top + size;
c.drawRect(left, top, right, bottom, paint);
}
}
}
使用方法
DividerLine dividerLine = new DividerLine(DividerLine.VERTICAL);
dividerLine.setSize(1);
dividerLine.setColor(0xFFDDDDDD);
recyclerView.addItemDecoration(dividerLine);
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询