可以设置行列数的RadioGroup

废话不说直接代码,先上车,等项目不忙了再解释


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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 小辉蹲下身子,小光也蹲下,前者洒出尿液,后者拉出一小坨一小坨粪便。 出于调皮,他拿出一把扫帚,先帮哥哥的那一小潭液...
    昆悠阅读 1,735评论 0 3
  • 上次写下感受是上一年的八月三十。到今天已经过去五个月了,回想起来,一是感觉时间过的真快,而是忽然发现,我还是那个时...
    蒙昧无知阅读 1,547评论 0 0
  • 上周五晚自习,我们进行了九年级上册语文综合检测,我把试卷评改完后,又把学生们的作文挑出来,把他们文章中的能表达...
    与梦同行阅读 2,828评论 0 1