Android TeaPickerView数据级联选择器
程序员文章站
2022-03-18 16:24:47
Github地址 YangsBryant/TeaPickerView(Github排版比较好,建议进入这里查看详情,如果觉得好,点个star吧!) 引入module 主要代码 TeaPickerView属性大全 方法名 setHeights(int mHeight) 设置数据 给出参考bean地址 ......
github地址
yangsbryant/teapickerview
(github排版比较好,建议进入这里查看详情,如果觉得好,点个star吧!)
引入module
allprojects {
repositories {
google()
jcenter()
maven { url 'https://www.jitpack.io' }
}
}
implementation 'com.github.yangsbryant:teapickerview:1.0.2'
主要代码
public class mainactivity extends appcompatactivity {
@bindview(r.id.mbutton)
button button;
list<string> mprovincedatas=new arraylist<>();
map<string, list<string>> mseconddatas= new hashmap<>();
map<string, list<string>> mthirddatas= new hashmap<>();
@override
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.activity_main);
butterknife.bind( this );
intipickerview();
}
private void intipickerview(){
//一级列表
provincebean provincebean = new provincebean();
mprovincedatas.addall(provincebean.getrepdata().getprovince());
//二级列表
secondbean secondbean = new secondbean();
mseconddatas.putall(secondbean.getrepdata().getsecond());
//三级列表
thirdbean thirdbean = new thirdbean();
mthirddatas.putall(thirdbean.getrepdata().getthird());
log.i("json", jsonarrayutil.tojson(mprovincedatas));
log.i("json",jsonarrayutil.tojson(mseconddatas));
log.i("json",jsonarrayutil.tojson(mthirddatas));
//设置数据有多少层级
pickerdata data=new pickerdata();
data.setfirstdatas(mprovincedatas);//json: ["广东","江西"]
data.setseconddatas(mseconddatas);//json: {"江西":["南昌","赣州"],"广东":["广州","深圳","佛山","东莞"]}
data.setthirddatas(mthirddatas);//json: {"广州":["天河区","白云区","番禹区","花都区"],"赣州":["章贡区","黄金开发区"],"东莞":["东城","南城"],"深圳":["南山区","宝安区","龙华区"],"佛山":["禅城区","顺德区"],"南昌":["东湖区","青云谱区","青山湖区"]}
data.setinitselecttext("请选择");
teapickerview teapickerview =new teapickerview(this,data);
teapickerview.setscreenh(3)
.setdiscolourhook(true)
.setradius(25)
.setcontentline(true)
.setradius(25)
.build();
button.setonclicklistener(v -> {
//显示选择器
teapickerview.show(button);
});
//选择器点击事件
teapickerview.setonpickerclicklistener(pickerdata -> {
toast.maketext(mainactivity.this,pickerdata.getfirsttext()+","+pickerdata.getsecondtext()+","+pickerdata.getthirdtext(),toast.length_short).show();
teapickerview.dismiss();//关闭选择器
});
}
}
teapickerview属性大全
方法名 |
属性 |
setheights(int mheight) |
显示具体的高度(dp),设置0是自适应(高度没有默认值,需要主动设置) |
setscreenh(int num) | 显示的高度占屏幕的百分比 |
setbackground(int color) | 设置整体的背景颜色 默认是#ffffff |
setradius(int mradius) | 设置圆角,默认0 |
setcontentbackground(int color) | 内容栏的背景颜色 默认是#ffffff |
setcontentheight(int mheight) | 内容栏的高度(dp) 默认是50dp |
setcontenttext(int size,int color) | 内容栏字体的大小和颜色, 默认是16sp,#0aa666,用此方法会固定颜色 |
setcontenttext(colorstatelist drawable) | 自定义内容栏字体颜色变换器 在res目录下创建color文件夹用selector 默认颜色#555 选中颜色#0aa666 |
setcontentline(boolean bl) | 内容栏选中是否有下划线 默认不开启 |
setcontentlinecolor(drawable drawable) | 自定义内容栏下划线用layer-list 默认是下边框描边 颜色#0fbc72 高度1dp |
setline(int mheight,int color) | 分割线的高度和颜色 默认是0.5dp #e5e5e5 |
setitemheight(int mheight) | 设置list的item的高度(dp) 默认是40dp |
setlisttext(int size,int color) | 设置list的字体大小和颜色 默认是15 #555 |
setscrollbal(boolean bl) | 设置list是否显示滚动条,默认false |
setalpha(float mfloat) | 设置阴影层的透明度 默认是0.5f |
setdiscolour(boolean bl) | 设置选中项是否加色,默认true |
setdiscolourcolor(int color) | 设置选中项加色的颜色值,默认#0aa666 |
setdiscolourhook(boolean bl) | 设置选中项是否有√图标,默认false |
setcustomhook(drawable drawable) | 自定义√图标 |
build() | 参数设置完毕,一定要build一下 |
设置数据
方法名 | 属性 |
setinitselecttext(string firsttext) | 初始文字 |
setfirstdatas(list mfirstdatas) | 设置一级数据 |
setseconddatas(map<string, list> mseconddatas) | 设置二级数据 |
setthirddatas(map<string, list> mthirddatas) | 设置三级数据 |
setfourthdatas(map<string, list> mfourthdatas) | 设置四级数据 |
给出参考bean地址
一级provincebean 二级secondbean 三级thirdbean
默认内容栏字体颜色变换器
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:color="@color/picker_select_text_color"/>
<item android:state_pressed="true" android:color="@color/picker_select_text_color"/>
<item android:state_checked="true" android:color="@color/picker_select_text_color"/>
<item android:state_focused="true" android:color="@color/picker_select_text_color"/>
<item android:color="@color/picker_text_color"/>
</selector>
默认内容栏下划线
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- 边框颜色值 -->
<item>
<shape>
<solid android:color="@color/station_average" />
</shape>
</item>
<item android:bottom="1dp"> <!--设置只有底部有边框-->
<shape>
<solid android:color="#ffffff" />
</shape>
</item>
</layer-list>
无力吐槽,博客园的富文本编辑器真难用