三个依赖
implementation 'com.google.code.gson:gson:2.7'
implementation 'com.contrarywind:Android-PickerView:4.1.9'
implementation 'com.contrarywind:wheelview:4.1.0'
地址弹框封装
public class RegionalChooseUtil {
private static List options1Items = new ArrayList<>();
private static ArrayList> options2Items = new ArrayList<>();
private static ArrayList>> options3Items = new ArrayList<>();
/**
* 弹框展示
* @return
*/
public static void showPickerView(final Context context, final MyCallBack callBack) {// 弹出选择器
OptionsPickerView pvOptions = new OptionsPickerBuilder(context, new OnOptionsSelectListener() {
@Override
public void onOptionsSelect(int options1, int options2, int options3, View v) {
//返回的分别是三个级别的选中位置
String opt1tx = options1Items.size() > 0 ?
options1Items.get(options1).getPickerViewText() : "";
String opt2tx = options2Items.size() > 0
&& options2Items.get(options1).size() > 0 ?
options2Items.get(options1).get(options2) : "";
String opt3tx = options2Items.size() > 0
&& options3Items.get(options1).size() > 0
&& options3Items.get(options1).get(options2).size() > 0 ?
options3Items.get(options1).get(options2).get(options3) : "";
String tx = opt1tx + opt2tx + opt3tx;
ResultBean bean = new ResultBean(opt1tx, opt2tx, opt3tx);
callBack.callBack(bean);
}
})
.setTitleText("城市选择")
.setDividerColor(Color.BLACK)
.setTextColorCenter(Color.BLACK) //设置选中项文字颜色
.setContentTextSize(18)
.build();
/*pvOptions.setPicker(options1Items);//一级选择器
pvOptions.setPicker(options1Items, options2Items);//二级选择器*/
pvOptions.setPicker(options1Items, options2Items, options3Items);//三级选择器
pvOptions.show();
}
/**
* 初始化弹框
*/
public static void initJsonData(Context context) {//解析数据
/**
* 注意:assets 目录下的Json文件仅供参考,实际使用可自行替换文件
* 关键逻辑在于循环体
*
* */
String JsonData = getJson(context, "province.json");//获取assets目录下的json文件数据
ArrayList jsonBean = parseData(JsonData);//用Gson 转成实体
/**
* 添加省份数据
*
* 注意:如果是添加的JavaBean实体,则实体类需要实现 IPickerViewData 接口,
* PickerView会通过getPickerViewText方法获取字符串显示出来。
*/
options1Items = jsonBean;
for (int i = 0; i < jsonBean.size(); i++) {//遍历省份
ArrayList cityList = new ArrayList<>();//该省的城市列表(第二级)
ArrayList> province_AreaList = new ArrayList<>();//该省的所有地区列表(第三极)
for (int c = 0; c < jsonBean.get(i).getCityList().size(); c++) {//遍历该省份的所有城市
String cityName = jsonBean.get(i).getCityList().get(c).getName();
cityList.add(cityName);//添加城市
ArrayList city_AreaList = new ArrayList<>();//该城市的所有地区列表
//如果无地区数据,建议添加空字符串,防止数据为null 导致三个选项长度不匹配造成崩溃
/*if (jsonBean.get(i).getCityList().get(c).getArea() == null
|| jsonBean.get(i).getCityList().get(c).getArea().size() == 0) {
city_AreaList.add("");
} else {
city_AreaList.addAll(jsonBean.get(i).getCityList().get(c).getArea());
}*/
city_AreaList.addAll(jsonBean.get(i).getCityList().get(c).getArea());
province_AreaList.add(city_AreaList);//添加该省所有地区数据
}
/**
* 添加城市数据
*/
options2Items.add(cityList);
/**
* 添加地区数据
*/
options3Items.add(province_AreaList);
}
}
private static ArrayList parseData(String result) {//Gson 解析
ArrayList detail = new ArrayList<>();
try {
JSONArray data = new JSONArray(result);
Gson gson = new Gson();
for (int i = 0; i < data.length(); i++) {
RegionalBean entity = gson.fromJson(data.optJSONObject(i).toString(), RegionalBean.class);
detail.add(entity);
}
} catch (Exception e) {
e.printStackTrace();
}
return detail;
}
// 读取Json文件
private static String getJson(Context context, String fileName) {
StringBuilder stringBuilder = new StringBuilder();
try {
AssetManager assetManager = context.getAssets();
BufferedReader bf = new BufferedReader(new InputStreamReader(
assetManager.open(fileName)));
String line;
while ((line = bf.readLine()) != null) {
stringBuilder.append(line);
}
} catch (IOException e) {
e.printStackTrace();
}
return stringBuilder.toString();
}
/**
* 地区省份选择
*/
private class RegionalBean implements IPickerViewData {
/**
* name : 省份
* city : [{"name":"北京市","area":["东城区","西城区","崇文区","宣武区","朝阳区"]}]
*/
private String name;
private List city;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List getCityList() {
return city;
}
public void setCityList(List city) {
this.city = city;
}
// 实现 IPickerViewData 接口,
// 这个用来显示在PickerView上面的字符串,
// PickerView会通过IPickerViewData获取getPickerViewText方法显示出来。
@Override
public String getPickerViewText() {
return this.name;
}
}
/**
* 城市对象
*/
private class CityBean {
/**
* name : 城市
* area : ["东城区","西城区","崇文区","昌平区"]
*/
private String name;
private List area;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List getArea() {
return area;
}
public void setArea(List area) {
this.area = area;
}
}
/**
* 选择结果
*/
public static class ResultBean {
private String province;
private String city;
private String area;
public ResultBean(String province, String city, String area) {
this.province = province;
this.city = city;
this.area = area;
}
public ResultBean() {
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getArea() {
return area;
}
public void setArea(String area) {
this.area = area;
}
@Override
public String toString() {
return "ProvinceBean{" +
"province='" + province + '\'' +
", city='" + city + '\'' +
", area='" + area + '\'' +
'}';
}
}
}
MainActivity中调用
RegionalChooseUtil.initJsonData(MainActivity.this);
RegionalChooseUtil.showPickerView(MainActivity.this, new MyCallBack() {
@Override
public void callBack(Object object) {
Toast.makeText(MainActivity.this, object.toString(), Toast.LENGTH_SHORT).show();
}
});
还有一个简单的回调
public interface MyCallBack {
void callBack(Object object);
}
最后一个资源文件(这个不太好放在这里,有需要的可以去github中下载)