废话不说直接代码,先上车,等项目不忙了再解释
public class GridRadioGroup extends GridLayout {
private CompoundButton.OnCheckedChangeListener mChildOnCheckedChangeListener;
private boolean mProtectFromCheckedChange = false;
private PassThroughHierarchyChangeListener mPassThroughListener;
private int mCheckedId = -1;
public GridRadioGroup(Context context) {
super(context);
}
public GridRadioGroup(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
@Override
public void addView(View child, int index, ViewGroup.LayoutParams params) {
if (child instanceof RadioButton) {
final RadioButton button = (RadioButton) child;
if (button.isChecked()) {
mProtectFromCheckedChange = true;
if (mCheckedId != -1) {
setCheckedStateForView(mCheckedId, false);
}
mProtectFromCheckedChange = false;
setCheckedId(button.getId());
}
}
super.addView(child, index, params);
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
if (mCheckedId != -1) {
mProtectFromCheckedChange = true;
setCheckedStateForView(mCheckedId, true);
mProtectFromCheckedChange = false;
setCheckedId(mCheckedId);
}
}
private void setCheckedStateForView(int viewId, boolean checked) {
View checkedView = findViewById(viewId);
if (checkedView != null && checkedView instanceof RadioButton) {
((RadioButton) checkedView).setChecked(checked);
}
}
private void setCheckedId(@IdRes int id) {
mCheckedId = id;
}
private class PassThroughHierarchyChangeListener implements
ViewGroup.OnHierarchyChangeListener {
private ViewGroup.OnHierarchyChangeListener mOnHierarchyChangeListener;
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
public void onChildViewAdded(View parent, View child) {
if (parent == GridRadioGroup.this && child instanceof RadioButton) {
int id = child.getId();
if (id == View.NO_ID) {
id = View.generateViewId();
child.setId(id);
}
((RadioButton) child).setOnCheckedChangeListener(
mChildOnCheckedChangeListener);
}
if (mOnHierarchyChangeListener != null) {
mOnHierarchyChangeListener.onChildViewAdded(parent, child);
}
}
public void onChildViewRemoved(View parent, View child) {
if (parent == GridRadioGroup.this && child instanceof RadioButton) {
((RadioButton) child).setOnCheckedChangeListener(null);
}
if (mOnHierarchyChangeListener != null) {
mOnHierarchyChangeListener.onChildViewRemoved(parent, child);
}
}
}
private void init() {
mChildOnCheckedChangeListener = new CheckedStateTracker();
mPassThroughListener = new PassThroughHierarchyChangeListener();
super.setOnHierarchyChangeListener(mPassThroughListener);
}
private class CheckedStateTracker implements CompoundButton.OnCheckedChangeListener {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (mProtectFromCheckedChange) {
return;
}
mProtectFromCheckedChange = true;
if (mCheckedId != -1) {
setCheckedStateForView(mCheckedId, false);
}
mProtectFromCheckedChange = false;
int id = buttonView.getId();
setCheckedId(id);
}
}
@Override
public void setOnHierarchyChangeListener(OnHierarchyChangeListener listener) {
mPassThroughListener.mOnHierarchyChangeListener = listener;
}
}
使用方法
<com.payne.app.GridRadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:columnCount="5"
android:rowCount="2">
<RadioButton
android:checked="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</com.payne.app.GridRadioGroup>
效果图
Paste_Image.png
网友评论