开发中仿IOS三级菜单,开源项目Android-PickerView实用自定义数据封装使用实例

compile 'com.contrarywind:Android-PickerView:3.2.7' 
androidStudio依赖,Eclipse可自行在github上搜索
Android-PickerView项目添加依赖 现git地址https://github.com/Bigkoo/Android-PickerView 下面给出项目中自定义数据的联动菜单实现
 

 

开发中仿IOS三级菜单,开源项目Android-PickerView实用自定义数据封装使用实例_第1张图片开发中仿IOS三级菜单,开源项目Android-PickerView实用自定义数据封装使用实例_第2张图片开发中仿IOS三级菜单,开源项目Android-PickerView实用自定义数据封装使用实例_第3张图片
二级菜单封装

 

public class LocationPickerShowerLocal {
    Context context;
    private ArrayList options1Items = new ArrayList<>();
    private ArrayList> options2Items = new ArrayList<>();
    static TextView tv;
    LocalAreaBean localAreaBean;

    OnLocalOptionsSelectListener mListener;
    public LocationPickerShowerLocal( Context context, String data,OnLocalOptionsSelectListener mListener) {
        this.mListener = mListener;
        this.context = context;
        initJsonData(data);

    }
    public interface OnLocalOptionsSelectListener {
        void getLocalAreaCode(String text, String areaCode);
    }
    public void show() {
        showPickerView();
    }

    private void showPickerView() {
        int color = context.getResources().getColor(R.color.maincolor);
        OptionsPickerView pvOptions = new OptionsPickerView.Builder(context, new OptionsPickerView.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                //返回的分别是三个级别的选中位置
                String text = options1Items.get(options1) +
                        options2Items.get(options1).get(options2);
                mListener.getLocalAreaCode(text,localAreaBean.getData().get(options1).getTOWN().get(options2).getAREA_CODE());
            }
        }).setTitleText("")
                .setDividerColor(color)
                .setTextColorCenter(color)
                .setContentTextSize(21)
                .setCancelColor(color)
                .setSubmitColor(color)
                .setSubmitText("完成")
                .setOutSideCancelable(true)
                .build();
        pvOptions.setPicker(options1Items, options2Items);//二级选择器*/
        pvOptions.show();
    }


    private void initJsonData(String data) {   //解析数据
        /**
         * 添加省份数据
         *
         * 注意:如果是添加的JavaBean实体,则实体类需要实现 IPickerViewData 接口,
         * PickerView会通过getPickerViewText方法获取字符串显示出来。
         */
        Gson gson = new Gson();
        localAreaBean = gson.fromJson(data, LocalAreaBean.class);
        ArrayList dataBean = (ArrayList)localAreaBean.getData();
//        options1Items = dataBean;

        for (int i = 0; i < dataBean.size(); i++) {//遍历省份
            options1Items.add(dataBean.get(i).getAREA_NAME());
            ArrayList CityList = new ArrayList<>();//该省的城市列表(第二级)

            for (int c = 0; c < dataBean.get(i).getTOWN().size(); c++) {//遍历该省份的所有城市
                String CityName = dataBean.get(i).getTOWN().get(c).getAREA_NAME();
                CityList.add(CityName);//添加城市
            }

            /**
             * 添加城市数据
             */
            options2Items.add(CityList);


        }
    }



}
三级菜单封装
public class LocationPickerShowerState { Context context;  private ArrayList options1Items = new ArrayList<>();  private ArrayList> options2Items = new ArrayList<>();  private ArrayList>> options3Items = new ArrayList<>();  OnOptionsSelectListener mListener;   public LocationPickerShowerState(Context context, String data, OnOptionsSelectListener mListener) { this.mListener = mListener;  this.context = context;  initJsonData(data);   } public void show() { showPickerView();  } public interface OnOptionsSelectListener { void getAreaCode(String text, String areaCode);  } String areaCode = "";   private void showPickerView() { int color = context.getResources().getColor(R.color.maincolor);  OptionsPickerView pvOptions = new OptionsPickerView.Builder(context, new OptionsPickerView.OnOptionsSelectListener() { @Override  public void onOptionsSelect(int options1, int options2, int options3, View v) { //返回的分别是三个级别的选中位置  String text = options1Items.get(options1) + options2Items.get(options1).get(options2) + options3Items.get(options1).get(options2).get(options3);  mListener.getAreaCode(text, stateAreaBean.getData().get(options1).getCITY().get(options2).getCOUNTY().get(options3).getAREA_CODE());  } }).setTitleText("") .setDividerColor(color) .setTextColorCenter(color) .setContentTextSize(21) .setCancelColor(color) .setSubmitColor(color) .setSubmitText("完成") .setOutSideCancelable(true) .build();  /*pvOptions.setPicker(options1Items);//一级选择器  pvOptions.setPicker(options1Items, options2Items);//二级选择器*/  pvOptions.setPicker(options1Items, options2Items, options3Items);//三级选择器  pvOptions.show();  } StateAreaBean stateAreaBean;   private void initJsonData(String data) { //解析数据  /**  * 添加省份数据  *  * 注意:如果是添加的JavaBean实体,则实体类需要实现 IPickerViewData 接口,  * PickerView会通过getPickerViewText方法获取字符串显示出来。  */  Gson gson = new Gson();  stateAreaBean = gson.fromJson(data, StateAreaBean.class);  ArrayList dataBean = (ArrayList) stateAreaBean.getData(); // options1Items = dataBean;   for (int i = 0; i < dataBean.size(); i++) {//遍历省份  options1Items.add(dataBean.get(i).getAREA_NAME());  ArrayList CityList = new ArrayList<>();//该省的城市列表(第二级)  ArrayList> Province_AreaList = new ArrayList<>();//该省的所有地区列表(第三极)   for (int c = 0; c < dataBean.get(i).getCITY().size(); c++) {//遍历该省份的所有城市  String CityName = dataBean.get(i).getCITY().get(c).getAREA_NAME();  CityList.add(CityName);//添加城市   ArrayList City_AreaList = new ArrayList<>();//该城市的所有地区列表   //如果无地区数据,建议添加空字符串,防止数据为null 导致三个选项长度不匹配造成崩溃  if (dataBean.get(i).getCITY().get(c).getAREA_NAME() == null  || dataBean.get(i).getCITY().get(c).getCOUNTY().size() == 0) { City_AreaList.add("");  } else { for (int d = 0; d < dataBean.get(i).getCITY().get(c).getCOUNTY().size(); d++) {//该城市对应地区所有数据  String AreaName = dataBean.get(i).getCITY().get(c).getCOUNTY().get(d).getAREA_NAME();   City_AreaList.add(AreaName);//添加该城市所有地区数据  } } Province_AreaList.add(City_AreaList);//添加该省所有地区数据  } /**  * 添加城市数据  */  options2Items.add(CityList);   /**  * 添加地区数据  */  options3Items.add(Province_AreaList);  } } }

三级时间菜单封装
 

public class BirthdayPickerShower {
    private TimePickerView pvTime;
    TextView tv;
    Context context;

    public BirthdayPickerShower(TextView textView, Context context) {
        tv = textView;
        this.context = context;
        initTimePicker();
    }

    public  void show() {
        pvTime.show(tv);
    }

    private void initTimePicker() {
        //控制时间范围(如果不设置范围,则使用默认时间1900-2100年,此段代码可注释)
        //因为系统Calendar的月份是从0-11的,所以如果是调用Calendar的set方法来设置时间,月份的范围也要是从0-11
        Calendar selectedDate = Calendar.getInstance();
        Calendar startDate = Calendar.getInstance();
        startDate.set(1917, 0, 23);
        Calendar endDate = Calendar.getInstance();
        endDate.set(2018, 11, 28);
        int color = context.getResources().getColor(R.color.maincolor);

        //时间选择器
        pvTime = new TimePickerView.Builder(context, new TimePickerView.OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {//选中事件回调
                // 这里回调过来的v,就是show()方法里面所添加的 View 参数,如果show的时候没有添加参数,v则为null
                TextView view = (TextView) v;
                view.setText(getTime(date));
            }
        })
                //年月日时分秒 的显示与否,不设置则默认全部显示
                .setType(new boolean[]{true, true, true, false, false, false})
                .setLabel("", "", "", "", "", "")
                .isCenterLabel(false)
                .setDividerColor(color)
                .setTextColorCenter(color)
                .setCancelColor(color)
                .setSubmitColor(color)
                .setSubmitText("完成")
                .setContentSize(21)
                .setDate(selectedDate)
                .setRangDate(startDate, endDate)
                .setDecorView(null)
                .build();
    }

    private String getTime(Date date) {//可根据需要自行截取数据显示
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }

}

实体bean,

 
如果不需要标题不需要实现IPickerViewData ,我这里也没好好实现 因为发现我的效果不需要标题

 

public class LocalAreaBean implements IPickerViewData {
    // 实现 IPickerViewData 接口,
    // 这个用来显示在PickerView上面的字符串,
    // PickerView会通过IPickerViewData获取getPickerViewText方法显示出来。
    @Override
    public String getPickerViewText() {
        return this.name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    /**
     * resCode : 000
     * resMsg : 请求成功!~
     * data : [{"AREA_NAME":"巴州镇","TOWN":[{"AREA_CODE":"632122001001","AREA_NAME":"巴州二村村委会"},{"AREA_CODE":"632122001002","AREA_NAME":"巴州一村村委会"},{"AREA_CODE":"632122001004","AREA_NAME":"大焦土村村委会"},{"AREA_CODE":"632122001003","AREA_NAME":"巴州垣村村委会"},{"AREA_CODE":"632122001010","AREA_NAME":"祁家村村委会"},{"AREA_CODE":"632122001018","AREA_NAME":"阳山村村委会"}]}]
     */

    private String name;
    private String resCode;
    private String resMsg;
    private List data;


    public String getResCode() {
        return resCode;
    }

    public void setResCode(String resCode) {
        this.resCode = resCode;
    }

    public String getResMsg() {
        return resMsg;
    }

    public void setResMsg(String resMsg) {
        this.resMsg = resMsg;
    }

    public List getData() {
        return data;
    }

    public void setData(List data) {
        this.data = data;
    }

    public static class DataBean {
        /**
         * AREA_NAME : 巴州镇
         * TOWN : [{"AREA_CODE":"632122001001","AREA_NAME":"巴州二村村委会"},{"AREA_CODE":"632122001002","AREA_NAME":"巴州一村村委会"},{"AREA_CODE":"632122001004","AREA_NAME":"大焦土村村委会"},{"AREA_CODE":"632122001003","AREA_NAME":"巴州垣村村委会"},{"AREA_CODE":"632122001010","AREA_NAME":"祁家村村委会"},{"AREA_CODE":"632122001018","AREA_NAME":"阳山村村委会"}]
         */

        private String AREA_NAME;
        private List TOWN;

        public String getAREA_NAME() {
            return AREA_NAME;
        }

        public void setAREA_NAME(String AREA_NAME) {
            this.AREA_NAME = AREA_NAME;
        }

        public List getTOWN() {
            return TOWN;
        }

        public void setTOWN(List TOWN) {
            this.TOWN = TOWN;
        }

        public static class TOWNBean {
            /**
             * AREA_CODE : 632122001001
             * AREA_NAME : 巴州二村村委会
             */

            private String AREA_CODE;
            private String AREA_NAME;

            public String getAREA_CODE() {
                return AREA_CODE;
            }

            public void setAREA_CODE(String AREA_CODE) {
                this.AREA_CODE = AREA_CODE;
            }

            public String getAREA_NAME() {
                return AREA_NAME;
            }

            public void setAREA_NAME(String AREA_NAME) {
                this.AREA_NAME = AREA_NAME;
            }
        }
    }
}

 

public class StateAreaBean { /**  * resCode : 000  * resMsg : 请求成功!~  * data : [{"CITY":[{"AREA_NAME":"海东市","AREA_CODE":"632100000000","COUNTY":[{"AREA_CODE":"632122000000","AREA_NAME":"民和回族土族自治县"},{"AREA_CODE":"632121000000","AREA_NAME":"平安区"},{"AREA_CODE":"632128000000","AREA_NAME":"循化撒拉族自治县"},{"AREA_CODE":"632127000000","AREA_NAME":"化隆回族自治县"},{"AREA_CODE":"632126000000","AREA_NAME":"互助土族自治县"},{"AREA_CODE":"632123000000","AREA_NAME":"乐都区"}]}],"AREA_NAME":"青海省","AREA_CODE":"630000000000"}]  */   private String resCode;  private String resMsg;  private List data;   public String getResCode() { return resCode;  } public void setResCode(String resCode) { this.resCode = resCode;  } public String getResMsg() { return resMsg;  } public void setResMsg(String resMsg) { this.resMsg = resMsg;  } public List getData() { return data;  } public void setData(List data) { this.data = data;  } public static class DataBean { /**  * CITY : [{"AREA_NAME":"海东市","AREA_CODE":"632100000000","COUNTY":[{"AREA_CODE":"632122000000","AREA_NAME":"民和回族土族自治县"},{"AREA_CODE":"632121000000","AREA_NAME":"平安区"},{"AREA_CODE":"632128000000","AREA_NAME":"循化撒拉族自治县"},{"AREA_CODE":"632127000000","AREA_NAME":"化隆回族自治县"},{"AREA_CODE":"632126000000","AREA_NAME":"互助土族自治县"},{"AREA_CODE":"632123000000","AREA_NAME":"乐都区"}]}]  * AREA_NAME : 青海省  * AREA_CODE : 630000000000  */   private String AREA_NAME;  private String AREA_CODE;  private List CITY;   public String getAREA_NAME() { return AREA_NAME;  } public void setAREA_NAME(String AREA_NAME) { this.AREA_NAME = AREA_NAME;  } public String getAREA_CODE() { return AREA_CODE;  } public void setAREA_CODE(String AREA_CODE) { this.AREA_CODE = AREA_CODE;  } public List getCITY() { return CITY;  } public void setCITY(List CITY) { this.CITY = CITY;  } public static class CITYBean { /**  * AREA_NAME : 海东市  * AREA_CODE : 632100000000  * COUNTY : [{"AREA_CODE":"632122000000","AREA_NAME":"民和回族土族自治县"},{"AREA_CODE":"632121000000","AREA_NAME":"平安区"},{"AREA_CODE":"632128000000","AREA_NAME":"循化撒拉族自治县"},{"AREA_CODE":"632127000000","AREA_NAME":"化隆回族自治县"},{"AREA_CODE":"632126000000","AREA_NAME":"互助土族自治县"},{"AREA_CODE":"632123000000","AREA_NAME":"乐都区"}]  */   private String AREA_NAME;  private String AREA_CODE;  private List COUNTY;   public String getAREA_NAME() { return AREA_NAME;  } public void setAREA_NAME(String AREA_NAME) { this.AREA_NAME = AREA_NAME;  } public String getAREA_CODE() { return AREA_CODE;  } public void setAREA_CODE(String AREA_CODE) { this.AREA_CODE = AREA_CODE;  } public List getCOUNTY() { return COUNTY;  } public void setCOUNTY(List COUNTY) { this.COUNTY = COUNTY;  } public static class COUNTYBean { /**  * AREA_CODE : 632122000000  * AREA_NAME : 民和回族土族自治县  */   private String AREA_CODE;  private String AREA_NAME;   public String getAREA_CODE() { return AREA_CODE;  } public void setAREA_CODE(String AREA_CODE) { this.AREA_CODE = AREA_CODE;  } public String getAREA_NAME() { return AREA_NAME;  } public void setAREA_NAME(String AREA_NAME) { this.AREA_NAME = AREA_NAME;  } } } } }


使用
1.实现接口

 

public class VerificationIdentityActivity extends BaseActivity implements LocationPickerShowerState.OnOptionsSelectListener, LocationPickerShowerLocal.OnLocalOptionsSelectListener

 

@Override
public void getAreaCode(String text, String areaCode) {
    countryText = text;
    liveDistrictTv.setText(countryText);
    countyCode = areaCode;
}

/**
 * 现居村社选择完成回调方法
 */
@Override
public void getLocalAreaCode(String text, String areaCode) {
    localText = text;
    liveVillageTv.setText(localText);
    localCode = areaCode;
}


2.显示

LocationPickerShowerState locationPickerShowerState;
LocationPickerShowerLocal locationPickerShowerLocal;
Handler mHandler = new Handler(new Handler.Callback() { @Override  public boolean handleMessage(Message msg) { switch (msg.what) { case STATE: locationPickerShowerState.show();  break;  case Local: locationPickerShowerLocal.show();  break;   } return false;  } });

 

 

 

 

 

 

 

你可能感兴趣的:(开发中仿IOS三级菜单,开源项目Android-PickerView实用自定义数据封装使用实例)