首先我们要了解到的是 RecycleView添加的分割线有两种方式,
而我们的类也就需要继承这个类,实现这两个方法就好了,
1.简单暴力的 ,直接在item的布局中添加分割线,
2.通过recycleView的内部方法addItemDecoration(class) ,这个class 集成下recyclerVIew.ItemDecoration这个类就可以了;
recyclerVIew.ItemDecoration 那这个类具体有啥方法呢,我们来看下源码中两个主要的方法 画分割线的OnDraw()以及获取分割线宽度的getItemOffsets()方法。
public static abstract class ItemDecoration {
/**
* Draw any appropriate decorations into the Canvas supplied to the RecyclerView.
* Any content drawn by this method will be drawn before the item views are drawn,
* and will thus appear underneath the views.
*
* @param c Canvas to draw into
* @param parent RecyclerView this ItemDecoration is drawing into
* @param state The current state of RecyclerView
*/
public void onDraw(Canvas c, RecyclerView parent, State state) {
onDraw(c, parent);
}
/**
* @deprecated
* Use {@link #getItemOffsets(Rect, View, RecyclerView, State)
*获取分割线的宽度
*/
@Deprecated
public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) {
outRect.set(0, 0, 0, 0);
}
}
首先实现个小目标,画一个简单的listview的分割线 这里我们首先要理解这个分割线就是一个距形,然后画一个距形也就是 canvas.drawRect(left,top,right,bottom,mPaint) ,所以我们的主要任务就是找到这个left. top. right.bottom 的值就好了,然后联系图看下这几个字段里面的距形就是我们要画的
理解了这几个字段后,我们就要去给这几个字段赋值了, 我们把红色框看成recyclerview
//横行的话,距离左右的距离是不变的, 上下的距离由每一个item的距离开始画的
final int left = recyclerview.getPaddingLeft();
final int right = recyclerview.getMeasuredWidth()-recyclerview.getPaddingRight();
//遍历每一个item
for (int i=0;i<recyclerview.getChildCount(); i++){
View child = parent.getChildAt(i);
RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams();
int top = child.getBottom()+layoutParams.bottomMargin ;
int bottom = top+divigh_height;//顶部加高度就是底部了
c.drawRect(left,top,right,bottom,mPaint);
}
ok,就是这样了,就能画出一个listView类型的分割线了
同理的话 gridView的就是
final int top = parent.getPaddingTop();
final int bottom = parent.getMeasuredHeight()-parent.getPaddingBottom();
for(int i=0;i<parent.getChildCount();i++){
View child = parent.getChildAt(i);
RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams();
int left = child.getRight()+layoutParams.rightMargin;
int right =left+divigh_height ;
c.drawRect(left,top,right,bottom,mPaint);
}
ok,最后贴下这个类的代码吧,没有仔细的去封装了
public class RyDecoration extends RecyclerView.ItemDecoration {
private Paint mPaint;
private int oritation = LinearLayout.HORIZONTAL;
private final int LINE_COLOR = 0xffff00ff;
private int divigh_height = 10;
public RyDecoration(Context context, int oritation) {
this.oritation = oritation;
if (oritation != LinearLayout.HORIZONTAL || oritation != LinearLayout.VERTICAL) {
new IllegalAccessError("输入的参数不对");
}
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setColor(LINE_COLOR);
mPaint.setStyle(Paint.Style.FILL);
}
@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
super.onDraw(c, parent, state);
if (oritation == LinearLayout.HORIZONTAL) {
//横行的话,距离左右的距离是不变的, 上下的距离由每一个item的距离开始画的
final int left = parent.getPaddingLeft();
final int right = parent.getMeasuredWidth() - parent.getPaddingRight();
for (int i = 0; i < parent.getChildCount(); i++) {
View child = parent.getChildAt(i);
RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams();
int top = child.getBottom() + layoutParams.bottomMargin;
int bottom = top + divigh_height;
c.drawRect(left, top, right, bottom, mPaint);
}
} else {
final int top = parent.getPaddingTop();
final int bottom = parent.getMeasuredHeight()-parent.getPaddingBottom();
for(int i=0;i<parent.getChildCount();i++){
View child = parent.getChildAt(i);
RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams();
int left = child.getRight()+layoutParams.rightMargin;
int right =left+divigh_height ;
c.drawRect(left,top,right,bottom,mPaint);
}
}
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
outRect.set(0, 0, 0, 0);
super.getItemOffsets(outRect, view, parent, state);
}
}
使用,listView形式用
mRecyclerView.addItemDecoration(new RyDecoration(this,LinearLayout.HORIZONTAL));
gridview 形式用
mRecyclerView.addItemDecoration(new RyDecoration(this,LinearLayout.VERTICAL));