分割线
public class MyDecoration extends DividerItemDecoration {
private int pos = -1;
/ ** * Creates a divider {@ link R ecyclerView.ItemDecoration} that can be used with a * * @ param c ontext Current context, it will be used to access resources. * @ param o rientation Divider orientation. Should be {@ link # HORIZONTAL} or {@ link # VERTICAL}. */ public MyDecoration (Context context, int orientation) {
super(context, orientation);
}
@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
drawVertical(c, parent);
}
private final Rect mBounds = new Rect();
private void drawVertical(Canvas canvas, RecyclerView parent) {
canvas.save();
final int left;
final int right;
if (parent.getClipToPadding()) {
left = parent.getPaddingLeft();
right = parent.getWidth() - parent.getPaddingRight();
canvas.clipRect(left, parent.getPaddingTop(), right,
parent.getHeight() - parent.getPaddingBottom());
} else {
left = 0;
right = parent.getWidth();
}
final int childCount = parent.getChildCount();
for (int i = 1; i < childCount-1; i++) {
final View child = parent.getChildAt(i);
parent.getDecoratedBoundsWithMargins(child, mBounds);
final int bottom = mBounds.bottom + Math.round( child.getTranslationY());
final int top = bottom - getDrawable().getIntrinsicHeight();
getDrawable().setBounds(left, top, right, bottom);
getDrawable().draw(canvas);
}
canvas.restore();
}
}
item间的空白
public class SpaceItemTopDecoration extends RecyclerView.ItemDecoration {
private int space = 0;
public SpaceItemTopDecoration(int space) {
this.space = space;
}
@Override
public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
if (parent.getChildAdapterPosition(view) != 0) {
outRect.top = space;
}
if (parent.getChildAdapterPosition(view)==parent.getChildCount()-1) {
outRect.bottom = space;
}
}
}