首先自定義一個RecyclerViewDivider 繼承 RecyclerView.ItemDecoration,實現自定義。android
public class RecyclerViewDivider extends RecyclerView.ItemDecoration {
private Paint mPaint;
private Drawable mDivider;
private int mDividerSize = 2;//分割線高度,默認爲1px
private int mOffsetStart = 0;
private int mOffsetEnd = 0;
private int mOrientation;//列表的方向:LinearLayoutManager.VERTICAL或LinearLayoutManager.HORIZONTAL
private static final int[] ATTRS = new int[]{android.R.attr.listDivider};canvas
/**
* 默認分割線:高度爲2px,顏色爲灰色
*
* @param context
* @param orientation 列表方向
*/
private RecyclerViewDivider(Context context, int orientation) {
if (orientation != LinearLayoutManager.VERTICAL && orientation != LinearLayoutManager.HORIZONTAL) {
throw new IllegalArgumentException("請輸入正確的參數!");
}
mOrientation = orientation;ide
final TypedArray a = context.obtainStyledAttributes(ATTRS);
mDivider = a.getDrawable(0);
a.recycle();
}this
/**
* 自定義分割線
*
* @param context
* @param orientation 列表方向
* @param drawableId 分割線圖片
*/
public RecyclerViewDivider(Context context, int orientation, int drawableId) {
this(context, orientation);
mDivider = ContextCompat.getDrawable(context, drawableId);
mDividerSize = mDivider.getIntrinsicHeight();
}繼承
/**
* 自定義分割線
*
* @param context
* @param orientation 列表方向
* @param dividerSize 分割線高度
* @param dividerColor 分割線顏色
*/
public RecyclerViewDivider(Context context, int orientation, int dividerSize, @ColorRes int dividerColor) {
this(context, orientation);
mDividerSize = dividerSize;
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);圖片
mPaint.setColor(context.getResources().getColor(dividerColor));
mPaint.setStyle(Paint.Style.FILL);
}get
/**
* 自定義分割線
*
* @param context context
* @param orientation 列表方向
* @param dividerHeight 分割線高度
* @param dividerColor 分割線顏色
* @param offsetStart 起始偏移量
* @param offsetEnd 結束偏移量
*/
public RecyclerViewDivider(Context context, int orientation, int dividerHeight, @ColorRes int dividerColor, int offsetStart, int offsetEnd) {
this(context, orientation);
mDividerSize = dividerHeight;
mOffsetStart = Util.dp2px(context, offsetStart);
mOffsetEnd = Util.dp2px(context, offsetEnd);
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
if (dividerColor == 0) {
dividerColor = R.color.lightGray;
}
mPaint.setColor(context.getResources().getColor(dividerColor));
mPaint.setStyle(Paint.Style.FILL);
}it
//重寫此方法,防止設置的波紋背景把分隔線覆蓋掉。
@Override
public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
RecyclerView.ViewHolder childViewHolder = parent.getChildViewHolder(view);
if (childViewHolder.getItemViewType() != 0) {
outRect.set(0, 0, 0, 0);
return;
}
if (mOrientation == LinearLayoutManager.HORIZONTAL) {
outRect.set(0, 0, 0, mDividerSize);
} else {
outRect.set(0, 0, mDividerSize, 0);
}
}io
//繪製分割線
@Override
public void onDraw(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
if (mOrientation == LinearLayoutManager.VERTICAL) {
drawVertical(c, parent);
} else {
drawHorizontal(c, parent);
}
}class
//繪製橫向 item 分割線
private void drawHorizontal(Canvas canvas, RecyclerView parent) {
final int left = parent.getPaddingLeft() + mOffsetStart;
final int right = parent.getMeasuredWidth() - (parent.getPaddingRight() + mOffsetEnd);
final int childSize = parent.getChildCount();
for (int i = 0; i < childSize - 1; i++) {
final View child = parent.getChildAt(i);
RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams();
final int top = child.getBottom() + layoutParams.bottomMargin;
final int bottom = top + mDividerSize;
if (mDivider != null) {
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(canvas);
}
if (mPaint != null) {
canvas.drawRect(left, top, right, bottom, mPaint);
}
}
}
//繪製縱向 item 分割線
private void drawVertical(Canvas canvas, RecyclerView parent) {
final int top = parent.getPaddingTop() + mDividerSize;
final int bottom = parent.getMeasuredHeight() - parent.getPaddingBottom() + mDividerSize;
final int childSize = parent.getChildCount();
for (int i = 0; i < childSize; i++) {
final View child = parent.getChildAt(i);
RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams();
final int left = child.getRight() + layoutParams.rightMargin;
final int right = left + mDividerSize;
if (mDivider != null) {
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(canvas);
}
if (mPaint != null) {
canvas.drawRect(left, top, right, bottom, mPaint);
}
}
}
}
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
舉個例子:
rcyDetailsDelegation.addItemDecoration(new RecyclerViewDivider(getContext(), LinearLayoutManager.HORIZONTAL, 5, R.color.white, 8, 8));12其中提供一個 RecyclerViewDivider:---------------------