forked from haozi5460/GridDividerItemDecoration
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GridDividerItemDecoration.java
295 lines (261 loc) · 11 KB
/
GridDividerItemDecoration.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package com.starlight.mobile.android.smsone.common;
import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.ColorInt;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
/**
* Created by Haozi on 2018/10/10 0023.
*/
public class GridDividerItemDecoration extends RecyclerView.ItemDecoration {
private Paint mPaint;
private int mDividerWidth; //您所需指定的间隔宽度,主要为第一列和最后一列与父控件的间隔;行间距,列间距将动态分配
private int mFirstRowTopMargin = 0; //第一行顶部是否需要间隔
private boolean isNeedSpace = false;//第一列和最后一列是否需要指定间隔(默认不指定)
private boolean isLastRowNeedSpace = false;//最后一行是否需要间隔(默认不需要)
int spanCount = 0;
private Context mContext;
/**
*
* @param dividerWidth 间隔宽度
* @param isNeedSpace 第一列和最后一列是否需要间隔
*/
public GridDividerItemDecoration(Context context, int dividerWidth, boolean isNeedSpace) {
this(context,dividerWidth,0,isNeedSpace,false);
}
/**
*
* @param dividerWidth 间隔宽度
* @param isNeedSpace 第一列和最后一列是否需要间隔
* @param firstRowTopMargin 第一行顶部是否需要间隔(根据间隔大小判断)
*/
public GridDividerItemDecoration(Context context, int dividerWidth, int firstRowTopMargin, boolean isNeedSpace) {
this(context,dividerWidth,firstRowTopMargin,isNeedSpace,false);
}
/**
* @param dividerWidth 间隔宽度
* @param firstRowTopMargin 第一行顶部是否需要间隔
* @param isNeedSpace 第一列和最后一列是否需要间隔
* @param isLastRowNeedSpace 最后一行是否需要间隔
*/
public GridDividerItemDecoration(Context context, int dividerWidth, int firstRowTopMargin, boolean isNeedSpace, boolean isLastRowNeedSpace) {
this(context,dividerWidth,firstRowTopMargin,isNeedSpace,isLastRowNeedSpace, Color.WHITE);
}
/**
* @param dividerWidth 间隔宽度
* @param firstRowTopMargin 第一行顶部是否需要间隔
* @param isNeedSpace 第一列和最后一列是否需要间隔
* @param isLastRowNeedSpace 最后一行是否需要间隔
*/
public GridDividerItemDecoration(Context context, int dividerWidth, int firstRowTopMargin, boolean isNeedSpace, boolean isLastRowNeedSpace, @ColorInt int color) {
mDividerWidth = dividerWidth;
this.isNeedSpace = isNeedSpace;
this.mContext = context;
this.isLastRowNeedSpace = isLastRowNeedSpace;
this.mFirstRowTopMargin = firstRowTopMargin;
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setColor(color);
mPaint.setStyle(Paint.Style.FILL);
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
int top = 0;
int left = 0;
int right = 0;
int bottom = 0;
int itemPosition = ((RecyclerView.LayoutParams) view.getLayoutParams()).getViewLayoutPosition();
spanCount = getSpanCount(parent);
int childCount = parent.getAdapter().getItemCount();
int maxAllDividerWidth = getMaxDividerWidth(view); //
int spaceWidth = 0;//首尾两列与父布局之间的间隔
if(isNeedSpace)
spaceWidth = mDividerWidth;
int eachItemWidth = maxAllDividerWidth/spanCount;//每个Item left+right
int dividerItemWidth = (maxAllDividerWidth-2*spaceWidth)/(spanCount-1);//item与item之间的距离
left = itemPosition % spanCount * (dividerItemWidth - eachItemWidth) + spaceWidth;
right = eachItemWidth - left;
bottom = mDividerWidth;
if(mFirstRowTopMargin > 0 && isFirstRow(parent, itemPosition, spanCount, childCount))//第一行顶部是否需要间隔
top = mFirstRowTopMargin;
if (!isLastRowNeedSpace && isLastRow(parent, itemPosition, spanCount, childCount)){//最后一行是否需要间隔
bottom = 0;
}
outRect.set(left, top, right, bottom);
}
/**
* 获取Item View的大小,若无则自动分配空间
* 并根据 屏幕宽度-View的宽度*spanCount 得到屏幕剩余空间
* @param view
* @return
*/
private int getMaxDividerWidth(View view) {
int itemWidth = view.getLayoutParams().width;
int itemHeight = view.getLayoutParams().height;
int screenWidth = mContext.getResources().getDisplayMetrics().widthPixels > mContext.getResources().getDisplayMetrics().heightPixels
? mContext.getResources().getDisplayMetrics().heightPixels : mContext.getResources().getDisplayMetrics().widthPixels;
int maxDividerWidth = screenWidth-itemWidth * spanCount;
if(itemHeight < 0 || itemWidth < 0 || (isNeedSpace && maxDividerWidth <= (spanCount-1) * mDividerWidth)){
view.getLayoutParams().width = getAttachCloumnWidth();
view.getLayoutParams().height = getAttachCloumnWidth();
maxDividerWidth = screenWidth-view.getLayoutParams().width * spanCount;
}
return maxDividerWidth;
}
/**
* 根据屏幕宽度和item数量分配 item View的width和height
* @return
*/
private int getAttachCloumnWidth() {
int itemWidth = 0;
int spaceWidth = 0;
try {
int width = mContext.getResources().getDisplayMetrics().widthPixels > mContext.getResources().getDisplayMetrics().heightPixels
? mContext.getResources().getDisplayMetrics().heightPixels : mContext.getResources().getDisplayMetrics().widthPixels;
if(isNeedSpace)
spaceWidth = 2 * mDividerWidth;
itemWidth = (width-spaceWidth) / spanCount - 40;
} catch (Exception e) {
e.printStackTrace();
}
return itemWidth;
}
@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
super.onDraw(c, parent, state);
draw(c, parent);
}
//绘制item分割线
private void draw(Canvas canvas, RecyclerView parent) {
int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
View child = parent.getChildAt(i);
RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams();
//画水平分隔线
int left = child.getLeft();
int right = child.getRight();
int top = child.getBottom() + layoutParams.bottomMargin;
int bottom = top + mDividerWidth;
if (mPaint != null) {
canvas.drawRect(left, top, right, bottom, mPaint);
}
//画垂直分割线
top = child.getTop();
bottom = child.getBottom() + mDividerWidth;
left = child.getRight() + layoutParams.rightMargin;
right = left + mDividerWidth;
if (mPaint != null) {
canvas.drawRect(left, top, right, bottom, mPaint);
}
}
}
/**
* 判读是否是第一列
* @param parent
* @param pos
* @param spanCount
* @param childCount
* @return
*/
private boolean isFirstColumn(RecyclerView parent, int pos, int spanCount, int childCount) {
RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
if (layoutManager instanceof GridLayoutManager) {
if (pos % spanCount == 0) {
return true;
}
} else if (layoutManager instanceof StaggeredGridLayoutManager) {
int orientation = ((StaggeredGridLayoutManager) layoutManager)
.getOrientation();
if (orientation == StaggeredGridLayoutManager.VERTICAL) {
if (pos % spanCount == 0) {// 第一列
return true;
}
} else {
}
}
return false;
}
/**
* 判断是否是最后一列
* @param parent
* @param pos
* @param spanCount
* @param childCount
* @return
*/
private boolean isLastColumn(RecyclerView parent, int pos, int spanCount, int childCount) {
RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
if (layoutManager instanceof GridLayoutManager) {
if ((pos + 1) % spanCount == 0) {// 如果是最后一列,则不需要绘制右边
return true;
}
} else if (layoutManager instanceof StaggeredGridLayoutManager) {
int orientation = ((StaggeredGridLayoutManager) layoutManager)
.getOrientation();
if (orientation == StaggeredGridLayoutManager.VERTICAL) {
if ((pos + 1) % spanCount == 0) {// 最后一列
return true;
}
} else {
}
}
return false;
}
/**
* 判读是否是最后一行
* @param parent
* @param pos
* @param spanCount
* @param childCount
* @return
*/
private boolean isLastRow(RecyclerView parent, int pos, int spanCount, int childCount) {
RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
if (layoutManager instanceof GridLayoutManager) {
int lines = childCount % spanCount == 0 ? childCount / spanCount : childCount / spanCount + 1;
return lines == pos / spanCount + 1;
} else if (layoutManager instanceof StaggeredGridLayoutManager) {
}
return false;
}
/**
* 判断是否是第一行
* @param parent
* @param pos
* @param spanCount
* @param childCount
* @return
*/
private boolean isFirstRow(RecyclerView parent, int pos, int spanCount, int childCount) {
RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
if (layoutManager instanceof GridLayoutManager) {
if ((pos / spanCount + 1) == 1) {
return true;
} else {
return false;
}
} else if (layoutManager instanceof StaggeredGridLayoutManager) {
}
return false;
}
/**
* 获取列数
* @param parent
* @return
*/
private int getSpanCount(RecyclerView parent) {
int spanCount = -1;
RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
if (layoutManager instanceof GridLayoutManager) {
spanCount = ((GridLayoutManager) layoutManager).getSpanCount();
} else if (layoutManager instanceof StaggeredGridLayoutManager) {
spanCount = ((StaggeredGridLayoutManager) layoutManager).getSpanCount();
}
return spanCount;
}
}