可以設(shè)置行列數(shù)的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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 小輝蹲下身子,小光也蹲下,前者灑出尿液,后者拉出一小坨一小坨糞便。 出于調(diào)皮,他拿出一把掃帚,先幫哥哥的那一小潭液...
    昆悠閱讀 299評論 0 3
  • 上次寫下感受是上一年的八月三十。到今天已經(jīng)過去五個月了,回想起來,一是感覺時間過的真快,而是忽然發(fā)現(xiàn),我還是那個時...
    蒙昧無知閱讀 246評論 0 0
  • 上周五晚自習(xí),我們進(jìn)行了九年級上冊語文綜合檢測,我把試卷評改完后,又把學(xué)生們的作文挑出來,把他們文章中的能表達(dá)...
    與夢同行閱讀 419評論 0 1

友情鏈接更多精彩內(nèi)容