全國省市縣選擇在app中是十分常見的,個人信息資料填寫需要,電商填寫收貨地址需要弧轧,按地區(qū)分類填寫地址需要礁遣。有的是用wheelview滾輪實現(xiàn)斑芜,這里我是用的popupwindow實現(xiàn)。
先上效果圖:
giphy.gif
全國省市縣數(shù)據(jù)文件
json數(shù)據(jù)展示
1.從資源文件中讀取完整json字符串祟霍,用openRawResource()方法打開raw文件夾下的文件杏头。
private String getAddress() {
StringBuilder sb = new StringBuilder();
try {
InputStream inputStream = context.getResources().openRawResource(R.raw.area);
byte[] buffer = new byte[1024];
while (inputStream.read(buffer) != -1) {
sb.append(new String(buffer, "UTF-8"));
}
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}
2.分別解析出所有省市縣集合
/**
* 解析出所有省市縣
*/
private void parseArray() {
try {
JSONObject object = new JSONObject(getAddress());
JSONArray provinceArray = object.getJSONArray("province");
JSONArray cityArray = object.getJSONArray("city");
JSONArray countyArray = object.getJSONArray("district");
provinceDatas = JSON.parseArray(provinceArray.toString(), ProvinceEntity.class);
cityDatas = JSON.parseArray(cityArray.toString(), CityEntity.class);
countyDatas = JSON.parseArray(countyArray.toString(), CountyEntity.class);
} catch (JSONException e) {
e.printStackTrace();
}
}
3.逐步解析省市縣:所有的省市縣對象都有名稱text和標(biāo)記id。id由6個數(shù)字組成沸呐,前2位是省份標(biāo)識醇王,中間2位是市標(biāo)識,后面2位是縣標(biāo)識垂谢。先選擇省厦画,用id截取前2位數(shù)字,遍歷所有市前2位相等則找到對應(yīng)市滥朱。選擇市根暑,用id截取前2位數(shù)字,遍歷所有縣的id前2位徙邻,相等的則為下屬所有縣排嫌。
實體類:
public class ProvinceEntity {
private String text;
private String id;
//sett get
}
public class CityEntity {
private String text;
private String id;
//get set
}
public class CountyEntity {
private String text;
private String id;
private String zipcode;
//get set
}
關(guān)鍵代碼:
/**
* 選擇省
*/
private void selectProvince() {
recyclerView = (RecyclerView) view.findViewById(R.id.rv_popup_address);
recyclerView.setLayoutManager(new LinearLayoutManager(context));
parseArray();
CommonAdapter commonAdapter = new CommonAdapter<ProvinceEntity>(context,R.layout.item_address_textview,provinceDatas) {
@Override
protected void convert(ViewHolder holder, ProvinceEntity provinceEntity, int position) {
holder.setText(R.id.tv_popup_place,provinceEntity.getText());
}
};
recyclerView.setAdapter(commonAdapter);
commonAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
@Override
public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
saveProvinceCode = provinceDatas.get(position).getId();
saveProvinceName = provinceDatas.get(position).getText();
provinceChargeCode = saveProvinceCode.substring(0, 2);
String cityCode;
for (int i = 0; i < cityDatas.size(); i++) {
cityCode = cityDatas.get(i).getId();
if (provinceChargeCode.equals(cityCode.substring(0, 2))) {
currentCitiesDatas.add(cityDatas.get(i));
}
}
selectCites();
}
@Override
public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
return false;
}
});
view.findViewById(R.id.tv_popup_cancel).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
view.clearAnimation();
}
});
}
/**
* 根據(jù)當(dāng)前省選擇市
*/
private void selectCites(){
CommonAdapter adapter = new CommonAdapter<CityEntity>(context,R.layout.item_address_textview,currentCitiesDatas) {
@Override
protected void convert(ViewHolder holder, CityEntity cityEntity, int position) {
holder.setText(R.id.tv_popup_place,cityEntity.getText());
}
};
recyclerView.setAdapter(adapter);
adapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
@Override
public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
saveCityCode = currentCitiesDatas.get(position).getId();
saveCityName = currentCitiesDatas.get(position).getText();
citiesChargeCode = saveCityCode.substring(2, 4);
String countyCode;
for (int i = 0; i < countyDatas.size(); i++) {
countyCode = countyDatas.get(i).getId();
if (countyCode.substring(0, 2).equals(provinceChargeCode) && countyCode.substring(2,4).equals(citiesChargeCode)) {
currentCountiesDatas.add(countyDatas.get(i));
}
}
selectCounties();
}
@Override
public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
return false;
}
});
}
/**
* 根據(jù)當(dāng)前市選擇縣
*/
private void selectCounties(){
CommonAdapter adapter = new CommonAdapter<CountyEntity>(context,R.layout.item_address_textview,currentCountiesDatas) {
@Override
protected void convert(ViewHolder holder, CountyEntity countyEntity, int position) {
holder.setText(R.id.tv_popup_place,countyEntity.getText());
}
};
recyclerView.setAdapter(adapter);
adapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
@Override
public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
saveCountyCode = currentCountiesDatas.get(position).getId();
saveCountyName = currentCountiesDatas.get(position).getText();
saveZipCode = currentCountiesDatas.get(position).getZipcode();
dismiss();
view.clearAnimation();
if(callBack != null){
callBack.callBack(saveProvinceName + ";" + saveCityName + ";" + saveCountyName);
}
}
@Override
public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
return false;
}
});
}
4.Popupwindow使用方法:rootview為頁面根布局
AddressPopupWindow addressPopupWindow = new AddressPopupWindow(this);
addressPopupWindow.showAtLocation(rootView, Gravity.BOTTOM, 0, 0);
完整代碼請看github上demo。后續(xù)會出全球國家選擇器缰犁。