已经不记得是从哪里转载的了 RecycleView很好用但是不熟悉 这个分割线写下记录一下
新建一个名称为 RecyclerViewItemDecoration 的类即可
复制代码
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.DashPathEffect; import android.graphics.NinePatch; import android.graphics.Paint; import android.graphics.Path; import android.graphics.PathEffect; 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; import java.util.regex.Pattern; /** * RecycleView item decoration * Created by Eminem Lu on 24/11/15. * Email arjinmc@hotmail.com */ public class RecyclerViewItemDecoration extends RecyclerView.ItemDecoration { /** * mode for direction */ public static final int MODE_HORIZONTAL = 0; public static final int MODE_VERTICAL = 1; public static final int MODE_GRID = 2; /** * default decoration color */ private static final String DEFAULT_COLOR = "#cccccc"; /** * image resource id for R.java */ private int mDrawableRid = 0; /** * decoration color */ private int mColor = Color.parseColor(DEFAULT_COLOR); /** * decoration thickness */ private int mThickness; /** * decoration dash with */ private int mDashWidth = 0; /** * decoration dash gap */ private int mDashGap = 0; private boolean mFirstLineVisible; private boolean mLastLineVisible; private int mPaddingStart = 0; private int mPaddingEnd = 0; /** * direction mode for decoration */ private int mMode; private Paint mPaint; private Bitmap mBmp; private NinePatch mNinePatch; /** * choose the real thickness for image or thickness */ private int mCurrentThickness; /** * sign for if the resource image is a ninepatch image */ private Boolean hasNinePatch = false; public RecyclerViewItemDecoration() { } @Deprecated public RecyclerViewItemDecoration(int recyclerviewMode, Context context, int drawableRid) { this.mMode = recyclerviewMode; this.mDrawableRid = drawableRid; this.mBmp = BitmapFactory.decodeResource(context.getResources(), drawableRid); if (mBmp.getNinePatchChunk() != null) { hasNinePatch = true; mNinePatch = new NinePatch(mBmp, mBmp.getNinePatchChunk(), null); } initPaint(); } @Deprecated public RecyclerViewItemDecoration(int recyclerviewMode, int color, int thick, int dashWidth, int dashGap) { this.mMode = recyclerviewMode; this.mColor = color; this.mThickness = thick; this.mDashWidth = dashWidth; this.mDashGap = dashGap; initPaint(); } @Deprecated public RecyclerViewItemDecoration(int recyclerviewMode, String color, int thick, int dashWidth, int dashGap) { this.mMode = recyclerviewMode; if (isColorString(color)) { this.mColor = Color.parseColor(color); } else { this.mColor = Color.parseColor(DEFAULT_COLOR); } this.mThickness = thick; this.mDashWidth = dashWidth; this.mDashGap = dashGap; initPaint(); } public void setParams(Context context, Param params) { this.mMode = params.mode; this.mDrawableRid = params.drawableRid; this.mColor = params.color; this.mThickness = params.thickness; this.mDashGap = params.dashGap; this.mDashWidth = params.dashWidth; this.mPaddingStart = params.paddingStart; this.mPaddingEnd = params.paddingEnd; this.mFirstLineVisible = params.firstLineVisible; this.mLastLineVisible = params.lastLineVisible; this.mBmp = BitmapFactory.decodeResource(context.getResources(), mDrawableRid); if (mBmp != null) { if (mBmp.getNinePatchChunk() != null) { hasNinePatch = true; mNinePatch = new NinePatch(mBmp, mBmp.getNinePatchChunk(), null); } if (mMode == MODE_HORIZONTAL) mCurrentThickness = mThickness == 0 ? mBmp.getHeight() : mThickness; if (mMode == MODE_VERTICAL) mCurrentThickness = mThickness == 0 ? mBmp.getWidth() : mThickness; } initPaint(); } private void initPaint() { mPaint = new Paint(); mPaint.setColor(mColor); mPaint.setStyle(Paint.Style.STROKE); mPaint.setStrokeWidth(mThickness); } @Override public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) { mPaint.setColor(mColor); if (mMode == MODE_HORIZONTAL) { drawHorinzonal(c, parent); } else if (mMode == MODE_VERTICAL) { drawVertical(c, parent); } else if (mMode == MODE_GRID) { drawGrid(c, parent); } } @Override public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { if (mMode == MODE_HORIZONTAL) { if (!(!mLastLineVisible && parent.getChildLayoutPosition(view) == parent.getAdapter().getItemCount() - 1)) { if (mDrawableRid != 0) { outRect.set(0, 0, 0, mCurrentThickness); } else { outRect.set(0, 0, 0, mThickness); } } if (mFirstLineVisible && parent.getChildLayoutPosition(view) == 0) { if (mDrawableRid != 0) { outRect.set(0, mCurrentThickness, 0, mCurrentThickness); } else { outRect.set(0, mThickness, 0, mThickness); } } } else if (mMode == MODE_VERTICAL) { if (!(!mLastLineVisible && parent.getChildLayoutPosition(view) == parent.getAdapter().getItemCount() - 1)) { if (mDrawableRid != 0) { outRect.set(0, 0, mCurrentThickness, 0); } else { outRect.set(0, 0, mThickness, 0); } } if (mFirstLineVisible && parent.getChildLayoutPosition(view) == 0) { if (mDrawableRid != 0) { outRect.set(mCurrentThickness, 0, mCurrentThickness, 0); } else { outRect.set(mThickness, 0, mThickness, 0); } } } else if (mMode == MODE_GRID) { int columnSize = ((GridLayoutManager) parent.getLayoutManager()).getSpanCount(); int itemSzie = parent.getAdapter().getItemCount(); if (mDrawableRid != 0) { if (isLastRowGrid(parent.getChildLayoutPosition(view), itemSzie, columnSize) && isLastGridColumn(parent.getChildLayoutPosition(view), columnSize)) { outRect.set(0, 0, 0, 0); } else if (isLastRowGrid(parent.getChildLayoutPosition(view), itemSzie, columnSize)) { outRect.set(0, 0, mBmp.getWidth(), 0); } else if ((parent.getChildLayoutPosition(view) + 1) % columnSize != 0) { outRect.set(0, 0, mBmp.getWidth(), mBmp.getHeight()); } else { outRect.set(0, 0, 0, mBmp.getHeight()); } } else { if (isLastRowGrid(parent.getChildLayoutPosition(view), itemSzie, columnSize) && isLastGridColumn(parent.getChildLayoutPosition(view), columnSize)) { outRect.set(0, 0, 0, 0); } else if (isLastRowGrid(parent.getChildLayoutPosition(view), itemSzie, columnSize)) { outRect.set(0, 0, mThickness, 0); } else if ((parent.getChildLayoutPosition(view) + 1) % columnSize != 0) { outRect.set(0, 0, mThickness, mThickness); } else { outRect.set(0, 0, 0, mThickness); } } } } /** * judge is a color string like #xxxxxx or #xxxxxxxx * * @param colorStr * @return */ public static boolean isColorString(String colorStr) { return Pattern.matches("^#([0-9a-fA-F]{6}||[0-9a-fA-F]{8})$", colorStr); } private boolean isPureLine() { if (mDashGap == 0 && mDashWidth == 0) return true; return false; } /** * draw horizonal decoration * * @param c * @param parent */ private void drawHorinzonal(Canvas c, RecyclerView parent) { int childrentCount = parent.getChildCount(); if (mDrawableRid != 0) { if (mFirstLineVisible) { View childView = parent.getChildAt(0); int myY = childView.getTop(); if (hasNinePatch) { Rect rect = new Rect(mPaddingStart, myY - mCurrentThickness, parent.getWidth() - mPaddingEnd, myY); mNinePatch.draw(c, rect); } else { c.drawBitmap(mBmp, mPaddingStart, myY - mCurrentThickness, mPaint); } } for (int i = 0; i < childrentCount; i++) { if (!mLastLineVisible && i == childrentCount - 1) break; View childView = parent.getChildAt(i); int myY = childView.getBottom(); if (hasNinePatch) { Rect rect = new Rect(mPaddingStart, myY, parent.getWidth() - mPaddingEnd, myY + mCurrentThickness); mNinePatch.draw(c, rect); } else { c.drawBitmap(mBmp, mPaddingStart, myY, mPaint); } } } else { boolean isPureLine = isPureLine(); if (!isPureLine) { PathEffect effects = new DashPathEffect(new float[]{0, 0, mDashWidth, mThickness}, mDashGap); mPaint.setPathEffect(effects); } if (mFirstLineVisible) { View childView = parent.getChildAt(0); int myY = childView.getTop() - mThickness/ 2 ; if (isPureLine) { c.drawLine(mPaddingStart, myY, parent.getWidth() - mPaddingEnd, myY, mPaint); } else { Path path = new Path(); path.moveTo(mPaddingStart, myY); path.lineTo(parent.getWidth() - mPaddingEnd, myY); c.drawPath(path, mPaint); } } for (int i = 0; i < childrentCount; i++) { if (!mLastLineVisible && i == childrentCount - 1) break; View childView = parent.getChildAt(i); int myY = childView.getBottom() + mThickness / 2; if (isPureLine) { c.drawLine(mPaddingStart, myY, parent.getWidth() - mPaddingEnd, myY, mPaint); } else { Path path = new Path(); path.moveTo(mPaddingStart, myY); path.lineTo(parent.getWidth() - mPaddingEnd, myY); c.drawPath(path, mPaint); } } } } /** * draw vertival decoration * * @param c * @param parent */ private void drawVertical(Canvas c, RecyclerView parent) { int childrentCount = parent.getChildCount(); if (mDrawableRid != 0) { if (mFirstLineVisible) { View childView = parent.getChildAt(0); int myX = childView.getLeft(); if (hasNinePatch) { Rect rect = new Rect(myX - mCurrentThickness, mPaddingStart, myX, parent.getHeight() - mPaddingEnd); mNinePatch.draw(c, rect); } else { c.drawBitmap(mBmp, myX - mCurrentThickness, mPaddingStart, mPaint); } } for (int i = 0; i < childrentCount; i++) { if (!mLastLineVisible && i == childrentCount - 1) break; View childView = parent.getChildAt(i); int myX = childView.getRight(); if (hasNinePatch) { Rect rect = new Rect(myX, mPaddingStart, myX + mCurrentThickness, parent.getHeight() - mPaddingEnd); mNinePatch.draw(c, rect); } else { c.drawBitmap(mBmp, myX, mPaddingStart, mPaint); } } } else { boolean isPureLine = isPureLine(); if (!isPureLine) { PathEffect effects = new DashPathEffect(new float[]{0, 0, mDashWidth, mThickness}, mDashGap); mPaint.setPathEffect(effects); } if (mFirstLineVisible) { View childView = parent.getChildAt(0); int myX = childView.getLeft() - mThickness / 2; if (isPureLine) { c.drawLine(myX, mPaddingStart, myX, parent.getHeight() - mPaddingEnd, mPaint); } else { Path path = new Path(); path.moveTo(myX, mPaddingStart); path.lineTo(myX, parent.getHeight() - mPaddingEnd); c.drawPath(path, mPaint); } } for (int i = 0; i < childrentCount; i++) { if (!mLastLineVisible && i == childrentCount - 1) break; View childView = parent.getChildAt(i); int myX = childView.getRight() + mThickness / 2; if (isPureLine) { c.drawLine(myX, mPaddingStart, myX, parent.getHeight() - mPaddingEnd, mPaint); } else { Path path = new Path(); path.moveTo(myX, mPaddingStart); path.lineTo(myX, parent.getHeight() - mPaddingEnd); c.drawPath(path, mPaint); } } } } /** * draw grid decoration * * @param c * @param parent */ private void drawGrid(Canvas c, RecyclerView parent) { int childrentCount = parent.getChildCount(); int columnSize = ((GridLayoutManager) parent.getLayoutManager()).getSpanCount(); int adapterChildrenCount = parent.getAdapter().getItemCount(); if (mDrawableRid != 0) { if (hasNinePatch) { for (int i = 0; i < childrentCount; i++) { View childView = parent.getChildAt(i); int myX = childView.getRight(); int myY = childView.getBottom(); //horizonal if (!isLastRowGrid(i, adapterChildrenCount, columnSize)) { Rect rect = new Rect(0, myY, myX, myY + mBmp.getHeight()); mNinePatch.draw(c, rect); } //vertical if (isLastRowGrid(i, adapterChildrenCount, columnSize) && !isLastGridColumn(i, columnSize)) { Rect rect = new Rect(myX, childView.getTop(), myX + mBmp.getWidth(), myY); mNinePatch.draw(c, rect); } else if (!isLastGridColumn(i, columnSize)) { Rect rect = new Rect(myX, childView.getTop(), myX + mBmp.getWidth(), myY + mBmp.getHeight()); mNinePatch.draw(c, rect); } } } else { for (int i = 0; i < childrentCount; i++) { View childView = parent.getChildAt(i); int myX = childView.getRight(); int myY = childView.getBottom(); //horizonal if (!isLastRowGrid(i, adapterChildrenCount, columnSize)) { c.drawBitmap(mBmp, childView.getLeft(), myY, mPaint); } //vertical if (!isLastGridColumn(i, columnSize)) { c.drawBitmap(mBmp, myX, childView.getTop(), mPaint); } } } } else if (mDashWidth == 0 && mDashGap == 0) { for (int i = 0; i < childrentCount; i++) { View childView = parent.getChildAt(i); int myX = childView.getRight() + mThickness / 2; int myY = childView.getBottom() + mThickness / 2; //horizonal if (!isLastRowGrid(i, adapterChildrenCount, columnSize)) { c.drawLine(childView.getLeft(), myY, childView.getRight() + mThickness, myY, mPaint); } //vertical if (isLastRowGrid(i, adapterChildrenCount, columnSize) && !isLastGridColumn(i, columnSize)) { c.drawLine(myX, childView.getTop(), myX, childView.getBottom(), mPaint); } else if (!isLastGridColumn(i, columnSize)) { c.drawLine(myX, childView.getTop(), myX, myY, mPaint); } } } else { PathEffect effects = new DashPathEffect(new float[]{0, 0, mDashWidth, mThickness}, mDashGap); mPaint.setPathEffect(effects); for (int i = 0; i < childrentCount; i++) { View childView = parent.getChildAt(i); int myX = childView.getRight() + mThickness / 2; int myY = childView.getBottom() + mThickness / 2; //horizonal if (!isLastRowGrid(i, adapterChildrenCount, columnSize)) { Path path = new Path(); path.moveTo(0, myY); path.lineTo(myX, myY); c.drawPath(path, mPaint); } //vertical if (isLastRowGrid(i, adapterChildrenCount, columnSize) && !isLastGridColumn(i, columnSize)) { Path path = new Path(); path.moveTo(myX, childView.getTop()); path.lineTo(myX, childView.getBottom()); c.drawPath(path, mPaint); } else if (!isLastGridColumn(i, columnSize)) { Path path = new Path(); path.moveTo(myX, childView.getTop()); path.lineTo(myX, childView.getBottom()); c.drawPath(path, mPaint); } } } } /** * check if is one of the last columns * * @param position * @param columnSize * @return */ private boolean isLastGridColumn(int position, int columnSize) { boolean isLast = false; if ((position + 1) % columnSize == 0) { isLast = true; } return isLast; } /** * check if is the last row of the grid * * @param position * @param itemSize * @param columnSize * @return */ private boolean isLastRowGrid(int position, int itemSize, int columnSize) { return position / columnSize == (itemSize - 1) / columnSize; } public static class Builder { private Param params; private Context context; public Builder(Context context) { params = new Param(); this.context = context; } public RecyclerViewItemDecoration create() { RecyclerViewItemDecoration recyclerViewItemDecoration = new RecyclerViewItemDecoration(); recyclerViewItemDecoration.setParams(context, params); return recyclerViewItemDecoration; } public Builder mode(int mode) { params.mode = mode; return this; } public Builder drawableID(int drawableID) { params.drawableRid = drawableID; return this; } public Builder color(@ColorInt int color) { params.color = color; return this; } public Builder color(String color) { if (isColorString(color)) { params.color = Color.parseColor(color); } return this; } public Builder thickness(int thickness) { params.thickness = thickness; return this; } public Builder dashWidth(int dashWidth) { params.dashWidth = dashWidth; return this; } public Builder dashGap(int dashGap) { params.dashGap = dashGap; return this; } public Builder lastLineVisible(boolean visible) { params.lastLineVisible = visible; return this; } public Builder firstLineVisible(boolean visible) { params.firstLineVisible = visible; return this; } public Builder paddingStart(int padding) { params.paddingStart = padding; return this; } public Builder paddingEnd(int padding) { params.paddingEnd = padding; return this; } } private static class Param { public int mode = MODE_HORIZONTAL; public int drawableRid = 0; public int color = Color.parseColor(DEFAULT_COLOR); public int thickness; public int dashWidth = 0; public int dashGap = 0; public boolean lastLineVisible; public boolean firstLineVisible; public int paddingStart; public int paddingEnd; } }
这里不强调怎么使用RecycleView 下面是调用方法
复制代码
1
2
3
4
5
6
7recyclerView. addItemDecoration(new RecyclerViewItemDecoration.Builder(getActivity()) .mode(RecyclerViewItemDecoration.MODE_GRID) // .dashWidth(8) // .dashGap(5) .thickness(1) //.drawableID(R.color.line3) .create());
OK完毕 更多功能自己去挖掘 下面是截图
最后
以上就是拉长小兔子最近收集整理的关于Android RecycleView 网格分割线的全部内容,更多相关Android内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复