Android全国省市县逐级选择

全国省市县选择在app中是十分常见的,个人信息资料填写需要,电商填写收货地址需要,按地区分类填写地址需要。有的是用wheelview滚轮实现,这里我是用的popupwindow实现。

github代码直通车

先上效果图:


Android全国省市县逐级选择_第1张图片
giphy.gif

全国省市县数据文件

Android全国省市县逐级选择_第2张图片
json数据展示

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和标记id。id由6个数字组成,前2位是省份标识,中间2位是市标识,后面2位是县标识。先选择省,用id截取前2位数字,遍历所有市前2位相等则找到对应市。选择市,用id截取前2位数字,遍历所有县的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
}

关键代码:

/**
     * 选择省
     */
    private void selectProvince() {

        recyclerView = (RecyclerView) view.findViewById(R.id.rv_popup_address);
        recyclerView.setLayoutManager(new LinearLayoutManager(context));

        parseArray();
        CommonAdapter commonAdapter = new CommonAdapter(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();
            }
        });
    }

    /**
     * 根据当前省选择市
     */
    private void selectCites(){
        CommonAdapter adapter = new CommonAdapter(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;
            }
        });
    }

    /**
     * 根据当前市选择县
     */
    private void selectCounties(){
        CommonAdapter adapter = new CommonAdapter(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。后续会出全球国家选择器。

你可能感兴趣的:(Android全国省市县逐级选择)