可無(wú)限嵌套選擇的RadioGroup,以及可任意定義布局的RadioButton

NestedSelectionRadioGroup

github地址:https://github.com/LatoAndroid/NestedSelectionRadioGroup

優(yōu)勢(shì)

  • NestedSelectionRadioGroup:可嵌套ViewGroup,可多層嵌套,可多行單選

  • NestedRadioLayout:繼承RelativeLayout的RadioButton,這樣你可以定義UI更豐富的RadioButton,更方便的是RadioButton的子控件也會(huì)完全跟隨NestedRadioLayout的選中狀態(tài),不需要你做任何處理

項(xiàng)目來(lái)源

之前在做一個(gè)需求的時(shí)候,產(chǎn)品經(jīng)理要求在不同的列,不同的行的一些控件全部聯(lián)動(dòng)單選,而且每一個(gè)控件的樣式都不一樣,另外,在這些控件內(nèi)部,子控件也要跟隨變化。所以就有了這個(gè)庫(kù)

網(wǎng)上有很多類(lèi)似的文章,但我看了幾篇和幾個(gè)github分享后,一是感覺(jué)不夠全面(無(wú)法滿足所有需求),二是引入的RadioGroup太老,三是都是在RadioButton上面做文章,我無(wú)法定義UI更豐富的RadioButton,所以后面懶得找了就自己實(shí)現(xiàn)了

這個(gè)庫(kù)的很大一部分代碼來(lái)自RadioGroup、RadioButton和CompoundLayout,但是他們要不就是無(wú)法嵌套,要不就無(wú)法多行單選,所以在他的基礎(chǔ)上進(jìn)行了一些修改與適配

項(xiàng)目效果

Image preview

Image preview2

項(xiàng)目用法

1.NestedRadioGroup繼承LinearLayout,必須作為整體的父控件

2.如果子控件需要被選中,放入NestedRadioLayout中就可以了,其他不用處理

3.其他用法和radiogroup類(lèi)似,每個(gè)NestedRadioLayout都有自己的setOnCheckedChangeListener方法

引入:

    compile 'com.kyle.nestedradiogrouplib:radiogrouplib:1.0.1'

在布局中

<com.kyle.radiogrouplib.NestedRadioGroup
        android:id="@+id/nestedGroup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="不進(jìn)行選擇的text頭部"/>

        <!--extends Relativelayout-->
        <com.kyle.radiogrouplib.NestedRadioLayout
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:background="@drawable/selector_solid_f5f5f9_corner_1dp_solod_ebf1ff_corner_1dp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:text="test"
                android:textColor="@color/selector_47aefe_3f3f3f"/>
        </com.kyle.radiogrouplib.NestedRadioLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <com.kyle.radiogrouplib.NestedRadioLayout
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:background="@drawable/selector_solid_f5f5f9_corner_1dp_solod_ebf1ff_corner_1dp">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:text="test"
                    android:textColor="@color/selector_47aefe_3f3f3f"/>
            </com.kyle.radiogrouplib.NestedRadioLayout>
        </LinearLayout>

</com.kyle.radiogrouplib.NestedRadioGroup>

如果需要選擇效果,需要寫(xiě)帶selected的drawble文件

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/pop_type_selected" android:state_selected="true"/>
    <item android:drawable="@drawable/pop_type_not_selected"/>
</selector>

監(jiān)聽(tīng)事件

group.setOnCheckedChangeListener(new NestedRadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(NestedRadioGroup group, int checkedId) {
                Log.d("MainActivity", checkedId + "");
            }
        });

private void bindListener(final NestedRadioLayout compoundLayout) {
        compoundLayout.setOnCheckedChangeListener(new NestedRadioLayout.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(BaseRadioLayout baseRadioLayout, boolean checked) {
                if (checked) {
                    if (compoundLayout.getId() == R.id.rl_start_time) {

                    } else if (compoundLayout.getId() == R.id.rl_end_time) {

                    } else if (compoundLayout.getId() == R.id.rl_week) {


                    } else if (compoundLayout.getId() == R.id.rl_month) {

                    } else if (compoundLayout.getId() == R.id.rl_three_month) {

                    }
                }
            }


        });
    }

我的簡(jiǎn)書(shū):http://www.itdecent.cn/u/bb187b559365

我的掘金:https://juejin.im/user/58cd4b02128fe1006c8e8b81/posts

我的github:https://github.com/LatoAndroid

主要思路來(lái)自:https://github.com/Jaouan/CompoundLayout以及https://github.com/fodroid/XRadioGroup

歡迎star~

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

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

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