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~