Last active
March 17, 2016 15:04
-
-
Save hrules6872/042b7936c34940f844b6 to your computer and use it in GitHub Desktop.
AdvancedRadioGroup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AdvancedRadioGroup extends LinearLayout { | |
private int radioButtonCheckedId; | |
private boolean working; | |
private RadioGroup.OnCheckedChangeListener listener; | |
public AdvancedRadioGroup(Context context) { | |
this(context, null); | |
} | |
public AdvancedRadioGroup(Context context, AttributeSet attrs) { | |
this(context, attrs, 0); | |
} | |
public AdvancedRadioGroup(Context context, AttributeSet attrs, int defStyleAttr) { | |
super(context, attrs, defStyleAttr); | |
init(context, attrs); | |
} | |
@TargetApi(Build.VERSION_CODES.LOLLIPOP) | |
public AdvancedRadioGroup(Context context, AttributeSet attrs, int defStyleAttr, | |
int defStyleRes) { | |
super(context, attrs, defStyleAttr, defStyleRes); | |
init(context, attrs); | |
} | |
private void init(Context context, AttributeSet attrs) { | |
final TypedArray attributes = | |
context.obtainStyledAttributes(attrs, R.styleable.AdvancedRadioGroup); | |
radioButtonCheckedId = | |
attributes.getResourceId(R.styleable.AdvancedRadioGroup_checkedButton, View.NO_ID); | |
attributes.recycle(); | |
working = false; | |
super.setOnHierarchyChangeListener(hierarchyChangeListener); | |
} | |
private final CompoundButton.OnCheckedChangeListener checkedChangeListener = | |
new CompoundButton.OnCheckedChangeListener() { | |
@Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { | |
if (isChecked && !working) { | |
int checkedId = buttonView.getId(); | |
if (listener != null) { | |
listener.onCheckedChanged(null, checkedId); | |
} | |
check(checkedId); | |
} | |
} | |
}; | |
private final OnHierarchyChangeListener hierarchyChangeListener = | |
new OnHierarchyChangeListener() { | |
@Override public void onChildViewAdded(View parent, View child) { | |
ArrayList<View> views = getAllChildren(parent); | |
for (View view : views) { | |
if (view instanceof RadioButton) { | |
if (view.getId() == radioButtonCheckedId) { | |
((RadioButton) view).setChecked(true); | |
} | |
((RadioButton) view).setOnCheckedChangeListener(checkedChangeListener); | |
} | |
} | |
} | |
@Override public void onChildViewRemoved(View parent, View child) { | |
} | |
}; | |
public @IdRes int getCheckedRadioButtonId() { | |
return radioButtonCheckedId; | |
} | |
public void check(@IdRes int radioButtonResId) { | |
this.radioButtonCheckedId = radioButtonResId; | |
working = true; | |
ArrayList<View> views = getAllChildren(this); | |
for (View view : views) { | |
if (view instanceof RadioButton) { | |
((RadioButton) view).setChecked(view.getId() == radioButtonResId); | |
} | |
} | |
working = false; | |
} | |
public void check(@NonNull RadioButton radioButton) { | |
check(radioButton.getId()); | |
} | |
public void clearCheck() { | |
ArrayList<View> views = getAllChildren(this); | |
for (View view : views) { | |
if (view instanceof RadioButton) { | |
((RadioButton) view).setChecked(false); | |
} | |
} | |
} | |
public void setOnCheckedChangeListener(RadioGroup.OnCheckedChangeListener listener) { | |
this.listener = listener; | |
} | |
private ArrayList<View> getAllChildren(View view) { | |
ArrayList<View> views = new ArrayList<>(); | |
if (!(view instanceof ViewGroup)) { | |
views.add(view); | |
return views; | |
} | |
ViewGroup viewGroup = (ViewGroup) view; | |
for (int i = 0; i < viewGroup.getChildCount(); i++) { | |
View child = viewGroup.getChildAt(i); | |
views.add(view); | |
views.addAll(getAllChildren(child)); | |
} | |
return views; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment