github地址:github.com/LatoAndroid…android
NestedSelectionRadioGroup:可嵌套ViewGroup,可多層嵌套,可多行單選git
NestedRadioLayout:繼承RelativeLayout的RadioButton,這樣你能夠定義UI更豐富的RadioButton,更方便的是RadioButton的子控件也會徹底跟隨NestedRadioLayout的選中狀態,不須要你作任何處理github
以前在作一個需求的時候,產品經理要求在不一樣的列,不一樣的行的一些控件所有聯動單選,並且每個控件的樣式都不同,另外,在這些控件內部,子控件也要跟隨變化。因此就有了這個庫bash
網上有不少相似的文章,但我看了幾篇和幾個github分享後,一是感受不夠全面(沒法知足全部需求),二是引入的RadioGroup太老,三是都是在RadioButton上面作文章,我沒法定義UI更豐富的RadioButton,因此後面懶得找了就本身實現了ide
這個庫的很大一部分代碼來自RadioGroup、RadioButton和CompoundLayout,可是他們要不就是沒法嵌套,要不就沒法多行單選,因此在他的基礎上進行了一些修改與適配佈局
1.NestedRadioGroup繼承LinearLayout,必須做爲總體的父控件post
2.若是子控件須要被選中,放入NestedRadioLayout中就能夠了,其餘不用處理ui
3.其餘用法和radiogroup相似,每一個NestedRadioLayout都有本身的setOnCheckedChangeListener方法spa
引入:code
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="不進行選擇的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>
複製代碼
若是須要選擇效果,須要寫帶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>
複製代碼
監聽事件
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) {
}
}
}
});
}
複製代碼
個人github:github.com/LatoAndroid
歡迎star~