SpacesItemDecoration

public classSpacesItemDecorationextendsRecyclerView.ItemDecoration{

private intdividerHeight;

privatePaintpaint;

publicSpacesItemDecoration(Context context) {

dividerHeight= context.getResources().getDimensionPixelSize(R.dimen.margin);

paint=newPaint();

paint.setColor(context.getResources().getColor(R.color.colorAccent));

}

@Override

public voidgetItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {

super.getItemOffsets(outRect, view, parent, state);

/**

* 类似加了一个bottom的padding

*/

outRect.bottom=dividerHeight;

}

@Override

public voidonDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {

super.onDraw(c, parent, state);

intchildCount = parent.getChildCount();

intleft = parent.getPaddingLeft();

intright = parent.getWidth() - parent.getPaddingRight();

for(inti =0; i < childCount -1; i++) {

View view = parent.getChildAt(i);

floattop = view.getBottom();

floatbottom = view.getBottom() +dividerHeight;

/**

* 绘制的矩形也就是从,item的左上角,到右下角,类似于背景,

* 正好显示出一个横线,就是getItemOffsets空出来的范围

*/

c.drawRect(left, top, right, bottom,paint);

}

}

}

你可能感兴趣的:(SpacesItemDecoration)