-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimpleItemDecoration.java
216 lines (169 loc) · 6.97 KB
/
SimpleItemDecoration.java
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package com.mop.activity.widget;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import com.mop.activity.utils.Utils;
/**
* Created by zzj on 2017/7/7.
* 只实现了LinearLayoutManager
*/
public class SimpleItemDecoration extends RecyclerView.ItemDecoration {
private Paint dividerPaint;
protected final Builder builder;
private SimpleItemDecoration(Builder builder) {
this.builder = builder;
if (builder.getDividerColor() != 0) {
dividerPaint = new Paint();
dividerPaint.setColor(Utils.getColor(builder.getDividerColor()));
}
}
@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
super.onDraw(c, parent, state);
if (dividerPaint == null)
return;
RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
if (layoutManager instanceof LinearLayoutManager) {
/* 获取item数 */
int childCount = parent.getChildCount();
if (((LinearLayoutManager) layoutManager).getOrientation() == LinearLayoutManager.VERTICAL) {
/* 分割块的左边距*/
int left = parent.getPaddingLeft() + builder.getDividerLeftPadding();
/* 分割块的右边距*/
int right = parent.getWidth() - parent.getPaddingRight() - builder.getDividerRightPadding();
if (builder.isShowFirstDivider()) {
/* 绘制RecyclerView第一个分割块*/
c.drawRect(left, 0, right, builder.getDividerHeight(), dividerPaint);
}
/* 循环绘制每一个item底部的分割块*/
for (int i = 0; i < childCount - 1; i++) {
View view = parent.getChildAt(i);
float top = view.getBottom();
float bottom = view.getBottom() + builder.getDividerHeight();
c.drawRect(left, top, right, bottom, dividerPaint);
}
} else {
/* 分割块的上边距*/
int top = parent.getPaddingTop() + builder.getDividerTopPadding();
/* 分割块的下边距*/
int bottom = parent.getHeight() - parent.getPaddingBottom() - builder.getDividerBottomPadding();
if (builder.isShowFirstDivider()) {
/* 绘制RecyclerView第一个分割块*/
c.drawRect(0, top, builder.getDividerWidth(), bottom, dividerPaint);
}
/* 循环绘制每一个item右边的分割块*/
for (int i = 0; i < childCount - 1; i++) {
View view = parent.getChildAt(i);
float left = view.getRight();
float right = view.getRight() + builder.getDividerWidth();
c.drawRect(left, top, right, bottom, dividerPaint);
}
}
}
}
@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
super.onDrawOver(c, parent, state);
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
if (layoutManager instanceof LinearLayoutManager) {
if (((LinearLayoutManager) layoutManager).getOrientation() == LinearLayoutManager.VERTICAL) {
if (builder.isShowFirstDivider()) {
outRect.top = builder.getDividerHeight();
} else {
outRect.bottom = builder.getDividerHeight();
}
} else {
if (builder.isShowFirstDivider()) {
outRect.left = builder.getDividerWidth();
} else {
outRect.bottom = builder.getDividerWidth();
}
}
}
}
public static class Builder {
private final static boolean defaultShowFirstDivider = false;
/* 分割的高度 用于垂直线性布局 */
private int dividerHeight;
/* 分割的宽度 用于水平线性布局... */
private int dividerWidth;
/* 分割的左内边距*/
private int dividerLeftPadding;
/* 分割的右内边距*/
private int dividerRightPadding;
/* 分割的顶部内边距*/
private int dividerTopPadding;
/* 分割的底部内边距*/
private int dividerBottomPadding;
/* 分割的颜色*/
private int dividerColor;
/* 是否显示RecyclerView的第一个分割块*/
private boolean showFirstDivider = defaultShowFirstDivider;
private int getDividerHeight() {
return dividerHeight;
}
public Builder setDividerHeight(int dividerHeight) {
this.dividerHeight = dividerHeight;
return this;
}
private int getDividerLeftPadding() {
return dividerLeftPadding;
}
public Builder setDividerLeftPadding(int dividerLeftPadding) {
this.dividerLeftPadding = dividerLeftPadding;
return this;
}
private int getDividerRightPadding() {
return dividerRightPadding;
}
public Builder setDividerRightPadding(int dividerRightPadding) {
this.dividerRightPadding = dividerRightPadding;
return this;
}
private int getDividerColor() {
return dividerColor;
}
public Builder setDividerColor(int dividerColor) {
this.dividerColor = dividerColor;
return this;
}
private boolean isShowFirstDivider() {
return showFirstDivider;
}
public Builder setShowFirstDivider(boolean showFirstDivider) {
this.showFirstDivider = showFirstDivider;
return this;
}
private int getDividerWidth() {
return dividerWidth;
}
public Builder setDividerWidth(int dividerWidth) {
this.dividerWidth = dividerWidth;
return this;
}
private int getDividerBottomPadding() {
return dividerBottomPadding;
}
public Builder setDividerBottomPadding(int dividerBottomPadding) {
this.dividerBottomPadding = dividerBottomPadding;
return this;
}
private int getDividerTopPadding() {
return dividerTopPadding;
}
public Builder setDividerTopPadding(int dividerTopPadding) {
this.dividerTopPadding = dividerTopPadding;
return this;
}
public SimpleItemDecoration build() {
return new SimpleItemDecoration(this);
}
}
}