-
Notifications
You must be signed in to change notification settings - Fork 232
Expand file tree
/
Copy pathNineGridImageView.java
More file actions
727 lines (696 loc) · 30.5 KB
/
Copy pathNineGridImageView.java
File metadata and controls
727 lines (696 loc) · 30.5 KB
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
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
package com.jaeger.ninegridimageview;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Jaeger on 16/2/24.
* <p>
* Email: chjie.jaeger@gamil.com
* GitHub: https://github.com/laobie
*/
public class NineGridImageView<T> extends ViewGroup {
public final static int STYLE_GRID = 0; // 宫格布局
public final static int STYLE_FILL = 1; // 全填充布局
///////////////////////////////////////////////////////////////////////////
// 跨行跨列的类型
///////////////////////////////////////////////////////////////////////////
public final static int NOSPAN = 0; // 不跨行也不跨列
public final static int TOPCOLSPAN = 2; // 首行跨列
public final static int BOTTOMCOLSPAN = 3; // 末行跨列
public final static int LEFTROWSPAN = 4; // 首列跨行
private int mRowCount; // 行数
private int mColumnCount; // 列数
private int mMaxSize; // 最大图片数
private int mShowStyle; // 显示风格
private int mGap; // 宫格间距
private int mSingleImgSize; // 单张图片时的尺寸
private int mGridSize; // 宫格大小,即图片大小
private int mSpanType; // 跨行跨列的类型
private List<ImageView> mImageViewList = new ArrayList<>();
private List<T> mImgDataList;
private NineGridImageViewAdapter<T> mAdapter;
private ItemImageClickListener<T> mItemImageClickListener;
private ItemImageLongClickListener<T> mItemImageLongClickListener;
public NineGridImageView(Context context) {
this(context, null);
}
public NineGridImageView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.NineGridImageView);
this.mGap = (int) typedArray.getDimension(R.styleable.NineGridImageView_imgGap, 0);
this.mSingleImgSize = typedArray.getDimensionPixelSize(R.styleable.NineGridImageView_singleImgSize, -1);
this.mShowStyle = typedArray.getInt(R.styleable.NineGridImageView_showStyle, STYLE_GRID);
this.mMaxSize = typedArray.getInt(R.styleable.NineGridImageView_maxSize, 9);
typedArray.recycle();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
int totalWidth = width - getPaddingLeft() - getPaddingRight();
if (mImgDataList != null && mImgDataList.size() > 0) {
if (mImgDataList.size() == 1 && mSingleImgSize != -1) {
mGridSize = mSingleImgSize > totalWidth ? totalWidth : mSingleImgSize;
} else {
mImageViewList.get(0).setScaleType(ImageView.ScaleType.CENTER_CROP);
mGridSize = (totalWidth - mGap * (mColumnCount - 1)) / mColumnCount;
}
height = mGridSize * mRowCount + mGap * (mRowCount - 1) + getPaddingTop() + getPaddingBottom();
}
setMeasuredDimension(width, height);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
layoutChildrenView();
}
/**
* 根据照片数量和span类型来对子视图进行动态排版布局
*/
private void layoutChildrenView() {
if (mImgDataList == null) return;
int showChildrenCount = getNeedShowCount(mImgDataList.size());
//对不跨行不跨列的进行排版布局,单张或者2张默认进行普通排版
if (mSpanType == NOSPAN || showChildrenCount <= 2) {
layoutForNoSpanChildrenView(showChildrenCount);
return;
}
switch (showChildrenCount) {
case 3:
layoutForThreeChildrenView(showChildrenCount);
break;
case 4:
layoutForFourChildrenView(showChildrenCount);
break;
case 5:
layoutForFiveChildrenView(showChildrenCount);
break;
case 6:
layoutForSixChildrenView(showChildrenCount);
break;
default:
layoutForNoSpanChildrenView(showChildrenCount);
break;
}
}
private void layoutForNoSpanChildrenView(int childrenCount) {
if (childrenCount <= 0) return;
int row, column, left, top, right, bottom;
for (int i = 0; i < childrenCount; i++) {
ImageView childrenView = (ImageView) getChildAt(i);
row = i / mColumnCount;
column = i % mColumnCount;
left = (mGridSize + mGap) * column + getPaddingLeft();
top = (mGridSize + mGap) * row + getPaddingTop();
right = left + mGridSize;
bottom = top + mGridSize;
childrenView.layout(left, top, right, bottom);
if (mAdapter != null) {
mAdapter.onDisplayImage(getContext(), childrenView, mImgDataList.get(i));
}
}
}
private void layoutForThreeChildrenView(int childrenCount) {
int left, top, right, bottom;
for (int i = 0; i < childrenCount; i++) {
ImageView childrenView = (ImageView) getChildAt(i);
switch (mSpanType) {
case TOPCOLSPAN: //2行2列,首行跨列
if (i == 0) {
left = getPaddingLeft();
top = getPaddingTop();
right = left + mGridSize * 2 + mGap;
bottom = top + mGridSize;
} else if (i == 1) {
left = getPaddingLeft();
top = getPaddingTop() + mGridSize + mGap;
right = left + mGridSize;
bottom = top + mGridSize;
} else {
left = getPaddingLeft() + mGridSize + mGap;
top = getPaddingTop() + mGridSize + mGap;
right = left + mGridSize;
bottom = top + mGridSize;
}
childrenView.layout(left, top, right, bottom);
break;
case BOTTOMCOLSPAN: //2行2列,末行跨列
if (i == 0) {
left = getPaddingLeft();
top = getPaddingTop();
right = left + mGridSize;
bottom = top + mGridSize;
} else if (i == 1) {
left = getPaddingLeft() + mGridSize + mGap;
top = getPaddingTop();
right = left + mGridSize;
bottom = top + mGridSize;
} else {
left = getPaddingLeft();
top = getPaddingTop() + mGridSize + mGap;
right = left + mGridSize * 2 + mGap;
bottom = top + mGridSize;
}
childrenView.layout(left, top, right, bottom);
break;
case LEFTROWSPAN: //2行2列,首列跨行
if (i == 0) {
left = getPaddingLeft();
top = getPaddingTop();
right = left + mGridSize;
bottom = top + mGridSize * 2 + mGap;
} else if (i == 1) {
left = getPaddingLeft() + mGridSize + mGap;
top = getPaddingTop();
right = left + mGridSize;
bottom = top + mGridSize;
} else {
left = getPaddingLeft() + mGridSize + mGap;
top = getPaddingTop() + mGridSize + mGap;
right = left + mGridSize;
bottom = top + mGridSize;
}
childrenView.layout(left, top, right, bottom);
break;
default:
break;
}
if (mAdapter != null) {
mAdapter.onDisplayImage(getContext(), childrenView, mImgDataList.get(i));
}
}
}
private void layoutForFourChildrenView(int childrenCount) {
int left, top, right, bottom;
for (int i = 0; i < childrenCount; i++) {
ImageView childrenView = (ImageView) getChildAt(i);
switch (mSpanType) {
case TOPCOLSPAN: //3行3列,首行跨2行3列
if (i == 0) {
left = getPaddingLeft();
top = getPaddingTop();
right = left + mGridSize * 3 + mGap * 2;
bottom = top + mGridSize * 2 + mGap;
} else if (i == 1) {
left = getPaddingLeft();
top = getPaddingTop() + mGridSize * 2 + mGap * 2;
right = left + mGridSize;
bottom = top + mGridSize;
} else if (i == 2) {
left = getPaddingLeft() + mGridSize + mGap;
top = getPaddingTop() + mGridSize * 2 + mGap * 2;
right = left + mGridSize;
bottom = top + mGridSize;
} else {
left = getPaddingLeft() + mGridSize * 2 + mGap * 2;
top = getPaddingTop() + mGridSize * 2 + mGap * 2;
right = left + mGridSize;
bottom = top + mGridSize;
}
childrenView.layout(left, top, right, bottom);
break;
case BOTTOMCOLSPAN: //3行3列,末行跨2行3列
if (i == 0) {
left = getPaddingLeft();
top = getPaddingTop();
right = left + mGridSize;
bottom = top + mGridSize;
} else if (i == 1) {
left = getPaddingLeft() + mGridSize + mGap;
top = getPaddingTop();
right = left + mGridSize;
bottom = top + mGridSize;
} else if (i == 2) {
left = getPaddingLeft() + mGridSize * 2 + mGap * 2;
top = getPaddingTop();
right = left + mGridSize;
bottom = top + mGridSize;
} else {
left = getPaddingLeft();
top = getPaddingTop() + mGridSize + mGap;
right = left + mGridSize * 3 + mGap * 2;
bottom = top + mGridSize * 2 + mGap;
}
childrenView.layout(left, top, right, bottom);
break;
case LEFTROWSPAN: //3行3列,首列跨3行2列
if (i == 0) {
left = getPaddingLeft();
top = getPaddingTop();
right = left + mGridSize * 2 + mGap;
bottom = top + mGridSize * 3 + mGap * 2;
} else if (i == 1) {
left = getPaddingLeft() + mGridSize * 2 + mGap * 2;
top = getPaddingTop();
right = left + mGridSize;
bottom = top + mGridSize;
} else if (i == 2) {
left = getPaddingLeft() + mGridSize * 2 + mGap * 2;
top = getPaddingTop() + mGridSize + mGap;
right = left + mGridSize;
bottom = top + mGridSize;
} else {
left = getPaddingLeft() + mGridSize * 2 + mGap * 2;
top = getPaddingTop() + mGridSize * 2 + mGap * 2;
right = left + mGridSize;
bottom = top + mGridSize;
}
childrenView.layout(left, top, right, bottom);
break;
default:
break;
}
if (mAdapter != null) {
mAdapter.onDisplayImage(getContext(), childrenView, mImgDataList.get(i));
}
}
}
private void layoutForFiveChildrenView(int childrenCount) {
int left, top, right, bottom;
for (int i = 0; i < childrenCount; i++) {
ImageView childrenView = (ImageView) getChildAt(i);
switch (mSpanType) {
case TOPCOLSPAN: //3行3列,首行跨2行,2列跨3列
if (i == 0) {
left = getPaddingLeft();
top = getPaddingTop();
right = left + (mGridSize * 3 + mGap) / 2;
bottom = top + mGridSize * 2 + mGap;
} else if (i == 1) {
left = getPaddingLeft() + (mGridSize * 3 + mGap) / 2 + mGap;
top = getPaddingTop();
right = left + (mGridSize * 3 + mGap) / 2;
bottom = top + mGridSize * 2 + mGap;
} else if (i == 2) {
left = getPaddingLeft();
top = getPaddingTop() + mGridSize * 2 + mGap * 2;
right = left + mGridSize;
bottom = top + mGridSize;
} else if (i == 3) {
left = getPaddingLeft() + mGridSize + mGap;
top = getPaddingTop() + mGridSize * 2 + mGap * 2;
right = left + mGridSize;
bottom = top + mGridSize;
} else {
left = getPaddingLeft() + mGridSize * 2 + mGap * 2;
top = getPaddingTop() + mGridSize * 2 + mGap * 2;
right = left + mGridSize;
bottom = top + mGridSize;
}
childrenView.layout(left, top, right, bottom);
break;
case BOTTOMCOLSPAN: //3行3列,末行跨2行,2列跨3列
if (i == 0) {
left = getPaddingLeft();
top = getPaddingTop();
right = left + mGridSize;
bottom = top + mGridSize;
} else if (i == 1) {
left = getPaddingLeft() + mGridSize + mGap;
top = getPaddingTop();
right = left + mGridSize;
bottom = top + mGridSize;
} else if (i == 2) {
left = getPaddingLeft() + mGridSize * 2 + mGap * 2;
top = getPaddingTop();
right = left + mGridSize;
bottom = top + mGridSize;
} else if (i == 3) {
left = getPaddingLeft();
top = getPaddingTop() + mGridSize + mGap;
right = left + (mGridSize * 3 + mGap) / 2;
bottom = top + mGridSize * 2 + mGap;
} else {
left = getPaddingLeft() + (mGridSize * 3 + mGap) / 2 + mGap;
top = getPaddingTop() + mGridSize + mGap;
right = left + (mGridSize * 3 + mGap) / 2;
bottom = top + mGridSize * 2 + mGap;
}
childrenView.layout(left, top, right, bottom);
break;
case LEFTROWSPAN: //3行3列,2行跨3行,1列
if (i == 0) {
left = getPaddingLeft();
top = getPaddingTop();
right = left + mGridSize * 2 + mGap;
bottom = top + (mGridSize * 3 + mGap) / 2;
} else if (i == 1) {
left = getPaddingLeft();
top = getPaddingTop() + (mGridSize * 3 + mGap) / 2 + mGap;
right = left + mGridSize * 2 + mGap;
bottom = top + (mGridSize * 3 + mGap) / 2;
} else if (i == 2) {
left = getPaddingLeft() + mGridSize * 2 + mGap * 2;
top = getPaddingTop();
right = left + mGridSize;
bottom = top + mGridSize;
} else if (i == 3) {
left = getPaddingLeft() + mGridSize * 2 + mGap * 2;
top = getPaddingTop() + mGridSize + mGap;
right = left + mGridSize;
bottom = top + mGridSize;
} else {
left = getPaddingLeft() + mGridSize * 2 + mGap * 2;
top = getPaddingTop() + mGridSize * 2 + mGap * 2;
right = left + mGridSize;
bottom = top + mGridSize;
}
childrenView.layout(left, top, right, bottom);
break;
default:
break;
}
if (mAdapter != null) {
mAdapter.onDisplayImage(getContext(), childrenView, mImgDataList.get(i));
}
}
}
private void layoutForSixChildrenView(int childrenCount) {
int left, top, right, bottom;
for (int i = 0; i < childrenCount; i++) {
ImageView childrenView = (ImageView) getChildAt(i);
switch (mSpanType) {
case TOPCOLSPAN: //3行3列,第一张跨2行2列
if (i == 0) {
left = getPaddingLeft();
top = getPaddingTop();
right = left + mGridSize * 2 + mGap;
bottom = top + mGridSize * 2 + mGap;
} else if (i == 1) {
left = getPaddingLeft() + mGridSize * 2 + mGap * 2;
top = getPaddingTop();
right = left + mGridSize;
bottom = top + mGridSize;
} else if (i == 2) {
left = getPaddingLeft() + mGridSize * 2 + mGap * 2;
top = getPaddingTop() + mGridSize + mGap;
right = left + mGridSize;
bottom = top + mGridSize;
} else if (i == 3) {
left = getPaddingLeft();
top = getPaddingTop() + mGridSize * 2 + mGap * 2;
right = left + mGridSize;
bottom = top + mGridSize;
} else if (i == 4) {
left = getPaddingLeft() + mGridSize + mGap;
top = getPaddingTop() + mGridSize * 2 + mGap * 2;
right = left + mGridSize;
bottom = top + mGridSize;
} else {
left = getPaddingLeft() + mGridSize * 2 + mGap * 2;
top = getPaddingTop() + mGridSize * 2 + mGap * 2;
right = left + mGridSize;
bottom = top + mGridSize;
}
childrenView.layout(left, top, right, bottom);
break;
case BOTTOMCOLSPAN: //3行3列,第4张跨2行2列
if (i == 0) {
left = getPaddingLeft();
top = getPaddingTop();
right = left + mGridSize;
bottom = top + mGridSize;
} else if (i == 1) {
left = getPaddingLeft() + mGridSize + mGap;
top = getPaddingTop();
right = left + mGridSize;
bottom = top + mGridSize;
} else if (i == 2) {
left = getPaddingLeft() + mGridSize * 2 + mGap * 2;
top = getPaddingTop();
right = left + mGridSize;
bottom = top + mGridSize;
} else if (i == 3) {
left = getPaddingLeft();
top = getPaddingTop() + mGridSize + mGap;
right = left + mGridSize * 2 + mGap;
bottom = top + mGridSize * 2 + mGap;
} else if (i == 4) {
left = getPaddingLeft() + mGridSize * 2 + mGap * 2;
top = getPaddingTop() + mGridSize + mGap;
right = left + mGridSize;
bottom = top + mGridSize;
} else {
left = getPaddingLeft() + mGridSize * 2 + mGap * 2;
top = getPaddingTop() + mGridSize * 2 + mGap * 2;
right = left + mGridSize;
bottom = top + mGridSize;
}
childrenView.layout(left, top, right, bottom);
break;
case LEFTROWSPAN: //3行3列,第2张跨2行2列
if (i == 0) {
left = getPaddingLeft();
top = getPaddingTop();
right = left + mGridSize;
bottom = top + mGridSize;
} else if (i == 1) {
left = getPaddingLeft() + mGridSize + mGap;
top = getPaddingTop();
right = left + mGridSize * 2 + mGap;
bottom = top + mGridSize * 2 + mGap;
} else if (i == 2) {
left = getPaddingLeft();
top = getPaddingTop() + mGridSize + mGap;
right = left + mGridSize;
bottom = top + mGridSize;
} else if (i == 3) {
left = getPaddingLeft();
top = getPaddingTop() + mGridSize * 2 + mGap * 2;
right = left + mGridSize;
bottom = top + mGridSize;
} else if (i == 4) {
left = getPaddingLeft() + mGridSize + mGap;
top = getPaddingTop() + mGridSize * 2 + mGap * 2;
right = left + mGridSize;
bottom = top + mGridSize;
} else {
left = getPaddingLeft() + mGridSize * 2 + mGap * 2;
top = getPaddingTop() + mGridSize * 2 + mGap * 2;
right = left + mGridSize;
bottom = top + mGridSize;
}
childrenView.layout(left, top, right, bottom);
break;
default:
break;
}
if (mAdapter != null) {
mAdapter.onDisplayImage(getContext(), childrenView, mImgDataList.get(i));
}
}
}
/**
* 根据跨行跨列的类型,以及图片数量,来确定单元格的行数和列数
*
* @param imagesSize 图片数量
* @param gridParam 单元格的行数和列数
*/
private void generatUnitRowAndColumnForSpanType(int imagesSize, int[] gridParam) {
if (imagesSize <= 2) {
gridParam[0] = 1;
gridParam[1] = imagesSize;
} else if (imagesSize == 3) {
switch (mSpanType) {
case TOPCOLSPAN: //2行2列,首行跨列
case BOTTOMCOLSPAN: //2行2列,末行跨列
case LEFTROWSPAN: //2行2列,首列跨行
gridParam[0] = 2;
gridParam[1] = 2;
break;
case NOSPAN: //1行3列
default:
gridParam[0] = 1;
gridParam[1] = 3;
break;
}
} else if (imagesSize <= 6) {
switch (mSpanType) {
case TOPCOLSPAN: //3行3列,首行跨列
case BOTTOMCOLSPAN: //3行3列,末行跨列
case LEFTROWSPAN: //3行3列,首列跨行
gridParam[0] = 3;
gridParam[1] = 3;
break;
case NOSPAN: //2行
default:
gridParam[0] = 2;
gridParam[1] = imagesSize / 2 + imagesSize % 2;
break;
}
} else {
gridParam[0] = imagesSize / 3 + (imagesSize % 3 == 0 ? 0 : 1);
gridParam[1] = 3;
}
}
public void setImagesData(List<T> lists) {
setImagesData(lists, NOSPAN);
}
/**
* 设置图片数据
*
* @param lists 图片数据集合
* @param spanType 跨行跨列排版类型
*/
public void setImagesData(List<T> lists, int spanType) {
if (lists == null || lists.isEmpty()) {
this.setVisibility(GONE);
return;
} else {
this.setVisibility(VISIBLE);
}
this.mSpanType = spanType;
int newShowCount = getNeedShowCount(lists.size());
int[] gridParam = calculateGridParam(newShowCount, mShowStyle);
mRowCount = gridParam[0];
mColumnCount = gridParam[1];
if (mImgDataList == null) {
int i = 0;
while (i < newShowCount) {
ImageView iv = getImageView(i);
if (iv == null) {
return;
}
addView(iv, generateDefaultLayoutParams());
i++;
}
} else {
int oldShowCount = getNeedShowCount(mImgDataList.size());
if (oldShowCount > newShowCount) {
removeViews(newShowCount, oldShowCount - newShowCount);
} else if (oldShowCount < newShowCount) {
for (int i = oldShowCount; i < newShowCount; i++) {
ImageView iv = getImageView(i);
if (iv == null) {
return;
}
addView(iv, generateDefaultLayoutParams());
}
}
}
mImgDataList = lists;
requestLayout();
}
private int getNeedShowCount(int size) {
if (mMaxSize > 0 && size > mMaxSize) {
return mMaxSize;
} else {
return size;
}
}
/**
* 获得 ImageView
* 保证了 ImageView 的重用
*
* @param position 位置
*/
private ImageView getImageView(final int position) {
if (position < mImageViewList.size()) {
return mImageViewList.get(position);
} else {
if (mAdapter != null) {
ImageView imageView = mAdapter.generateImageView(getContext());
mImageViewList.add(imageView);
imageView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mAdapter.onItemImageClick(getContext(), (ImageView) v, position, mImgDataList);
if (mItemImageClickListener != null) {
mItemImageClickListener.onItemImageClick(getContext(), (ImageView) v, position, mImgDataList);
}
}
});
imageView.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
boolean consumedEvent = mAdapter.onItemImageLongClick(getContext(), (ImageView) v, position, mImgDataList);
if (mItemImageLongClickListener != null) {
consumedEvent = mItemImageLongClickListener.onItemImageLongClick(getContext(), (ImageView) v, position, mImgDataList) || consumedEvent;
}
return consumedEvent;
}
});
return imageView;
} else {
Log.e("NineGirdImageView", "Your must set a NineGridImageViewAdapter for NineGirdImageView");
return null;
}
}
}
/**
* 设置 宫格参数
*
* @param imagesSize 图片数量
* @param showStyle 显示风格
* @return 宫格参数 gridParam[0] 宫格行数 gridParam[1] 宫格列数
*/
protected int[] calculateGridParam(int imagesSize, int showStyle) {
int[] gridParam = new int[2];
switch (showStyle) {
case STYLE_FILL:
generatUnitRowAndColumnForSpanType(imagesSize, gridParam);
break;
default:
case STYLE_GRID:
gridParam[0] = imagesSize / 3 + (imagesSize % 3 == 0 ? 0 : 1);
gridParam[1] = 3;
}
return gridParam;
}
/**
* 设置适配器
*
* @param adapter 适配器
*/
public void setAdapter(NineGridImageViewAdapter adapter) {
mAdapter = adapter;
}
/**
* 设置宫格间距
*
* @param gap 宫格间距 px
*/
public void setGap(int gap) {
mGap = gap;
requestLayout();
}
/**
* 设置显示风格
*
* @param showStyle 显示风格
*/
public void setShowStyle(int showStyle) {
mShowStyle = showStyle;
requestLayout();
}
/**
* 设置只有一张图片时的尺寸大小
*
* @param singleImgSize 单张图片的尺寸大小
*/
public void setSingleImgSize(int singleImgSize) {
mSingleImgSize = singleImgSize;
requestLayout();
}
/**
* 设置最大图片数
*
* @param maxSize 最大图片数
*/
public void setMaxSize(int maxSize) {
mMaxSize = maxSize;
requestLayout();
}
public void setItemImageClickListener(ItemImageClickListener<T> itemImageViewClickListener) {
mItemImageClickListener = itemImageViewClickListener;
}
public void setItemImageLongClickListener(ItemImageLongClickListener<T> itemImageViewLongClickListener) {
mItemImageLongClickListener = itemImageViewLongClickListener;
}
}