最近有一个需求需要做分割线,网上有一些大致的做法,但感觉都不是那么通俗易懂,然后自己想了想,干脆自己弄吧,后面发现了一个比较好的,非常简洁,分享给大家
效果
代码
class GridSpanDecoration extends RecyclerView.ItemDecoration {
private Paint mPaint;
GridSpanDecoration(int color) {
mPaint = new Paint();
mPaint.setColor(color);
mPaint.setStyle(Paint.Style.STROKE);
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
}
@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
drawVertical(c, parent);
}
private void drawVertical(Canvas c, RecyclerView parent) {
final int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
final View child = parent.getChildAt(i);
c.drawRect(child.getLeft(), child.getTop(), child.getRight(), child.getBottom(), mPaint);
}
}
}
原理就是给每个子view画一个边框就行了!是不是很简单