demo1链接:https://download.csdn.net/download/meixi_android/10819638
效果1——跳页全屏式地区选择器:
demo2链接:https://download.csdn.net/download/meixi_android/10823744
样式2——dialog市区选择器:
实现方法:
1、添加依赖
compile 'cn.qqtheme.framework:WheelPicker:1.4.3' compile 'com.alibaba:fastjson:1.1.54.android'
2、自定义底部地区选择器dialog
/*** 作者:created by meixi* 邮箱:13164716840@163.com* 日期:2018/12/3 08*/public class BecomePicktask extends AsyncTask<String, Void, ArrayList<Province>> {private WeakReference<Activity> weakReference;private ProgressDialog dialog;private Callback callback;private String selectedProvince = "", selectedCity = "", selectedCounty = "";private boolean hideProvince = false;private boolean hideCounty = false;//隐藏区truepublic BecomePicktask(Activity activity) {weakReference = new WeakReference<>(activity);}public void setHideProvince(boolean hideProvince) {this.hideProvince = hideProvince;}public void setHideCounty(boolean hideCounty) {this.hideCounty = hideCounty;}public void setCallback(Callback callback) {this.callback = callback;}@Overrideprotected void onPreExecute() {Activity activity = weakReference.get();if (activity != null) {dialog = ProgressDialog.show(activity, null, "正在初始化数据...", true, true);}}@Overrideprotected ArrayList<Province> doInBackground(String... params) {if (params != null) {switch (params.length) {case 1:selectedProvince = params[0];break;case 2:selectedProvince = params[0];selectedCity = params[1];break;case 3:selectedProvince = params[0];selectedCity = params[1];selectedCounty = params[2];//隐藏区break;default:break;}}ArrayList<Province> data = new ArrayList<>();try {Activity activity = weakReference.get();if (activity != null) {String json = ConvertUtils.toString(activity.getAssets().open("city.json"));data.addAll(FastJsonUtils.getObjectsList(json, Province.class));}} catch (Exception e) {e.printStackTrace();}return data;}@Overrideprotected void onPostExecute(ArrayList<Province> result) {dialog.dismiss();if (result.size() > 0) {Activity activity = weakReference.get();if (activity != null) {AddressPicker picker = new AddressPicker(activity, result);picker.setHideProvince(hideProvince);picker.setHideCounty(hideCounty);if (hideCounty) {picker.setColumnWeight(1 / 3.0, 2 / 3.0);//将屏幕分为3份,省级和地级的比例为1:2picker.setSelectedItem(selectedProvince, selectedCity);} else {picker.setColumnWeight(2 / 8.0, 3 / 8.0, 3 / 8.0);//省级、地级和县级的比例为2:3:3picker.setSelectedItem(selectedProvince, selectedCity, selectedCounty);} // picker.setSelectedItem(selectedProvince, selectedCity, selectedCounty);//隐藏区picker.setOnAddressPickListener(callback);picker.show();}} else {callback.onAddressInitFailed();}}public interface Callback extends AddressPicker.OnAddressPickListener {void onAddressInitFailed();}}
添加 city.json 到asset
city.json数据结果