可以自定义布局 先贴一个效果图
恩 我好像之前写过一份二级的地址选择器 可以去这里找到一份city.json 也就是地址文件 也可以找到依赖
先上一个解析的工具类
import android.content.Context;
import android.content.res.AssetManager;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
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();
}
}
下边贴代码 注释都有解释 我就不再重复了
private void initJsonData() {//解析数据
/**
* 注意:assets 目录下的Json文件仅供参考,实际使用可自行替换文件
* 关键逻辑在于循环体
* */
String CityData = new GetJsonDataUtil().getJson(this, "city.json");//获取assets目录下的json文件数据
options1Items = parseData(CityData);//用Gson 转成实体
for (int i = 0; i < options1Items.size(); i++) {
String areaName = options1Items.get(i).getAreaName();
Log.i("地址", "省份" + areaName);
}
/**
* 添加省份数据
* 注意:如果是添加的JavaBean实体,则实体类需要实现 IPickerViewData 接口,
* PickerView会通过getPickerViewText方法获取字符串显示出来。
*/
//城市/市/区
for (int i = 0; i < options1Items.size(); i++) {//遍历一个城市的市
ArrayList<String> CityList = new ArrayList<>();//声明存放市的集合
ArrayList<ArrayList<String>> CityList3 = new ArrayList<>();//声明存放区的集合
for (int i1 = 0; i1 < options1Items.get(i).getCities().size(); i1++) {//遍历该省份的所有城市
String name = options1Items.get(i).getCities().get(i1).getAreaName(); //获取二级城市名
CityList.add(name);//添加城市
ArrayList<String> CityList2 = new ArrayList<>();//声明存放区县的小集合
for (int i2 = 0; i2 < options1Items.get(i).getCities().get(i1).getCounties().size(); i2++) {//遍历该市的全部区
String name3 = options1Items.get(i).getCities().get(i1).getCounties().get(i2).getAreaName(); //获取区
CityList2.add(name3);//添加城市
}
CityList3.add(CityList2);
}
/**
* 添加城市数据
* 把二级放入一级
*/
options2Items.add(CityList);
options3Items.add(CityList3);
}
}
public ArrayList<textBean> parseData(String result) {//Gson 解析
ArrayList<textBean> detail = new ArrayList<>();
try {
JSONArray data = new JSONArray(result);
Gson gson = new Gson();
for (int i = 0; i < data.length(); i++) {
textBean entity = gson.fromJson(data.optJSONObject(i).toString(), textBean.class);
detail.add(entity);
}
} catch (Exception e) {
e.printStackTrace();
}
return detail;
}
private void ziDingyi() {
/**
* @description
*
* 注意事项:
* 自定义布局中,id为 optionspicker 或者 timepicker 的布局以及其子控件必须要有,否则会报空指针。
* 具体可参考demo 里面的两个自定义layout布局。
*/
pvCustomOptions = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
@Override
public void onOptionsSelect(int options1, int options2, int options3, View v) {
//返回的分别是三个级别的选中位置
province = options1Items.get(options1).getAreaName();//一级城市 选中的值
city = options2Items.get(options1).get(options2);
three = options3Items.get(options1).get(options2).get(options3);
if (province.equals(city)) {
mPersonalSite.setText(province + " " + three);//避免出现北京市北京市这种情况
} else {
mPersonalSite.setText(province + " " + city + " " + three);//选定后显示的值
}
}
})
.setLayoutRes(R.layout.pickerview_custom_options, new CustomListener() {
@Override
public void customLayout(View v) {
//自定义布局中的控件初始化及事件处理
final TextView tvSubmit = (TextView) v.findViewById(R.id.tv_finish);//保存
tvSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
pvCustomOptions.returnData();
pvCustomOptions.dismiss();
}
});
}
})
.setLineSpacingMultiplier(2.5f)//设置Item 的间距倍数,用于控制 Item 高度间隔
.setContentTextSize(14)//滚轮文字大小
.setDividerColor(Color.WHITE)//设置分割线的颜色 这几个值没需求的可以不要
.build();
pvCustomOptions.setPicker(options1Items, options2Items, options3Items);//三级选择器
pvCustomOptions.show();
}
这是储存数据的三个集合
//地址选择器
private ArrayList<textBean> options1Items = new ArrayList<>();//一级
private ArrayList<ArrayList<String>> options2Items = new ArrayList<>();//二级
private ArrayList<ArrayList<ArrayList<String>>> options3Items = new ArrayList<>();//三级
这是自定义布局
R.layout.pickerview_custom_options
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg_banyuan"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="@color/white">
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="#aaa"
android:visibility="gone" />
<ImageView
android:id="@+id/iv_cancel"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_centerVertical="true"
android:layout_marginLeft="16dp"
android:padding="8dp"
android:src="@drawable/touxiang"
android:visibility="gone" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="城市"
android:textColor="@color/text_color_2"
android:textSize="18sp" />
<TextView
android:id="@+id/tv_finish"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="16dp"
android:text="保存"
android:textColor="#24AD9D"
android:textSize="14sp" />
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="#aaa" />
</RelativeLayout>
<!--此部分需要完整复制过去,删减或者更改ID会导致初始化找不到内容而报空-->
<LinearLayout
android:id="@+id/optionspicker"
android:layout_width="match_parent"
android:layout_height="238dp"
android:background="@android:color/white"
android:gravity="center_vertical"
android:orientation="horizontal">
<com.contrarywind.view.WheelView
android:id="@+id/options1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<com.contrarywind.view.WheelView
android:id="@+id/options2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<com.contrarywind.view.WheelView
android:id="@+id/options3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="#24ad9d"
android:visibility="gone" />
<TextView
android:id="@+id/tv_add"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#E5E9EC"
android:gravity="center"
android:text="+ 添加加油卡"
android:textColor="#24AD9D"
android:textSize="18sp"
android:visibility="gone" />
</LinearLayout>
恩 貌似没有了 加油吧少年