Android省市区三级联动控件使用方法实例讲解
最近有需求需要实现省市区三级联动,但是发现之前的实现不够灵活,自己做了一些优化。为了方便以后使用,抽离出来放在了github上wheelview。同时把其核心库放在了jcenter中了,可以直接引用。也可以参考项目中的demo进行引用
下面介绍一下如何使用
如果用的是androidstudio那么直接在build.gradle文件中添加依赖:
dependencies { compile 'chuck.wheelitemview:library:1.0.1' }
成功引入库之后,可以在需要弹出省市区选择框的页面生成一addresspickerview 实例。
pickerview = new addresspickerview(this);
下面来说说需要的数据源,我对数据做了抽象,在使用的时候,将自己的数据model实现iwheelviewmodel接口就可以了,如下:
public class addressmodel implements iwheelviewmodel { public string addressname; public string addressid; @override public string getvaluestring() { return addressname; } @override public string getvalueid() { return addressid; } }
而后,就是讲你自己从服务器或者本地数据库得到到的数据按照一定的格式组件成三级数据list,这里可能会有一点复杂,需要在组装的过程中小心一定,省市区要对应不然数据会错乱。组装数据源,最后得到省市区对应的list:
private list<iwheelviewmodel> moneitems = new arraylist<>(); private list<list<iwheelviewmodel>> mtwoitems; private list<list<list<iwheelviewmodel>>> mthreeitems;
将数据源填充到view中:
//设置三级数据 pickerview.setpickerdata(moneitems, mtwoitems, mthreeitems, true);
给确定和关闭按钮设置监听:
pickerview.setonpickerselectlistener(new onpickerselectlistener() { @override public void onselect(int pos1, int pos2, int pos3) { string resultone, resulttwo, resultthree; resultone = moneitems.get(pos1).getvaluestring(); resulttwo = mtwoitems.get(pos1).get(pos2).getvaluestring(); resultthree = mthreeitems.get(pos1).get(pos2).get(pos3).getvaluestring(); toast.maketext(mainactivity.this, resultone + resulttwo + resultthree, toast.length_short).show(); } }); pickerview.setondismisslistener(new ondismisslistener() { @override public void ondismiss(object o) { } });
如果不做特殊的设置,选中字体和非中字体都有默认的颜色和大小,如果想改变这些属性,我也在addresspickerview中提供了对应的方法:
/** * 被选中item的字体大小 * * @param size */ public void setselecttextsize(int size) /** * 被选中item字体颜色 * * @param color */ public void setselecttextcolor(int color) /** * 被选中item的字体大小 * * @param size dimen中定义的size大小 */ public void setunselecttextsize(int size) /** * 被选中item字体颜色 * * @param colorid */ public void setunselecttextcolor(int colorid)
设置完这些属性之后,直接调用show()方法就可以弹出这个选择框了。
完整的代码:
public class mainactivity extends appcompatactivity { addresspickerview pickerview; private list<iwheelviewmodel> moneitems = new arraylist<>(); private list<list<iwheelviewmodel>> mtwoitems; private list<list<list<iwheelviewmodel>>> mthreeitems; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); initdata(); //创建实例 pickerview = new addresspickerview(this); //设置title pickerview.settitle("收货地址"); //设置被选中文字的颜色 pickerview.setselecttextcolor(r.color.green); pickerview.setselecttextsize(r.dimen.select_text_size); pickerview.setunselecttextcolor(r.color.address_view_confirm_text_color); pickerview.setunselecttextsize(r.dimen.unselect_text_size); //设置三级数据 pickerview.setpickerdata(moneitems, mtwoitems, mthreeitems, true); //设置确定和关闭监听 pickerview.setonpickerselectlistener(new onpickerselectlistener() { @override public void onselect(int pos1, int pos2, int pos3) { string resultone, resulttwo, resultthree; resultone = moneitems.get(pos1).getvaluestring(); resulttwo = mtwoitems.get(pos1).get(pos2).getvaluestring(); resultthree = mthreeitems.get(pos1).get(pos2).get(pos3).getvaluestring(); toast.maketext(mainactivity.this, resultone + resulttwo + resultthree, toast.length_short).show(); } }); pickerview.setondismisslistener(new ondismisslistener() { @override public void ondismiss(object o) { } }); } private void initdata() { addressmodel model = new addressmodel(); model.addressname = "北京"; moneitems.add(model); addressmodel model1 = new addressmodel(); model1.addressname = "湖北"; moneitems.add(model1); addressmodel model2 = new addressmodel(); model2.addressname = "河北"; moneitems.add(model2); mtwoitems = new arraylist<>(); list<iwheelviewmodel> l21 = new arraylist<>(); addressmodel model21 = new addressmodel(); model21.addressname = "北京市"; l21.add(model21); list<iwheelviewmodel> l22 = new arraylist<>(); addressmodel model221 = new addressmodel(); model221.addressname = "武汉"; addressmodel model222 = new addressmodel(); model222.addressname = "襄阳"; addressmodel model223 = new addressmodel(); model223.addressname = "十堰"; addressmodel model224 = new addressmodel(); model224.addressname = "孝感"; addressmodel model225 = new addressmodel(); model225.addressname = "荆州"; l22.add(model221); l22.add(model222); l22.add(model223); l22.add(model224); l22.add(model225); list<iwheelviewmodel> l23 = new arraylist<>(); addressmodel model231 = new addressmodel(); model231.addressname = "石家庄"; addressmodel model232 = new addressmodel(); model232.addressname = "邯郸"; addressmodel model233 = new addressmodel(); model233.addressname = "秦皇岛"; addressmodel model234 = new addressmodel(); model234.addressname = "保定"; addressmodel model235 = new addressmodel(); model235.addressname = "张家口"; l23.add(model231); l23.add(model232); l23.add(model233); l23.add(model234); l23.add(model235); mtwoitems.add(l21); mtwoitems.add(l22); mtwoitems.add(l23); mthreeitems = new arraylist<>(); list<iwheelviewmodel> l31 = new arraylist<>(); for (int i = 0; i < 10; i++) { addressmodel model3 = new addressmodel(); model3.addressname = "北京市" + i; l31.add(model3); } list<list<iwheelviewmodel>> list21 = new arraylist<>(); list21.add(l31); mthreeitems.add(list21); for (int k = 0; k < 2; k++) { list<list<iwheelviewmodel>> temp = new arraylist<>(); for (int j = 0; j < 5; j++) { list<iwheelviewmodel> temp1 = new arraylist<>(); for (int i = 0; i < 10; i++) { addressmodel model111 = new addressmodel(); model111.addressname = "地区" + i; temp1.add(model111); } temp.add(temp1); } mthreeitems.add(temp); } } public void onclick(view view) { pickerview.show(); } }
这些代码可以参考demo,实际上,这里只是实现了省市区的三级联动,还可以自己参考addresspickerview定制自己想要的滚动选择框。这里只讨论省市区这种场景,其他的场景可以自己实现。
如果觉得有用,欢迎star,如果觉得有问题,欢迎给出建议。
https://github.com/hgchenkai/wheelview
以上所述是小编给大家介绍的android省市区三级联动控件使用方法实例讲解,希望对大家有所帮助