引言
很多时候我们都会在各种app上看到地区选择的功能,今天就来使用PickerView来实现地区三级联动的选择器功能。
本篇与通过接口解析地区数据的方式不同,而是采用解析本地地区json文件的方式来解析数据。也就是说,无需网络连接解析接口,而是直接通过本地工具类解析json文件。话不多说,快来看看怎么实现吧!
效果预览
用法
步骤一、引入第三方依赖库
在app下build.gradle中添加如下依赖:
//PickerView库
implementation 'com.contrarywind:Android-PickerView:4.1.9'
步骤二、布局文件
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".blog.Case46"
tools:ignore="MissingConstraints">
<Button
android:id="@+id/btnAreaPicker"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="@color/green"
android:text="地区三级联动"
android:textColor="@color/white"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
步骤三、添加地区json文件
请移步github中下载该文件
传送门:地区(省-市-区)json文件
添加到AndroidStudio中:app——main——src——assets文件夹中
步骤四、创建DataBean类
/**
* @data on 2020/11/2 3:30 PM
* @auther armStrong
* @describe 地区json解析类
*/
public class ProviceBean {
/**
* name : 北京市
* city : [{"name":"北京市","area":["东城区","西城区","崇文区","宣武区","朝阳区","丰台区","石景山区","海淀区","门头沟区","房山区","通州区","顺义区","昌平区","大兴区","平谷区","怀柔区","密云县","延庆县"]}]
*/
private String name;
private List<CityBean> city;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<CityBean> getCity() {
return city;
}
public void setCity(List<CityBean> city) {
this.city = city;
}
public static class CityBean {
/**
* name : 北京市
* area : ["东城区","西城区","崇文区","宣武区","朝阳区","丰台区","石景山区","海淀区","门头沟区","房山区","通州区","顺义区","昌平区","大兴区","平谷区","怀柔区","密云县","延庆县"]
*/
private String name;
private List<String> area;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<String> getArea() {
return area;
}
public void setArea(List<String> area) {
this.area = area;
}
}
}
步骤五、解析数据工具类
/**
* @data on 2020/11/2 3:32 PM
* @auther armStrong
* @describe 读取Assent资源文件中的并将.json文件转换成String类型
*/
public class GetJsonDataUtil {
public 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();
}
}
步骤六、在Activity中书写业务逻辑代码
public class Case46 extends AppCompatActivity {
private Button btnAreaPicker;
//省、市、区-列表
private List<String> options1Items = new ArrayList<>();
private List<List<String>> options2Items = new ArrayList<>();
private List<List<List<String>>> options3Items = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_case46);
//初始化
initView();
//弹出选择器
getView();
//填充数据
initJsonData();
}
private void initView() {
btnAreaPicker = findViewById(R.id.btnAreaPicker);
}
private void getView() {
btnAreaPicker.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
areaPicker();
}
});
}
/**
* PickerView用法
*/
private void areaPicker() {
OptionsPickerView pvOptions = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
@Override
public void onOptionsSelect(int options1, int options2, int options3, View v) {
String str = options1Items.get(options1) +
options2Items.get(options1).get(options2) +
options3Items.get(options1).get(options2).get(options3);
ToastUtils.show(str);
}
})
.setTitleText("城市选择")
.setDividerColor(Color.BLACK)
.setTextColorCenter(Color.BLACK)
.setContentTextSize(20)
.setOutSideCancelable(false)
.build();
pvOptions.setPicker(options1Items, options2Items, options3Items);
pvOptions.show();
}
/**
* 解析数据
*/
private void initJsonData() {
String str = new GetJsonDataUtil().getJson(this, "province.json");
List<ProviceBean> list = new Gson().fromJson(str, new TypeToken<List<ProviceBean>>() {
}.getType());
for (ProviceBean bean : list) {
options1Items.add(bean.getName());
List<String> city = new ArrayList<>();
List<List<String>> area = new ArrayList<>();
for (ProviceBean.CityBean cityBean : bean.getCity()) {
city.add(cityBean.getName());
area.add(cityBean.getArea());
}
options2Items.add(city);
options3Items.add(area);
}
}
}