最近有需求需要实现省市区三级联动,但是发现之前的实现不够灵活,自己做了一些优化。为了方便以后使用,抽离出来放在了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省市区三级联动控件使用方法实例讲解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!