android dialog省市区三级联动滚动选取效果

android上做滚动选取的效果是没有原生的控件,我做这个滚动选取的小过失用的是大神整理好的插件,如图是插件的相关的类

轮子有了做效果是比较容易的,无非就是设置数据和显示,为了自己项目中用着方便,我写了一个自定义dialog来显示,并且把数据机械的方法也都放在里边了。

MyprovinceSelectdialog.class

/**
 * 自定义省市区选取控件
 */

public class MyprovinceSelectdialog extends Dialog {

    Context mContext;

    public MyprovinceSelectdialog(Context context) {
        super(context);
        mContext = context;
    }

    public MyprovinceSelectdialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    public static class Builder implements OnWheelChangedListener {
        ArrayList<Myprovice> data;
        private Context context;

        private Handler mHandler;
        WheelView provice_select;
        WheelView city_select;
        WheelView district_select;

        public Builder(Context context, Handler mHandler) {
            this.context = context;
            this.mHandler = mHandler;
        }

        public MyprovinceSelectdialog create() {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            // instantiate the dialog with the custom Theme
            final MyprovinceSelectdialog dialog = new MyprovinceSelectdialog(context, R.style.dialog);
            View layout = inflater.inflate(R.layout.shengshiqu, null);
            //初始化布局
            provice_select = (WheelView) layout.findViewById(R.id.province_select);
            city_select = (WheelView) layout.findViewById(R.id.city_select);
            district_select = (WheelView) layout.findViewById(R.id.district_select);
            Button shengshiqu_btn_cancel = (Button) layout.findViewById(R.id.shengshiqu_btn_cancel);
            Button shengshiqu_btn_sure = (Button) layout.findViewById(R.id.shengshiqu_btn_sure);
            initData();
            if (null != data) {
                //province
                String[] item = new String[data.size()];
                for (int i = 0; i < data.size(); i++) {
                    item[i] = data.get(i).getPname();
                }
                ArrayWheelAdapter<String> adapter = new ArrayWheelAdapter<>(context, item);
                adapter.setTextColor(R.color.black);
                provice_select.setViewAdapter(adapter);
                // 设置可见条目数量
                provice_select.setVisibleItems(5);
                city_select.setVisibleItems(5);
                district_select.setVisibleItems(5);
                provice_select.addChangingListener(this);
                city_select.addChangingListener(this);
                district_select.addChangingListener(this);
                updateCities();
                updateAreas();


            }
            shengshiqu_btn_cancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.cancel();
                }
            });
            shengshiqu_btn_sure.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Message message = new Message();
                    Bundle bundle = new Bundle();
                    bundle.putString("provinceId", data.get(provice_select.getCurrentItem()).getPid());
                    bundle.putString("cityId", data.get(provice_select.getCurrentItem()).getMyCities().get(city_select.getCurrentItem()).getCid());
                    bundle.putString("districtId",
                            data.get(provice_select.getCurrentItem()).getMyCities().get(city_select.getCurrentItem()).getMyDistricts().size() > district_select.getCurrentItem() ?
                                    data.get(provice_select.getCurrentItem()).getMyCities().get(city_select.getCurrentItem()).getMyDistricts().get(district_select.getCurrentItem()).getDid() :
                                    "");
                    bundle.putString("province", data.get(provice_select.getCurrentItem()).getPname());
                    bundle.putString("city", data.get(provice_select.getCurrentItem()).getMyCities().get(city_select.getCurrentItem()).getCname());
                    bundle.putString("district", data.get(provice_select.getCurrentItem()).getMyCities().get(city_select.getCurrentItem()).getMyDistricts().size() > district_select.getCurrentItem() ?
                            data.get(provice_select.getCurrentItem()).getMyCities().get(city_select.getCurrentItem()).getMyDistricts().get(district_select.getCurrentItem()).getDname() :
                            "");
                    message.setData(bundle);
                    mHandler.sendMessage(message);
                    dialog.cancel();
                }
            });
            dialog.setContentView(layout);
            return dialog;
        }

        private void updateAreas() {
            int cCurrent = city_select.getCurrentItem();
            int pCurrent = provice_select.getCurrentItem();
            String[] item = new String[data.get(pCurrent).getMyCities().get(cCurrent).getMyDistricts().size()];
            for (int i = 0; i < item.length; i++) {
                item[i] = data.get(pCurrent).getMyCities().get(cCurrent).getMyDistricts().get(i).getDname();
            }
            ArrayWheelAdapter<String> adapter = new ArrayWheelAdapter<>(context, item);
            adapter.setTextColor(R.color.black_true);
            district_select.setViewAdapter(adapter);
            district_select.setCurrentItem(0);
        }

        private void updateCities() {
            int pCurrent = provice_select.getCurrentItem();
            String[] item = new String[data.get(pCurrent).getMyCities().size()];
            for (int i = 0; i < item.length; i++) {
                item[i] = data.get(pCurrent).getMyCities().get(i).getCname();
            }
            ArrayWheelAdapter<String> adapter = new ArrayWheelAdapter<>(context, item);
            adapter.setTextColor(R.color.black);
            city_select.setViewAdapter(adapter);
            city_select.setCurrentItem(0);
            updateAreas();
        }


        //初始化省市区信息
        public void initData() {
            String json = MyUtils.readLocalJson(context, "jsons.txt");
            JSONArray jsonArray;
            try {
                jsonArray = new JSONArray(json);
                //初始化省列表
                data = new ArrayList<Myprovice>();
                for (int y = 0; y < jsonArray.length(); y++) {
                    Myprovice myprovice = new Myprovice();
                    myprovice.setPid(jsonArray.getJSONObject(y).getString("provinceId"));
                    myprovice.setPname(jsonArray.getJSONObject(y).getString("provinceName"));
                    JSONArray jsonArray1 = jsonArray.getJSONObject(y).getJSONArray("citys");
                    List<MyCity> myCities = new ArrayList<>();
                    for (int j = 0; j < jsonArray1.length(); j++) {
                        MyCity myCity = new MyCity();
                        myCity.setCid(jsonArray1.getJSONObject(j).getString("cityId"));
                        myCity.setCname(jsonArray1.getJSONObject(j).getString("cityName"));
                        if (jsonArray1.getJSONObject(j).has("countys")) {
                            JSONArray jsonArray2 = jsonArray1.getJSONObject(j).getJSONArray("countys");
                            List<Districts> myDistricts = new ArrayList<>();
                            for (int x = 0; x < jsonArray2.length(); x++) {
                                Districts districts = new Districts();
                                districts.setDid(jsonArray2.getJSONObject(x).getString("countyId"));
                                districts.setDname(jsonArray2.getJSONObject(x).getString("countyName"));
                                myDistricts.add(districts);
                            }
                            myCity.setMyDistricts(myDistricts);
                        } else {
                            List<Districts> myDistricts = new ArrayList<>();
                            myCity.setMyDistricts(myDistricts);
                        }
                        myCities.add(myCity);
                    }
                    myprovice.setMyCities(myCities);
                    data.add(myprovice);
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onChanged(WheelView wheel, int oldValue, int newValue) {
            if (wheel == provice_select) {
                updateCities();
            } else if (wheel == city_select) {
                updateAreas();
            } else if (wheel == district_select) {

            }
        }
    }
}


在initData方法中作了数据的解析,首先要在项目的assets目录下放一个json.txt的文件,里边放的是JsonArray 格式的省市区信息,然后分别创建对应的类用来解析数据,因为在真正用到wheelview的地方其实你只需要适配器一个String[]的数据源就行,所以解析的话无关紧要,只要能做出来数据就行。主要的地方就是一个是wheelview的初始化

provice_select = (WheelView) layout.findViewById(R.id.province_select)
ArrayWheelAdapter<String> adapter = new ArrayWheelAdapter<>(context, item);
                //adapter.setTextColor(R.color.black);
                provice_select.setViewAdapter(adapter);
                // 设置可见条目数量
                provice_select.setVisibleItems(5);
                provice_select.addChangingListener(this);
builder实现了 <span style="font-size: 13.3333px; font-family: Arial, Helvetica, sans-serif;">OnWheelChangedListener 接口,调用onchange方法就可以监听滚动了,最开始的时候吧省市区三个wheelview都默认初始化第一项,当监听到省改变的时侯重新构造市的适配器,然后监听到市的选项改变的</span>
<span style="font-size: 13.3333px; font-family: Arial, Helvetica, sans-serif;">时候重新构造区的适配器。</span>
<span style="font-size: 13.3333px; font-family: Arial, Helvetica, sans-serif;">为了方便开发,我传进来一个handler用来直接将处理结果发送给调用额页面,这里没有给外部设置布局和点击事件的接口,之间在内部设置好了确认的点击事件,直接利用handler发送消息给调用处</span>
<span style="font-size: 13.3333px; font-family: Arial, Helvetica, sans-serif;"></span><pre name="code" class="html">shengshiqu_btn_sure.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Message message = new Message();
                    Bundle bundle = new Bundle();
                    bundle.putString("provinceId", data.get(provice_select.getCurrentItem()).getPid());
                    bundle.putString("cityId", data.get(provice_select.getCurrentItem()).getMyCities().get(city_select.getCurrentItem()).getCid());
                    bundle.putString("districtId",
                            data.get(provice_select.getCurrentItem()).getMyCities().get(city_select.getCurrentItem()).getMyDistricts().size() > district_select.getCurrentItem() ?
                                    data.get(provice_select.getCurrentItem()).getMyCities().get(city_select.getCurrentItem()).getMyDistricts().get(district_select.getCurrentItem()).getDid() :
                                    "");
                    bundle.putString("province", data.get(provice_select.getCurrentItem()).getPname());
                    bundle.putString("city", data.get(provice_select.getCurrentItem()).getMyCities().get(city_select.getCurrentItem()).getCname());
                    bundle.putString("district", data.get(provice_select.getCurrentItem()).getMyCities().get(city_select.getCurrentItem()).getMyDistricts().size() > district_select.getCurrentItem() ?
                            data.get(provice_select.getCurrentItem()).getMyCities().get(city_select.getCurrentItem()).getMyDistricts().get(district_select.getCurrentItem()).getDname() :
                            "");
                    message.setData(bundle);
                    mHandler.sendMessage(message);
                    dialog.cancel();
                }
            });
使用的方法就行在调用的地方

 
<span style="font-size: 13.3333px; font-family: Arial, Helvetica, sans-serif;"></span><pre name="code" class="html">MyprovinceSelectdialog.Builder builder = new MyprovinceSelectdialog.Builder(AddAddressActivity.this,new Handler(){
                    @Override
                    public void handleMessage(Message msg) {
                        addAddressEditDirtect.setText(bundle.get("province").toString()+"-"+bundle.get("city").toString()+"-"+bundle.get("district").toString());
                    }
                });
                MyprovinceSelectdialog dialog =  builder.create();
                WindowManager wm = getWindowManager();
                //让dialog显示在底部,宽度充满屏幕
                int width = wm.getDefaultDisplay().getWidth();
                Window window = dialog.getWindow();
                window.setGravity(Gravity.BOTTOM);
                window.setLayout(width, AutoGridLayout.LayoutParams.WRAP_CONTENT);
                dialog.show();


 关于wheelview插件的部分,我也是从别的大神的demo里边拿出来的,demo 

地址http://download.csdn.net/detail/z690798364/9635945

欢迎建议和帮助改进。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

子夜微凉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值