MultiRowRadioGroup.java
10.8 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
package com.cnlive.goldenline.ui.widget;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.os.Build;
import android.support.annotation.IdRes;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Key on 2015/8/13 12:29
* email: MrKey.K@gmail.com
* description: 重写原生RadioGroup,用集合实现RadioGroup内可以再布局,从而实现多行多列的RadioGroup
* {getAllRadioButton}
* {addView}
*
* @version 1.2
* {@link PassThroughHierarchyChangeListener }
*/
public class MultiRowRadioGroup extends LinearLayout {
// holds the checked id; the selection is empty by default
private int mCheckedId = -1;
// tracks children radio buttons checked state
private CompoundButton.OnCheckedChangeListener mChildOnCheckedChangeListener;
// when true, mOnCheckedChangeListener discards events
private boolean mProtectFromCheckedChange = false;
private OnCheckedChangeListener mOnCheckedChangeListener;
private PassThroughHierarchyChangeListener mPassThroughListener;
public MultiRowRadioGroup(Context context) {
super(context);
setOrientation(VERTICAL);
init();
}
public MultiRowRadioGroup(Context context, AttributeSet attrs) {
super(context, attrs);
// retrieve selected radio button as requested by the user in the
// XML layout file
// com.android.internal.R.styleable.RadioGroup
// com.android.internal.R.attr.radioButtonStyle
TypedArray attributes = context.obtainStyledAttributes(attrs,
new int[]{Resources.getSystem().getIdentifier("RadioGroup", "styleable", "android")},
Resources.getSystem().getIdentifier("radioButtonStyle", "attr", "android"), 0);
// com.android.internal.R.styleable.RadioGroup_checkedButton
int value = attributes.getResourceId(Resources.getSystem().getIdentifier("RadioGroup_checkedButton", "styleable", "android"), View.NO_ID);
if (value != View.NO_ID) {
mCheckedId = value;
}
// com.android.internal.R.styleable.RadioGroup_orientation
final int index = attributes.getInt(Resources.getSystem().getIdentifier("RadioGroup_orientation", "styleable", "android"), VERTICAL);
if (index == LinearLayout.HORIZONTAL || index == LinearLayout.VERTICAL)
setOrientation(index);
attributes.recycle();
init();
}
private void init() {
mChildOnCheckedChangeListener = new CheckedStateTracker();
mPassThroughListener = new PassThroughHierarchyChangeListener();
super.setOnHierarchyChangeListener(mPassThroughListener);
}
@Override
public void setOnHierarchyChangeListener(OnHierarchyChangeListener listener) {
// the user listener is delegated to our pass-through listener
mPassThroughListener.mOnHierarchyChangeListener = listener;
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
// checks the appropriate radio button as requested in the XML file
if (mCheckedId != -1) {
mProtectFromCheckedChange = true;
setCheckedStateForView(mCheckedId, true);
mProtectFromCheckedChange = false;
setCheckedId(mCheckedId);
}
}
/**
* 每次添加view 就递归寻找是否含有RadioButton
*/
@Override
public void addView(View child, int index, ViewGroup.LayoutParams params) {
List<RadioButton> allRadioButton = getAllRadioButton(child);
if (allRadioButton != null && allRadioButton.size() > 0) {
for (RadioButton button : allRadioButton) {
if (button.isChecked()) {
mProtectFromCheckedChange = true;
if (mCheckedId != -1) {
setCheckedStateForView(mCheckedId, false);
}
mProtectFromCheckedChange = false;
setCheckedId(button.getId());
}
}
}
super.addView(child, index, params);
}
/**
* 递归寻找{@link RadioButton}
*/
private List<RadioButton> getAllRadioButton(View child) {
List<RadioButton> allRadioButton = new ArrayList<>();
if (child instanceof RadioButton) {
allRadioButton.add((RadioButton) child);
} else if (child instanceof ViewGroup) {
int counts = ((ViewGroup) child).getChildCount();
for (int i = 0; i < counts; i++) {
// 递归,把返回集合中的内容全部加入到外层的集合,然后返回
allRadioButton.addAll(getAllRadioButton(((ViewGroup) child).getChildAt(i)));
}
}
return allRadioButton;
}
public void check(@IdRes int id) {
// don't even bother
if (id != -1 && (id == mCheckedId)) {
return;
}
if (mCheckedId != -1) {
setCheckedStateForView(mCheckedId, false);
}
if (id != -1) {
setCheckedStateForView(id, true);
}
setCheckedId(id);
}
private void setCheckedId(@IdRes int id) {
mCheckedId = id;
if (mOnCheckedChangeListener != null) {
mOnCheckedChangeListener.onCheckedChanged(this, mCheckedId);
}
}
private void setCheckedStateForView(int viewId, boolean checked) {
View checkedView = findViewById(viewId);
if (checkedView != null && checkedView instanceof RadioButton) {
((RadioButton) checkedView).setChecked(checked);
}
}
@IdRes
public int getCheckedRadioButtonId() {
return mCheckedId;
}
public void clearCheck() {
check(-1);
}
public void setOnCheckedChangeListener(OnCheckedChangeListener listener) {
mOnCheckedChangeListener = listener;
}
@Override
public LayoutParams generateLayoutParams(AttributeSet attrs) {
return new LayoutParams(getContext(), attrs);
}
@Override
protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
return p instanceof LayoutParams;
}
@Override
protected LinearLayout.LayoutParams generateDefaultLayoutParams() {
return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
}
@Override
public CharSequence getAccessibilityClassName() {
return MultiRowRadioGroup.class.getName();
}
public static class LayoutParams extends LinearLayout.LayoutParams {
public LayoutParams(Context c, AttributeSet attrs) {
super(c, attrs);
}
public LayoutParams(int w, int h) {
super(w, h);
}
public LayoutParams(int w, int h, float initWeight) {
super(w, h, initWeight);
}
public LayoutParams(ViewGroup.LayoutParams p) {
super(p);
}
public LayoutParams(MarginLayoutParams source) {
super(source);
}
@Override
protected void setBaseAttributes(TypedArray a, int widthAttr, int heightAttr) {
if (a.hasValue(widthAttr)) {
width = a.getLayoutDimension(widthAttr, "layout_width");
} else {
width = WRAP_CONTENT;
}
if (a.hasValue(heightAttr)) {
height = a.getLayoutDimension(heightAttr, "layout_height");
} else {
height = WRAP_CONTENT;
}
}
}
public interface OnCheckedChangeListener {
public void onCheckedChanged(MultiRowRadioGroup group, @IdRes int checkedId);
}
private class CheckedStateTracker implements CompoundButton.OnCheckedChangeListener {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// prevents from infinite recursion
if (mProtectFromCheckedChange) {
return;
}
mProtectFromCheckedChange = true;
if (mCheckedId != -1) {
setCheckedStateForView(mCheckedId, false);
}
mProtectFromCheckedChange = false;
int id = buttonView.getId();
setCheckedId(id);
}
}
/**
* 从集合中去获取,不重写之前多行多列等复杂布局只会返回-1,所以实现的重点还是在于重写这个监听,递归获取
*/
private class PassThroughHierarchyChangeListener implements
OnHierarchyChangeListener {
private OnHierarchyChangeListener mOnHierarchyChangeListener;
@SuppressLint("NewApi")
public void onChildViewAdded(View parent, View child) {
if (parent == MultiRowRadioGroup.this) {
List<RadioButton> allRadioButton = getAllRadioButton(child);
if (allRadioButton != null && allRadioButton.size() > 0) {
for (RadioButton button : allRadioButton) {
int id = button.getId();
// generates an id if it's missing
if (id == View.NO_ID) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
// generateViewId() 这个方法最低需要API17 这个方法用于生成ID , 这里做一下向下兼容
id = View.generateViewId();
} else {
id = child.hashCode();
}
button.setId(id);
}
// 不知道为什么setOnCheckedChangeWidgetListener方法不能调 RadioButton 的父类CompoundButton中是有这个方法的
button.setOnCheckedChangeListener(
mChildOnCheckedChangeListener);
}
}
}
if (mOnHierarchyChangeListener != null) {
mOnHierarchyChangeListener.onChildViewAdded(parent, child);
}
}
public void onChildViewRemoved(View parent, View child) {
if (parent == MultiRowRadioGroup.this) {
List<RadioButton> allRadioButton = getAllRadioButton(child);
if (allRadioButton != null && allRadioButton.size() > 0) {
for (RadioButton button : allRadioButton) {
button.setOnCheckedChangeListener(null);
}
}
}
if (mOnHierarchyChangeListener != null) {
mOnHierarchyChangeListener.onChildViewRemoved(parent, child);
}
}
}
}