概述:
当我们准备为RecyclerView添加分割线时,你会发现RecyclerView并没有支持divider这样的属性。想想办法,你可以给Item的布局去设置margin,当然了这种方式不够优雅,我们可以自由的去定制它。
绘制横向或纵向列表分割线:
RecyclerView.ItemDecoration是一个抽象类,就是用来装饰RecyclerView的子item的,通过名字就可以知道,功能并不仅仅是添加间距绘制分割线,是用来装饰item的。
这个类包含三个方法 :
1、getItemOffsets(Rect outRect, View view, RecyclerView parent, State state)//设置四边边距
2、onDraw(Canvas c, RecyclerView parent, State state)//绘制装饰
3、onDrawOver(Canvas c, RecyclerView parent, State state)//绘制蒙层
1、getItemOffsets(Rect outRect, View view, RecyclerView parent, State state)方法:
该方法中有4个参数:
1、Rect outRect: outRect 设置的4个方向的值,将被计算进所有 decoration 的尺寸中,而这个尺寸,被用来计算 RecyclerView 每个 item view 的padding间距大小。
2、View view: childView,就是item,可以理解为item的根View,并不是item中的控件
3、RecyclerView parent:就是RecyclerView自身
4、RecyclerView.State state : RecyclerView的状态
代码实现:
//获取分割线尺寸
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
outRect.set(0, 0, 0, mDividerHeight);
}
2、onDraw(Canvas c, RecyclerView parent, RecyclerView.State state)方法:
onDraw用于绘制divider,绘制到每一个item的下一层,也就是说item会盖在divider所在层的上面
onDraw()方法可以为divier设置绘制范围,并且绘制范围可以超出在 getItemOffsets中设置的范围,但由于是在item下面一层进行绘制,会存在overdraw
代码实现:
//绘制分割线
@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
super.onDraw(c, parent, state);
if (mOrientation == LinearLayoutManager.VERTICAL) {
drawVertical(c, parent);
} else {
drawHorizontal(c, parent);
}
}
//绘制横向 item 分割线
private void drawHorizontal(Canvas canvas, RecyclerView parent) {
final int left = parent.getPaddingLeft();
final int right = parent.getMeasuredWidth() - parent.getPaddingRight();
final int childSize = parent.getChildCount();
for (int i = 0; i < childSize; i++) {
final View child = parent.getChildAt(i);
RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams();
final int top = child.getBottom() + layoutParams.bottomMargin;
final int bottom = top + mDividerHeight;
if (mDivider != null) {
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(canvas);
}
if (mPaint != null) {
canvas.drawRect(left, top, right, bottom, mPaint);
}
}
}
//绘制纵向 item 分割线
private void drawVertical(Canvas canvas, RecyclerView parent) {
final int top = parent.getPaddingTop();
final int bottom = parent.getMeasuredHeight() - parent.getPaddingBottom();
final int childSize = parent.getChildCount();
for (int i = 0; i < childSize; i++) {
final View child = parent.getChildAt(i);
RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams();
final int left = child.getRight() + layoutParams.rightMargin;
final int right = left + mDividerHeight;
if (mDivider != null) {
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(canvas);
}
if (mPaint != null) {
canvas.drawRect(left, top, right, bottom, mPaint);
}
}
}
3、onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state)方法:
这个方法是在item的onDraw()方法之后进行回调,也就绘制在了最上层
由于 onDrawOver 是绘制在最上层的,所以它的绘制位置并不受限制,
只要手指在RecyclerView上进行滑动,onDrawOver()方法就会被回调。但onDrawOver()每回调一次,会将上次的绘制清除,只有最后一次的绘制会被保留。也就是说绘制的蒙层在屏幕只会有一个
所以利用 onDrawOver 可以做很多事情,例如为 RecyclerView 整体顶部绘制一个蒙层,或者为特定的 item view 绘制蒙层。这里就不单独进行测试了。
绘制九宫格布局样式分割线:
对于分割线,前面的DividerItemDecoration就不适用了,主要是因为它在绘制的时候,比如水平线,因为每个Item一行,这样是没问题的。而GridLayoutManager时,一行有多个childItem,这样就多次绘制了,并且GridLayoutManager时,Item如果为最后一列(则右边无间隔线)或者为最后一行(底部无分割线)。
主要在getItemOffsets方法中,去判断如果是最后一行,则不需要绘制底部;如果是最后一列,则不需要绘制右边,整个判断也考虑到了StaggeredGridLayoutManager的横向和纵向,所以稍稍有些复杂
1、getItemOffsets(Rect outRect, View view, RecyclerView parent, State state)方法:
@Override
@Deprecated
public void getItemOffsets(Rect outRect, int itemPosition,
RecyclerView parent) {
// 四个方向的偏移值
int right = mDivider.getIntrinsicWidth();
int bottom = mDivider.getIntrinsicHeight();
if(isLastColum(itemPosition,parent)){//是否是最后一列
// outRect.set(0, 0, 0, bottom);
right = 0;
}
if(isLastRow(itemPosition,parent)){//是最后一行
// outRect.set(0, 0, right, 0);
bottom = 0;
}
outRect.set(0, 0, right, bottom);
}
/**
* 是否是最后一行
* @param itemPosition
* @param parent
* @return
*/
private boolean isLastRow(int itemPosition, RecyclerView parent) {
int spanCount = getSpanCount(parent);
LayoutManager layoutManager = parent.getLayoutManager();
//有多少列
if(layoutManager instanceof GridLayoutManager){
int childCount = parent.getAdapter().getItemCount();
int lastRowCount = childCount%spanCount;
//最后一行的数量小于spanCount
if(lastRowCount==0||lastRowCount
return true;
}
}
return false;
}
/**
* 判断是否是最后一列
* @param itemPosition
* @param parent
* @return
*/
private boolean isLastColum(int itemPosition, RecyclerView parent) {
LayoutManager layoutManager = parent.getLayoutManager();
//有多少列
if(layoutManager instanceof GridLayoutManager){
int spanCount = getSpanCount(parent);
if((itemPosition+1)%spanCount==0){
return true;
}
}
return false;
}
2、onDraw(Canvas c, RecyclerView parent, RecyclerView.State state)方法:
@Override
public void onDraw(Canvas c, RecyclerView parent, State state) {
drawVertical(c,parent);
drawHorizontal(c,parent);
}
private void drawHorizontal(Canvas c, RecyclerView parent) {
// 绘制水平间隔线
int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
View child = parent.getChildAt(i);
RecyclerView.LayoutParams params = (LayoutParams) child.getLayoutParams();
int left = child.getLeft() - params.leftMargin;
int right = child.getRight()+ params.rightMargin;
int top = child.getBottom() + params.bottomMargin;
int bottom = top + mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}
private void drawVertical(Canvas c, RecyclerView parent) {
//绘制垂直间隔线(垂直的矩形)
int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
View child = parent.getChildAt(i);
RecyclerView.LayoutParams params = (LayoutParams) child.getLayoutParams();
int left = child.getRight() + params.rightMargin;
int right = left + mDivider.getIntrinsicWidth();
int top = child.getTop() - params.topMargin;
int bottom = child.getBottom() + params.bottomMargin;
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}
源码demo链接:
https://github.com/heiyl/recyleview
csdn地址:https://blog.csdn.net/hylxnq/article/details/80292537