14
14
15
15
import static com .donkingliang .labelsviewdemo .R .id .labels ;
16
16
17
- public class MainActivity extends AppCompatActivity implements View . OnClickListener {
17
+ public class MainActivity extends AppCompatActivity implements LabelsView . OnLabelClickListener {
18
18
19
+ private LabelsView btnLabels ;
19
20
private LabelsView labelsView ;
20
21
21
22
@ Override
22
23
protected void onCreate (Bundle savedInstanceState ) {
23
24
super .onCreate (savedInstanceState );
24
25
setContentView (R .layout .activity_main );
25
-
26
- labelsView = (LabelsView ) findViewById (labels );
26
+ btnLabels = findViewById (R .id .btnLabels );
27
+ labelsView = findViewById (labels );
28
+
29
+ // 按钮组
30
+ ArrayList <String > btns = new ArrayList <>();
31
+ btns .add ("不可选中" );
32
+ btns .add ("单选(可反选)" );
33
+ btns .add ("单选(不可反选)" );
34
+ btns .add ("多选" );
35
+ btns .add ("多选(最多5个)" );
36
+ btns .add ("多选(最少1个)" );
37
+ btns .add ("多选(1,2必选)" );
38
+ btns .add ("指示器模式" );
39
+ btns .add ("取消选中" );
40
+ btns .add ("点击" );
41
+ btnLabels .setLabels (btns );
42
+ btnLabels .setOnLabelClickListener (this );
27
43
28
44
//测试的数据
29
45
// ArrayList<String> label = new ArrayList<>();
@@ -54,66 +70,6 @@ protected void onCreate(Bundle savedInstanceState) {
54
70
testList .add (new TestBean ("PHP" , 10 ));
55
71
testList .add (new TestBean ("Python" , 11 ));
56
72
testList .add (new TestBean ("Swift" , 12 ));
57
- testList .add (new TestBean ("Android" , 1 ));
58
- testList .add (new TestBean ("IOS" , 2 ));
59
- testList .add (new TestBean ("前端" , 3 ));
60
- testList .add (new TestBean ("后台" , 4 ));
61
- testList .add (new TestBean ("微信开发" , 5 ));
62
- testList .add (new TestBean ("游戏开发" , 6 ));
63
- testList .add (new TestBean ("Java" , 7 ));
64
- testList .add (new TestBean ("JavaScript" , 8 ));
65
- testList .add (new TestBean ("C++" , 9 ));
66
- testList .add (new TestBean ("PHP" , 10 ));
67
- testList .add (new TestBean ("Python" , 11 ));
68
- testList .add (new TestBean ("Swift" , 12 ));
69
- testList .add (new TestBean ("Android" , 1 ));
70
- testList .add (new TestBean ("IOS" , 2 ));
71
- testList .add (new TestBean ("前端" , 3 ));
72
- testList .add (new TestBean ("后台" , 4 ));
73
- testList .add (new TestBean ("微信开发" , 5 ));
74
- testList .add (new TestBean ("游戏开发" , 6 ));
75
- testList .add (new TestBean ("Java" , 7 ));
76
- testList .add (new TestBean ("JavaScript" , 8 ));
77
- testList .add (new TestBean ("C++" , 9 ));
78
- testList .add (new TestBean ("PHP" , 10 ));
79
- testList .add (new TestBean ("Python" , 11 ));
80
- testList .add (new TestBean ("Swift" , 12 ));
81
- testList .add (new TestBean ("Android" , 1 ));
82
- testList .add (new TestBean ("IOS" , 2 ));
83
- testList .add (new TestBean ("前端" , 3 ));
84
- testList .add (new TestBean ("后台" , 4 ));
85
- testList .add (new TestBean ("微信开发" , 5 ));
86
- testList .add (new TestBean ("游戏开发" , 6 ));
87
- testList .add (new TestBean ("Java" , 7 ));
88
- testList .add (new TestBean ("JavaScript" , 8 ));
89
- testList .add (new TestBean ("C++" , 9 ));
90
- testList .add (new TestBean ("PHP" , 10 ));
91
- testList .add (new TestBean ("Python" , 11 ));
92
- testList .add (new TestBean ("Swift" , 12 ));
93
- testList .add (new TestBean ("Android" , 1 ));
94
- testList .add (new TestBean ("IOS" , 2 ));
95
- testList .add (new TestBean ("前端" , 3 ));
96
- testList .add (new TestBean ("后台" , 4 ));
97
- testList .add (new TestBean ("微信开发" , 5 ));
98
- testList .add (new TestBean ("游戏开发" , 6 ));
99
- testList .add (new TestBean ("Java" , 7 ));
100
- testList .add (new TestBean ("JavaScript" , 8 ));
101
- testList .add (new TestBean ("C++" , 9 ));
102
- testList .add (new TestBean ("PHP" , 10 ));
103
- testList .add (new TestBean ("Python" , 11 ));
104
- testList .add (new TestBean ("Swift" , 12 ));
105
- testList .add (new TestBean ("Android" , 1 ));
106
- testList .add (new TestBean ("IOS" , 2 ));
107
- testList .add (new TestBean ("前端" , 3 ));
108
- testList .add (new TestBean ("后台" , 4 ));
109
- testList .add (new TestBean ("微信开发" , 5 ));
110
- testList .add (new TestBean ("游戏开发" , 6 ));
111
- testList .add (new TestBean ("Java" , 7 ));
112
- testList .add (new TestBean ("JavaScript" , 8 ));
113
- testList .add (new TestBean ("C++" , 9 ));
114
- testList .add (new TestBean ("PHP" , 10 ));
115
- testList .add (new TestBean ("Python" , 11 ));
116
- testList .add (new TestBean ("Swift" , 12 ));
117
73
118
74
labelsView .setLabels (testList , new LabelsView .LabelTextProvider <TestBean >() {
119
75
@ Override
@@ -126,75 +82,66 @@ public CharSequence getLabelText(TextView label, int position, TestBean data) {
126
82
// labelsView.setMaxLines(1);
127
83
128
84
labelsView .clearAllSelect ();
129
-
130
- findViewById (R .id .btn_none ).setOnClickListener (this );
131
- findViewById (R .id .btn_single ).setOnClickListener (this );
132
- findViewById (R .id .btn_multi ).setOnClickListener (this );
133
- findViewById (R .id .btn_multi_5 ).setOnClickListener (this );
134
- findViewById (R .id .btn_un_select ).setOnClickListener (this );
135
- findViewById (R .id .btn_click ).setOnClickListener (this );
136
- findViewById (R .id .btn_single_irrevocably ).setOnClickListener (this );
137
- findViewById (R .id .btn_multi_compulsory ).setOnClickListener (this );
138
- findViewById (R .id .btn_multi_1 ).setOnClickListener (this );
139
- findViewById (R .id .btn_indicator ).setOnClickListener (this );
140
85
}
141
86
142
87
@ Override
143
- public void onClick ( View v ) {
88
+ public void onLabelClick ( TextView label , Object data , int position ) {
144
89
labelsView .setOnLabelClickListener (null );
145
- switch (v .getId ()) {
146
- case R .id .btn_none :
90
+ labelsView .clearCompulsorys ();
91
+ switch (position ) {
92
+ case 0 :
147
93
labelsView .setSelectType (LabelsView .SelectType .NONE );
148
94
break ;
149
95
150
- case R . id . btn_single :
96
+ case 1 :
151
97
labelsView .setSelectType (LabelsView .SelectType .SINGLE );
152
98
break ;
153
99
154
- case R . id . btn_single_irrevocably :
100
+ case 2 :
155
101
labelsView .setSelectType (LabelsView .SelectType .SINGLE_IRREVOCABLY );
156
102
break ;
157
103
158
- case R . id . btn_multi :
104
+ case 3 :
159
105
labelsView .setSelectType (LabelsView .SelectType .MULTI );
160
106
labelsView .setMaxSelect (0 );
161
107
labelsView .setMinSelect (0 );
162
108
break ;
163
109
164
- case R . id . btn_multi_5 :
110
+ case 4 :
165
111
labelsView .setSelectType (LabelsView .SelectType .MULTI );
166
112
labelsView .setMaxSelect (5 );
167
113
labelsView .setMinSelect (0 );
168
114
break ;
169
115
170
- case R . id . btn_multi_1 :
116
+ case 5 :
171
117
labelsView .setSelectType (LabelsView .SelectType .MULTI );
172
118
labelsView .setMaxSelect (0 );
173
119
labelsView .setMinSelect (1 );
174
120
break ;
175
121
176
- case R . id . btn_multi_compulsory :
122
+ case 6 :
177
123
labelsView .setSelectType (LabelsView .SelectType .MULTI );
178
124
labelsView .setMaxSelect (0 );
179
125
labelsView .setMinSelect (0 );
180
126
labelsView .setCompulsorys (0 , 1 );
181
127
break ;
182
128
183
- case R .id .btn_indicator :
184
-
129
+ case 7 :
185
130
labelsView .setIndicator (!labelsView .isIndicator ());
186
131
if (labelsView .isIndicator ()) {
187
- ((TextView ) v ).setText ("取消指示器模式" );
132
+ btnLabels .getLabels ().set (position , "取消指示器模式" );
133
+ label .setText ("取消指示器模式" );
188
134
} else {
189
- ((TextView ) v ).setText ("指示器模式" );
135
+ btnLabels .getLabels ().set (position , "指示器模式" );
136
+ label .setText ("指示器模式" );
190
137
}
191
138
break ;
192
139
193
- case R . id . btn_un_select :
140
+ case 8 :
194
141
labelsView .clearAllSelect ();
195
142
break ;
196
143
197
- case R . id . btn_click :
144
+ case 9 :
198
145
labelsView .setSelectType (LabelsView .SelectType .NONE );
199
146
labelsView .setOnLabelClickListener (new LabelsView .OnLabelClickListener () {
200
147
@ Override
0 commit comments