笔记20170514--PickerView实现三级联动(非本地

2017-05-14  本文已影响0人  hello我的world

需求:

选择地址可以像美团的地址选择一样

要达到的效果

美团地址选择.jpg
不知道美团的行政单位数组是放在本地,还是接口加载的,但是我们自己的都是通过接口返回,今天笔记记录的重点就是如何通过接口加载省市区后适配到PickerView实现三级联动的。PickerView的github地址在这里

相关代码

相关变量声明:
//省级单位的数组
List<CityInfo> newCityInfoList = new ArrayList<>();
//市级单位的数组
List<List<CityInfo>> cityInfoList = new ArrayList<>();
//县区级单位的数组
List<List<List<CityInfo>>> areaInfoList = new ArrayList<>();
//pickerView
OptionsPickerView optionsPickerView;
//是否第一次加载  
int firstLoad =0;
//下标
int index1,index2;
在activity的oncreate方法中直接就开始请求所有的省级单位,地址返回的代码片段(返回所有“省”级别的单位,用数组封装好bean),最后一句是请求数组中第一位省级单位(通常是“北京”)下边的市:
public void showProvinceList(List<CityInfo> provinceList) {
        List<CityInfo> oldCityInfoList= provinceList;
        newCityInfoList = new ArrayList<>();
        for(CityInfo cityInfo :oldCityInfoList){
            if(cityInfo.getId()==110000){
                newCityInfoList.add(cityInfo);
                oldCityInfoList.remove(cityInfo);
                break;
            }
        }
        for(CityInfo cityInfo : oldCityInfoList){
            if (cityInfo.getId()==310000){
                newCityInfoList.add(cityInfo);
                oldCityInfoList.remove(cityInfo);
                break;
            }
        }
        newCityInfoList.addAll(oldCityInfoList);
        presenter.getCityList(newCityInfoList.get(0));
    }
地址返回的代码片段(返回所有市级单位,用数组封装好bean),最后一句是请求数组中第一位市级单位下边的县区(对应这一句:presenter.getAreaList(cityList.get(0));):
 public void showCityList(List<CityInfo> cityList) {
        if(cityList.size()==0){
            cityInfoList.add(cityList);
        }else{
            cityInfoList.clear();
            cityInfoList.add(cityList);
        }
        presenter.getAreaList(cityList.get(0));
        if(firstLoad!=0) optionsPickerView.setCPicker(cityInfoList,index1);
    }
地址返回的代码片段(返回所有的县区单位,用数组封装好bean):
 List<List<CityInfo>> list= new ArrayList<>();
        list.add(areaList);
        if(areaInfoList.size()==0){
            areaInfoList.add(list);
        }else{
            areaInfoList.clear();
            areaInfoList.add(list);
        }
        if(firstLoad!=0) optionsPickerView.setAPicker(areaInfoList,index2);
创建pickerView:
optionsPickerView = new OptionsPickerView.Builder(this, (o1, o2, o3, v) -> {
            CityInfo p = newCityInfoList.get(o1);
            CityInfo c = cityInfoList.get(0).get(o2);
            CityInfo a = areaInfoList.get(0).get(0).get(o3);
            address.setText(p.getName()+" "+c.getName()+" "+a.getName());
            presenter.updateAddress(p,c,a);
        }).setLayoutRes(R.layout.picker_item, v -> {
            final TextView tvSubmit = (TextView) v.findViewById(R.id.tv_finish);
            TextView ivCancel = (TextView) v.findViewById(R.id.iv_cancel);
            tvSubmit.setOnClickListener(view -> {
                optionsPickerView.returnData(view);
            });

            ivCancel.setOnClickListener(view -> {
                optionsPickerView.dismiss();
            });
        }).build();
点击某个控件时弹出pickerView:
optionsPickerView.setPicker(newCityInfoList,cityInfoList,areaInfoList);
optionsPickerView.show();
实现PickerView的OnItemSelectedListener监听器
//省级单位的选择监听,index是返回回来目前的省级单位的下标,index1是市级单位的下标
OnItemSelectedListener pSelect = index -> {
        index1 = 0;
        index2 = 0;
        firstLoad=1;
        //选择了哪个省后,获取这个省下边的城市列表
        presenter.getCityList(newCityInfoList.get(index));
    };
//市级单位的选择监听,index返回的是市级单位选中的下标
OnItemSelectedListener cCelect = index -> {
        index1 = index;
        index2 = 0;
        firstLoad=2;
        //选择了哪个市后,获取这个市下边的县区列表
        presenter.getAreaList(cityInfoList.get(0).get(index));
    };
设置选择监听器
//本身pickerView这两个监听器不提供外部设置的,这是自己加的
ListenerHelper.setProvinceSelecterListener(pSelect);
ListenerHelper.setCitySelecterListener(cCelect);

//就是下边这个类
package com.bigkoo.pickerview;

import com.bigkoo.pickerview.listener.OnItemSelectedListener;

/**
 * Created by Hello我的World on 2017/5/10.
 */

public class ListenerHelper {
    private static OnItemSelectedListener provinceSelecterListener;
    private static OnItemSelectedListener citySelecterListener;

    public static OnItemSelectedListener getProvinceSelecterListener() {
        return provinceSelecterListener;
    }

    public static void setProvinceSelecterListener(OnItemSelectedListener provinceSelecterListener) {
        ListenerHelper.provinceSelecterListener = provinceSelecterListener;
    }

    public static OnItemSelectedListener getCitySelecterListener() {
        return citySelecterListener;
    }

    public static void setCitySelecterListener(OnItemSelectedListener citySelecterListener) {
        ListenerHelper.citySelecterListener = citySelecterListener;
    }
}
请求返回数据后,联动更新的方法
//showCityList()中最后一句话
if(firstLoad!=0) optionsPickerView.setCPicker(cityInfoList,index1);
//showAreaList()中最后一句话
if(firstLoad!=0) optionsPickerView.setAPicker(areaInfoList,index2);
//省市中如果选择后,请求下一级时,返回没有下一级了,就清除一下,例如:澳门 澳门 空
optionsPickerView.clearArea();
//以上这仨个方法,源码中没有,自己加的

//在OptionsPicerView中增加三个方法
public void setCPicker(List<List<T>> cPicker,int index){
        wheelOptions.setCPicker(cPicker,index);
}

public void setAPicker(List<List<List<T>>> aPicker,int index){
        wheelOptions.setAPicker(aPicker,index);
}

public void clearArea(){
        wheelOptions.clearArea();
}

//在WheelOptions中增加上述的三个方法
//更新城市列表
public void setCPicker(List<List<T>> cPicker,int index){
        this.mOptions2Items  = cPicker;
        notifyPickerC(index);
}
//更新城市列表的具体操作
private void notifyPickerC(int index) {
        if (mOptions2Items != null)
            wv_option2.setAdapter(new ArrayWheelAdapter(mOptions2Items.get(0)));// 设置显示数据
        wv_option2.setCurrentItem(index);// 初始化时显示的数据
        if (this.mOptions2Items == null)
            wv_option2.setVisibility(View.GONE);
}

//更新县区列表
public void setAPicker(List<List<List<T>>> aPicker,int index){
        this.mOptions3Items = aPicker;
        notifyPickerA(index);
}
//更新县区列表的具体操作
private void notifyPickerA(int index){
        // 选项3
        if (mOptions3Items != null)
            wv_option3.setAdapter(new ArrayWheelAdapter(mOptions3Items.get(0).get(0)));// 设置显示数据
        wv_option3.setCurrentItem(index);
        wv_option1.setIsOptions(true);
        wv_option2.setIsOptions(true);
        wv_option3.setIsOptions(true);
        if (this.mOptions3Items == null)
            wv_option3.setVisibility(View.GONE);
}

//给县区列表直接设置个空的adapter
public void clearArea(){
        wv_option3.setAdapter(new ArrayWheelAdapter(new ArrayList(),0));
}

//设置两个监听器
if (options2Items != null && linkage)
            if(ListenerHelper.getProvinceSelecterListener()!=null)
                wv_option1.setOnItemSelectedListener(ListenerHelper.getProvinceSelecterListener());

if (options3Items != null && linkage)
            if(ListenerHelper.getCitySelecterListener()!=null)
                wv_option2.setOnItemSelectedListener(ListenerHelper.getCitySelecterListener());

实现思路:

以上是实现这个需求的笔记。PickerView的链接在这里,听说招行的app都有用这个哎

上一篇 下一篇

猜你喜欢

热点阅读