StackLabel.java
17.4 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
package com.hh.xuetubao.stacklabel;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.hh.xuetubao.R;
import java.util.ArrayList;
import java.util.List;
public class StackLabel extends ViewGroup {
private int textColor = 0;
private int textSize = 0;
private int paddingVertical = 0;
private int paddingHorizontal = 0;
private int itemMargin = 0;
private int itemMarginVertical = 0;
private int itemMarginHorizontal = 0;
private int maxLines = 0;
private boolean deleteButton = false;
private int deleteButtonImage = -1;
private int labelBackground = -1;
private boolean selectMode = false;
private int selectBackground = -1;
private int selectTextColor = -1;
private int maxSelectNum = 0;
private int minSelectNum = 0;
private OnLabelClickListener onLabelClickListener;
private Context context;
private List<String> labels;
private String loadLabelsArray;
public StackLabel(Context context) {
super(context);
this.context = context;
}
public StackLabel(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
loadAttrs(context, attrs);
}
public StackLabel(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.context = context;
loadAttrs(context, attrs);
}
private void loadAttrs(Context context, AttributeSet attrs) {
try {
//默认值
textColor = Color.argb(230, 0, 0, 0);
textSize = dp2px(12);
paddingVertical = dp2px(8);
paddingHorizontal = dp2px(12);
itemMargin = dp2px(4);
deleteButton = false;
//加载值
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.StackLabel);
textColor = typedArray.getColor(R.styleable.StackLabel_textColor, textColor);
textSize = typedArray.getDimensionPixelOffset(R.styleable.StackLabel_textSize, textSize);
paddingVertical = typedArray.getDimensionPixelOffset(R.styleable.StackLabel_paddingVertical, paddingVertical);
paddingHorizontal = typedArray.getDimensionPixelOffset(R.styleable.StackLabel_paddingHorizontal, paddingHorizontal);
itemMargin = typedArray.getDimensionPixelOffset(R.styleable.StackLabel_itemMargin, itemMargin);
itemMarginVertical = typedArray.getDimensionPixelOffset(R.styleable.StackLabel_itemMarginVertical, itemMarginVertical);
itemMarginHorizontal = typedArray.getDimensionPixelOffset(R.styleable.StackLabel_itemMarginHorizontal, itemMarginHorizontal);
deleteButton = typedArray.getBoolean(R.styleable.StackLabel_deleteButton, deleteButton);
deleteButtonImage = typedArray.getResourceId(R.styleable.StackLabel_deleteButtonImage, deleteButtonImage);
labelBackground = typedArray.getResourceId(R.styleable.StackLabel_labelBackground, labelBackground);
selectMode = typedArray.getBoolean(R.styleable.StackLabel_selectMode, selectMode);
selectBackground = typedArray.getResourceId(R.styleable.StackLabel_selectBackground, selectBackground);
selectTextColor = typedArray.getColor(R.styleable.StackLabel_selectTextColor, selectTextColor);
maxSelectNum = typedArray.getInt(R.styleable.StackLabel_maxSelectNum, maxSelectNum);
minSelectNum = typedArray.getInt(R.styleable.StackLabel_minSelectNum, minSelectNum);
maxLines = typedArray.getInt(R.styleable.StackLabel_maxLines, minSelectNum);
if (minSelectNum > maxSelectNum && maxSelectNum != 0) minSelectNum = 0;
loadLabelsArray = typedArray.getString(R.styleable.StackLabel_labels);
if (selectBackground == -1) selectBackground = R.drawable.rect_label_bkg_select_normal;
if (labelBackground == -1) labelBackground = R.drawable.rect_normal_label_button;
typedArray.recycle();
} catch (Exception e) {
}
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
if (loadLabelsArray != null && !loadLabelsArray.isEmpty()) {
if (loadLabelsArray.contains(",")) {
setLabels(loadLabelsArray.split(","));
} else {
addLabel(loadLabelsArray);
}
}
}
private int dp2px(float dpValue) {
return (int) (0.5f + dpValue * Resources.getSystem().getDisplayMetrics().density);
}
private float px2dp(int pxValue) {
return (pxValue / Resources.getSystem().getDisplayMetrics().density);
}
private List<View> items;
private int newHeight = 0;
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
refreshViews();
setMeasuredDimension(getMeasuredWidth(), newHeight);//设置宽高
}
private void refreshViews() {
int maxWidth = getMeasuredWidth();
if (labels != null && !labels.isEmpty()) {
newHeight = 0;
if (items != null && !items.isEmpty()) {
int l = 0, t = 0, r = 0, b = 0, lines = 0;
for (int i = 0; i < items.size(); i++) {
View item = items.get(i);
int mWidth = MeasureSpec.makeMeasureSpec(maxWidth, MeasureSpec.AT_MOST); //AT_MOST:先按照最大宽度计算,如果小于则按实际值,如果大于,按最大宽度
int mHeight = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); //UNSPECIFIED:不确定,根据实际情况计算
item.measure(mWidth, mHeight);
int childWidth = item.getMeasuredWidth();
int childHeight = item.getMeasuredHeight();
if ((l + childWidth) > maxWidth) {
l = 0;
t = t + childHeight;
lines++;
}
if (maxLines > 0 && lines >= maxLines) {
item.setVisibility(GONE);
} else {
r = l + childWidth;
if (childWidth > maxWidth) {
r = maxWidth;
}
b = t + childHeight;
item.layout(l, t, r, b);
l = l + childWidth;
newHeight = t + childHeight;
}
}
}
}
}
public List<String> getLabels() {
return labels;
}
public StackLabel setLabels(List<String> l) {
labels = l;
reloadViews();
return this;
}
public boolean remove(int index) {
if (labels == null) {
return false;
}
labels.remove(index);
reloadViews();
return true;
}
public boolean remove(String label) {
if (labels == null) {
return false;
}
boolean flag = labels.remove(label);
reloadViews();
return flag;
}
public StackLabel setLabels(String[] arrays) {
labels = new ArrayList<>();
for (String s : arrays) {
labels.add(s);
}
reloadViews();
return this;
}
public boolean isHave(String label) {
if (labels == null) {
return false;
}
return labels.contains(label);
}
public int count() {
if (labels == null) {
return 0;
} else {
return labels.size();
}
}
public void reloadViews() {
removeAllViews();
items = new ArrayList<>();
if (labels != null && !labels.isEmpty()) {
for (int i = 0; i < labels.size(); i++) {
View item = LayoutInflater.from(context).inflate(R.layout.layout_label, null, false);
addView(item);
items.add(item);
}
initItem();
}
}
public StackLabel addLabel(String label) {
if (labels == null) labels = new ArrayList<>();
labels.add(label);
reloadViews();
return this;
}
private List<Integer> selectIndexs = new ArrayList<>();
private void initItem() {
if (labels.size() != 0) {
selectIndexs = new ArrayList<>();
for (int i = 0; i < items.size(); i++) {
View item = items.get(i);
String s = labels.get(i);
LinearLayout boxLabel = item.findViewById(R.id.box_label);
TextView txtLabel = item.findViewById(R.id.txt_label);
ImageView imgDelete = item.findViewById(R.id.img_delete);
txtLabel.setText(s);
txtLabel.setTextColor(textColor);
txtLabel.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
boxLabel.setPadding(paddingHorizontal, paddingVertical, paddingHorizontal, paddingVertical);
MarginLayoutParams p = (MarginLayoutParams) boxLabel.getLayoutParams();
if (itemMarginHorizontal == 0 && itemMarginVertical == 0) {
p.setMargins(itemMargin, itemMargin, itemMargin, itemMargin);
} else {
p.setMargins(itemMarginHorizontal, itemMarginVertical, itemMarginHorizontal, itemMarginVertical);
}
boxLabel.requestLayout();
if (deleteButton) {
imgDelete.setVisibility(VISIBLE);
} else {
imgDelete.setVisibility(GONE);
}
if (deleteButtonImage != -1) imgDelete.setImageResource(deleteButtonImage);
boxLabel.setBackgroundResource(labelBackground);
final int index = i;
boxLabel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (selectMode) {
for (View item : items) {
LinearLayout boxLabel = item.findViewById(R.id.box_label);
TextView txtLabel = item.findViewById(R.id.txt_label);
boxLabel.setBackgroundResource(labelBackground);
txtLabel.setTextColor(textColor);
txtLabel.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
}
if (selectIndexs.contains(index)) {
if (selectIndexs.size() > minSelectNum) {
int ind = 0;
for (int i = 0; i < selectIndexs.size(); i++) {
if (selectIndexs.get(i) == index) {
ind = i;
break;
}
}
selectIndexs.remove(ind);
}
} else {
if (maxSelectNum == 1) selectIndexs.clear();
if (maxSelectNum <= 0 || (maxSelectNum > 0 && selectIndexs.size() < maxSelectNum)) {
selectIndexs.add(index);
}
}
for (int index : selectIndexs) {
View item = items.get(index);
LinearLayout boxLabel = item.findViewById(R.id.box_label);
TextView txtLabel = item.findViewById(R.id.txt_label);
boxLabel.setBackgroundResource(selectBackground);
txtLabel.setTextColor(selectTextColor);
}
}
if (onLabelClickListener != null)
onLabelClickListener.onClick(index, v, labels.get(index));
}
});
if (whichIsSelected != null) {
for (String selectStr : whichIsSelected) {
if (s.equals(selectStr)) {
selectIndexs.add(index);
boxLabel.setBackgroundResource(selectBackground);
txtLabel.setTextColor(selectTextColor);
}
}
}
}
whichIsSelected = null;
}
}
public OnLabelClickListener getOnLabelClickListener() {
return onLabelClickListener;
}
public StackLabel setOnLabelClickListener(OnLabelClickListener onLabelClickListener) {
this.onLabelClickListener = onLabelClickListener;
return this;
}
public boolean isDeleteButton() {
return deleteButton;
}
public StackLabel setDeleteButton(boolean deleteButton) {
this.deleteButton = deleteButton;
initItem();
return this;
}
public boolean isSelectMode() {
return selectMode;
}
public StackLabel setSelectMode(boolean selectMode) {
this.selectMode = selectMode;
setLabels(labels);
return this;
}
private List<String> whichIsSelected; //初始化已选择列表
public StackLabel setSelectMode(boolean selectMode, List<String> whichIsSelected) {
this.selectMode = selectMode;
if (selectMode) {
this.whichIsSelected = whichIsSelected;
} else {
whichIsSelected = null;
}
setLabels(labels);
return this;
}
public int getSelectBackground() {
return selectBackground;
}
public StackLabel setSelectBackground(int selectBackground) {
this.selectBackground = selectBackground;
setLabels(labels);
return this;
}
public int getSelectTextColor() {
return selectTextColor;
}
public StackLabel setSelectTextColor(int selectTextColor) {
this.selectTextColor = selectTextColor;
return this;
}
public int getMaxSelectNum() {
return maxSelectNum;
}
public StackLabel setMaxSelectNum(int maxSelectNum) {
this.maxSelectNum = maxSelectNum;
setLabels(labels);
return this;
}
public List<Integer> getSelectIndexList() {
return selectIndexs;
}
public int[] getSelectIndexArray() {
int[] arrays = new int[selectIndexs.size()];
for (int i = 0; i < selectIndexs.size(); i++) {
arrays[i] = selectIndexs.get(i);
}
return arrays;
}
public int getTextColor() {
return textColor;
}
public StackLabel setTextColor(int textColor) {
this.textColor = textColor;
setLabels(labels);
return this;
}
public int getTextSize() {
return textSize;
}
public StackLabel setTextSize(int textSize) {
this.textSize = textSize;
setLabels(labels);
return this;
}
public int getPaddingVertical() {
return paddingVertical;
}
public StackLabel setPaddingVertical(int paddingVertical) {
this.paddingVertical = paddingVertical;
setLabels(labels);
return this;
}
public int getPaddingHorizontal() {
return paddingHorizontal;
}
public StackLabel setPaddingHorizontal(int paddingHorizontal) {
this.paddingHorizontal = paddingHorizontal;
setLabels(labels);
return this;
}
public int getItemMargin() {
return itemMargin;
}
public StackLabel setItemMargin(int itemMargin) {
this.itemMargin = itemMargin;
setLabels(labels);
return this;
}
public int getItemMarginVertical() {
return itemMarginVertical;
}
public StackLabel setItemMarginVertical(int itemMarginVertical) {
this.itemMarginVertical = itemMarginVertical;
setLabels(labels);
return this;
}
public int getItemMarginHorizontal() {
return itemMarginHorizontal;
}
public StackLabel setItemMarginHorizontal(int itemMarginHorizontal) {
this.itemMarginHorizontal = itemMarginHorizontal;
setLabels(labels);
return this;
}
public int getMaxLines() {
return maxLines;
}
public StackLabel setMaxLines(int maxLines) {
this.maxLines = maxLines;
return this;
}
private void log(Object s) {
Log.i(">>>", s.toString());
}
}