android wheelview实现三级城市选择

很早之前看淘宝就有了ios那种的城市选择控件,当时也看到网友有分享,不过那个写的很烂,后来(大概是去年吧),我们公司有这么一个项目,当时用的还是网上比较流行的那个黑框的那个,感觉特别的丑,然后我在那个开源的wheelview的基础上做封装,用户只需要专心数据的组装即可,然后填充就行,其他的可以不必考虑。

先上下效果图

接下来说下我的思路:网络请求-数据返回-设置数据-数据填充控件

接下来直接按上面的流程直接上代码:

网络请求我用的本地的json数据

 String address = Utils.readAssert(this, "address.txt");
        AddressModel model = JsonUtil.parseJson(address, AddressModel.class);

然后我们将本地的数据通过InputStream转换为String,并用过Gson将String映射到Model对象中

<span style="font-size:18px;"> public static String readAssert(Context context,  String fileName){
        String jsonString="";
        String resultString="";
        try {
            InputStream inputStream=context.getResources().getAssets().open(fileName);
            byte[] buffer=new byte[inputStream.available()];
            inputStream.read(buffer);
            resultString=new String(buffer,"utf-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultString;
    }</span>

接下来我们写自定义的Popwindos实现选择城市的弹框

<span style="font-size:18px;">public class ChooseAddressWheel implements MyOnWheelChangedListener {

    @Bind(R.id.province_wheel)
    MyWheelView provinceWheel;
    @Bind(R.id.city_wheel)
    MyWheelView cityWheel;
    @Bind(R.id.district_wheel)
    MyWheelView districtWheel;

    private Activity context;
    private View parentView;
    private PopupWindow popupWindow = null;
    private WindowManager.LayoutParams layoutParams = null;
    private LayoutInflater layoutInflater = null;

    private List<AddressDtailsEntity.ProvinceEntity> province = null;

    private OnAddressChangeListener onAddressChangeListener = null;

    public ChooseAddressWheel(Activity context) {
        this.context = context;
        init();
    }

    private void init() {
        layoutParams = context.getWindow().getAttributes();
        layoutInflater = context.getLayoutInflater();
        initView();
        initPopupWindow();
    }

    private void initView() {
        parentView = layoutInflater.inflate(R.layout.choose_city_layout, null);
        ButterKnife.bind(this, parentView);

        provinceWheel.setVisibleItems(7);
        cityWheel.setVisibleItems(7);
        districtWheel.setVisibleItems(7);

        provinceWheel.addChangingListener(this);
        cityWheel.addChangingListener(this);
        districtWheel.addChangingListener(this);
    }

    private void initPopupWindow() {
        popupWindow = new PopupWindow(parentView, WindowManager.LayoutParams.MATCH_PARENT, (int) (Utils.getScreenHeight(context) * (2.0 / 5)));
        popupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        popupWindow.setAnimationStyle(R.style.anim_push_bottom);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setOutsideTouchable(false);
        popupWindow.setFocusable(true);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            public void onDismiss() {
                layoutParams.alpha = 1.0f;
                context.getWindow().setAttributes(layoutParams);
                popupWindow.dismiss();
            }
        });
    }

    private void bindData() {
        provinceWheel.setViewAdapter(new ProvinceWheelAdapter(context, province));
        updateCitiy();
        updateDistrict();
    }

    @Override
    public void onChanged(MyWheelView wheel, int oldValue, int newValue) {
        if (wheel == provinceWheel) {
            updateCitiy();//省份改变后城市和地区联动
        } else if (wheel == cityWheel) {
            updateDistrict();//城市改变后地区联动
        } else if (wheel == districtWheel) {
        }
    }

    private void updateCitiy() {
        int index = provinceWheel.getCurrentItem();
        List<AddressDtailsEntity.ProvinceEntity.CityEntity> citys = province.get(index).City;
        if (citys != null && citys.size() > 0) {
            cityWheel.setViewAdapter(new CityWheelAdapter(context, citys));
            cityWheel.setCurrentItem(0);
            updateDistrict();
        }
    }

    private void updateDistrict() {
        int provinceIndex = provinceWheel.getCurrentItem();
        List<ProvinceEntity.CityEntity> citys = province.get(provinceIndex).City;
        int cityIndex = cityWheel.getCurrentItem();
        List<ProvinceEntity.AreaEntity> districts = citys.get(cityIndex).Area;
        if (districts != null && districts.size() > 0) {
            districtWheel.setViewAdapter(new AreaWheelAdapter(context, districts));
            districtWheel.setCurrentItem(0);
        }

    }

    public void show(View v) {
        layoutParams.alpha = 0.6f;
        context.getWindow().setAttributes(layoutParams);
        popupWindow.showAtLocation(v, Gravity.BOTTOM, 0, 0);
    }

    public void setProvince(List<ProvinceEntity> province) {
        this.province = province;
        bindData();
    }

    public void defaultValue(String provinceStr, String city, String arae) {
        if (TextUtils.isEmpty(provinceStr)) return;
        for (int i = 0; i < province.size(); i++) {
            ProvinceEntity provinces = province.get(i);
            if (provinces != null && provinces.Name.equalsIgnoreCase(provinceStr)) {
                provinceWheel.setCurrentItem(i);
                if (TextUtils.isEmpty(city)) return;
                List<ProvinceEntity.CityEntity> citys = provinces.City;
                for (int j = 0; j < citys.size(); j++) {
                    ProvinceEntity.CityEntity cityEntity = citys.get(j);
                    if (cityEntity != null && cityEntity.Name.equalsIgnoreCase(city)) {
                        cityWheel.setViewAdapter(new CityWheelAdapter(context, citys));
                        cityWheel.setCurrentItem(j);
                        if (TextUtils.isEmpty(arae)) return;
                        List<ProvinceEntity.AreaEntity> areas = cityEntity.Area;
                        for (int k = 0; k < areas.size(); k++) {
                            ProvinceEntity.AreaEntity areaEntity = areas.get(k);
                            if (areaEntity != null && areaEntity.Name.equalsIgnoreCase(arae)) {
                                districtWheel.setViewAdapter(new AreaWheelAdapter(context, areas));
                                districtWheel.setCurrentItem(k);
                            }
                        }
                    }
                }
            }
        }
    }

    @OnClick(R.id.confirm_button)
    public void confirm() {
        if (onAddressChangeListener != null) {
            int provinceIndex = provinceWheel.getCurrentItem();
            int cityIndex = cityWheel.getCurrentItem();
            int areaIndex = districtWheel.getCurrentItem();

            String provinceName = null, cityName = null, areaName = null;

            List<ProvinceEntity.CityEntity> citys = null;
            if (province != null && province.size() > provinceIndex) {
                ProvinceEntity provinceEntity = province.get(provinceIndex);
                citys = provinceEntity.City;
                provinceName = provinceEntity.Name;
            }
            List<ProvinceEntity.AreaEntity> districts = null;
            if (citys != null && citys.size() > cityIndex) {
                ProvinceEntity.CityEntity cityEntity = citys.get(cityIndex);
                districts = cityEntity.Area;
                cityName = cityEntity.Name;
            }

            if (districts != null && districts.size() > areaIndex) {
                ProvinceEntity.AreaEntity areaEntity = districts.get(areaIndex);
                areaName = areaEntity.Name;
            }

            onAddressChangeListener.onAddressChange(provinceName, cityName, areaName);
        }
        cancel();
    }

    @OnClick(R.id.cancel_button)
    public void cancel() {
        popupWindow.dismiss();
    }

    public void setOnAddressChangeListener(OnAddressChangeListener onAddressChangeListener) {
        this.onAddressChangeListener = onAddressChangeListener;
    }</span>

然后通过三个Adapter去设置值,这里展示一个

<span style="font-size:18px;">public class ProvinceWheelAdapter extends BaseWheelAdapter<AddressDtailsEntity.ProvinceEntity> {
	public ProvinceWheelAdapter(Context context, List<AddressDtailsEntity.ProvinceEntity> list) {
		super(context,list);
	}

	@Override
	protected CharSequence getItemText(int index) {
		AddressDtailsEntity.ProvinceEntity data = getItemData(index);
		if(data != null){
			return data.Name;
		}
		return null;
	}
}</span>

那么在我们的页面中我们怎么用呢?

我们先初始化:

<span style="font-size:18px;">private void init() {
        initWheel();
        initData();
    }</span>
 <span style="font-size:18px;">private void initWheel() {
        chooseAddressWheel = new ChooseAddressWheel(this);
        chooseAddressWheel.setOnAddressChangeListener(this);
    }</span>

接下来设置值了

<span style="font-size:18px;">private void initData() {
        String address = Utils.readAssert(this, "address.txt");
        AddressModel model = JsonUtil.parseJson(address, AddressModel.class);
        if (model != null) {
            AddressDtailsEntity data = model.Result;
            if (data == null) return;
            chooseAddress.setText(data.Province + " " + data.City + " " + data.Area);
            if (data.ProvinceItems != null && data.ProvinceItems.Province != null) {
                chooseAddressWheel.setProvince(data.ProvinceItems.Province);
                chooseAddressWheel.defaultValue(data.Province, data.City, data.Area);
            }
        }
    }</span>

好了,就写到这里了,有需要的请加我们的群:278792776或者188716429
最后附上代码下载地址:
https://github.com/xiangzhihong/wheelview-master

时间: 2024-08-04 02:23:01

android wheelview实现三级城市选择的相关文章

Android中使用开源框架Citypickerview实现省市区三级联动选择

1.概述 记得之前做商城项目,需要在地址选择中实现省市区三级联动,方便用户快速的填写地址,当时使用的是一个叫做android-wheel 的开源控件,当时感觉非常好用,唯一麻烦的是需要自己整理并解析省市区的xml文件,思路很简单,但是代码量相对大了些.偶然期间发现了另外一个开源组件,也就是今天要介绍的citypickerview. github地址:crazyandcoder/citypicker 2. 实现效果 下面给大家演示下实现效果: 3.   实现方法 (1)添加依赖 dependenc

自己动手丰衣足食之移动端城市选择插件

下载地址:http://download.csdn.net/detail/cometwo/9436021 接着上一篇纯js移动端日期选择插件,话说今天同事又来咨询省市县联动的效果在移动端中如何实现,还是老样子,百度上一搜,诶~又全是基于jquery.zepto的,更加可恨的是大多数都是PC版的,三个select标签!!!这在移动端上的体验太low了,我想以我的脾气肯定是要自己做的,正好之前做了日期选择,那就依葫芦画瓢自己再做一个吧,来来来,先上效果图: 用法 在html页面中引入input标签,

js实现仿阿里巴巴城市选择框效果实例

  本文实例讲述了js实现仿阿里巴巴城市选择框效果.分享给大家供大家参考.具体分析如下: 这并不是一个城市选择插件,在这里介绍只是为了mark一下二级联动的方法,此效果适用于有二级子菜单的效果,如导航栏.城市选择.类别选择等等. 样式效果是基于阿里的样式,懒得做其他调整,在area.css中仅仅是为了修改浏览器兼容性略做了一点调整. 城市数据是通过js构造,当然也可以通过后端取得数据,不过感觉没必要. ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

android 如何实现图片的选择框上下左右移动加淡入淡出的动画效果?

问题描述 android 如何实现图片的选择框上下左右移动加淡入淡出的动画效果? 就是比如说一个图片查看器 然后屏幕上有好多的图片 选中一个图片有一个选择框 然后对那个选择框做处理 实现上下左右移动加淡入淡出的效果 难点是如何控制动画的效果 比如两个不一样大小的图片 希望高手能给出答案 或者提供一个思路,谢谢!

城市选择之搜索框

新项目要做城市选择,效果图出来,用系统的搜索框达不到效果图的效果,设置searchBarStyle属性时,UISearchBarStyleDefault时设置barTintColor后边框会有黑线,设置UISearchBarStyleMinimal时无黑线单文本框颜色不符合要求.所以就自定义了下.先看下效果图 上面两个效果图的区别就是下面的图有个城市和图片.所以就封装到一个类中.只是TextField的LeftView变化.还有个问题就是城市名长短不一,设置城市的时候要进行重绘,所以我在set城

Android实现下拉导航选择菜单效果

本文介绍在Android中如何实现下拉导航选择菜单效果.   关于下拉导航选择菜单效果在新闻客户端中用的比较多,当然也可以用在其他的项目中,这样可以很方便的选择更多的菜单.我们可以让我们的应用顶部有左右滑动或进行切换的导航菜单,也可以为了增强用户体验在应用中添加这样的下拉导航选择菜单效果. 关于它的实现原理,其实也是挺简单的,就是使用PopupWindow来进行展现,在显示时控制其高度并配置以相应的动画效果.在PopupWindow中我使用GridView来控制里面的菜单项,每个菜单项对应相应的

android视频解码-android视频加速播放可选择多少倍速播放

问题描述 android视频加速播放可选择多少倍速播放 请问android视频加速播放是如何实现的!例如以1.5倍速播放视频.求教原理 解决方案 视频是由连续的帧形成的,一个帧对应一个画面,就像胶片电影.通过每秒切换多少帧形成连续的画面,当切换速度大于眼睛所能接受的速度时(貌似人眼每秒能接受24帧),那么我们就无法察觉这些变化了,我们大脑所接受的图像便是一段视频图像.这便是帧率.假如一个视频的默认帧率为60帧每秒,当我们在播放时采用120帧每秒的帧速率.那么我们看到的视频就以原来的2倍速度播放.

android webview file事件响应 选择图片过程中退出的话 按钮点不了

问题描述 android webview file事件响应 选择图片过程中退出的话 按钮点不了 public void onActivityResult(int requestCode int resultCode Intent data) { if(requestCode==REQUEST_UPLOAD_FILE_CODE) { if (null == mUploadFile) return; Uri result = data == null || resultCode != -1 ? nu

android 数据 服务器 该怎么选择

问题描述 android 数据 服务器 该怎么选择 谢各个大大看到这个问题!! 最近开发的项目是 一个 gprs模块,就是一个2g模块,和一个android手机通信,将2g模块的数据直接传送给android端,之前是直接获取android端的ip地址,并将其作为服务器监听数据.但是现在,基站分配给手机的变成了局域网ip.....欲哭无泪.....原来的方案不行了.. 所以现在只能考虑做一个服务器来负责转发.这个要求实时性要高..现在是看到bat都有云服务,但是不知道该采用什么样的方案,,服务器怎