给RecyclerView添加分割线的步骤
【吐槽】RecyclerView没有提供分割线的方法,想要加个线还要自己画,点击事件的监听都要自己实现,不过真的好用。
给RecyclerView添加分割线的步骤
1, 新建类继承于RecyclerView.ItemDecoration,此为是抽象类:
public static abstract class ItemDecoration {
public void onDraw(Canvas c, RecyclerView parent, State state) {
onDraw(c, parent);
}
public void onDrawOver(Canvas c, RecyclerView parent, State state) {
onDrawOver(c, parent);
}
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, State state) {
getItemOffsets(outRect, ((LayoutParams) view.getLayoutParams()).getViewLayoutPosition(),
parent);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
“onDraw和onDrawOver,显然,这两个方法是用于绘制的,那么绘制分割线的逻辑可以放在这里面,它们二者的具体区别是:onDraw是在item view绘制之前调用,而onDrawOver是在item view绘制之后调用,因此我们一般选择重写其中一个方法即可。getItemOffsets,这个方法是告诉RecyclerView在绘制完一个item view的时候,应该留下多少空位,以便于绘制分割线。”
好像逻辑也不难,但是真的好麻烦,比如我要给我的瀑布流布局加一条系统自带的分割线,系统自带的就行,那我要新建一个实现类:
public class DividerItemDecoration extends RecyclerView.ItemDecoration {
//使用系统自带的listDivider
private static final int[] ATTRS = new int[]{
android.R.attr.listDivider
};
private Drawable mDivider;
public DividerItemDecoration(Context context) {
super();
// 从TypedArray中得到一个Drawable对象
final TypedArray typedArray = context.obtainStyledAttributes(ATTRS);
mDivider = typedArray.getDrawable(0);
typedArray.recycle();
}
@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
super.onDraw(c, parent, state);
drawDivider(c, parent);
}
/**
* 遍历childView,为每一个childView描绘divider
* @param c 画布对象
* @param parent 父控件即RecyclerView
*/
private void drawDivider(Canvas c, RecyclerView parent) {
//获取分割线的上边距,即RecyclerView的padding值
final int top = parent.getPaddingTop();
//分割线下边距
final int bottom = parent.getHeight() - parent.getPaddingBottom();
final int childCount = parent.getChildCount();
//遍历所有item view,为它们的右边方绘制分割线,就是计算出上下左右四个值画一个矩形
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 + mDivider.getIntrinsicWidth();
//画右边的divider
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
//画完右边画底边
mDivider.setBounds(child.getLeft() + child.getPaddingLeft(), child.getBottom() + params.bottomMargin,
child.getRight() - child.getPaddingRight(), child.getBottom() + mDivider.getIntrinsicHeight());
mDivider.draw(c);
}
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
outRect.set(0, 0, mDivider.getIntrinsicHeight(), mDivider.getIntrinsicHeight());
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
2, 为RecyclerView添加写好的ItemDecoration即可
mRecyclerView.addItemDecoration(new DividerItemDecoration(this));
- 1
- 1
3, 没有3。
其实从 RecyclerView.ItemDecoration 的名字就看得出来,它是RecyclerView的item的装饰品,也就是说,除了画分割线,想画什么都是可以