最近项目中用到wheelview效果,参考了大神Hongyang的实现思路,然后在github上merge了一个库,主要的工作是修复了各种异常情况下的bug
效果图:
ps:用到了一个json文件以及解析拼接我们需要的数据
json数据文件我已经放到压缩包里面了,我们通过读取assert中的json,拼接为三个list,来适配我们的view
主要代码如下:
1:初始化数据【解析json】
private ArrayList<SdkProvinceBean> options1Items = new ArrayList<SdkProvinceBean>(); private ArrayList<ArrayList<String>> options2Items = new ArrayList<ArrayList<String>>(); private ArrayList<ArrayList<ArrayList<String>>> options3Items = new ArrayList<ArrayList<ArrayList<String>>>(); OptionsPickerView pvOptions; private void pickViewInit() { try { String jsonStr = FileUtil.readJSONFileSDK(EPApp.mContext); SdkProvience mProvience = JSON.parseObject(jsonStr, SdkProvience.class); ArrayList<SdkCity> citylist = mProvience.getCitylist(); //opt1 for (int i = 0; i < citylist.size(); i++) { SdkCity currentCity = citylist.get(i); options1Items.add(new SdkProvinceBean(i, currentCity.getP(), "", "")); ArrayList<String> options2Items_ = new ArrayList<String>(); ArrayList<ArrayList<String>> options3Items_01_0 = new ArrayList<ArrayList<String>>(); for (int j=0;j<currentCity.getC().size();j++){ LogUtils.i("jone","size "+currentCity.getC().size()); WeelC currentWeelC = currentCity.getC().get(j); options2Items_.add(currentWeelC.getN()); ArrayList<String> options3Items_01_1 = new ArrayList<String>(); if (currentWeelC.getA() != null){ for (int k=0;k<currentWeelC.getA().size();k++){ options3Items_01_1.add(currentWeelC.getA().get(k).getS()); } options3Items_01_0.add(options3Items_01_1); } } options2Items.add(options2Items_); options3Items.add(options3Items_01_0); } } catch (Exception e) { } //监听确定选择按钮 pvOptions.setOnoptionsSelectListener(new OptionsPickerView.OnOptionsSelectListener() { @Override public void onOptionsSelect(int options1, int option2, int options3) { //返回的分别是三个级别的选中位置 pvOptions.setDismis(); StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append(options1Items.get(options1).getPickerViewText() + options2Items.get(options1).get(option2)); try { stringBuffer.append(options3Items.get(options1).get(option2).get(options3)); } catch (Exception e) { } ToastUtil.showMessage(getActivity(),stringBuffer.toString()); } @Override public void onOptionsDismiss() { pvOptions.setDismis(); } }); }
pvOptions.setPicker(options1Items, options2Items, options3Items, true); pvOptions.setTitle("选择城市"); pvOptions.setCyclic(false, false, false); pvOptions.setSelectOptions(0, 1, 0); pvOptions.show();
public static String readJSONFileSDK(Context context) { int len; byte buf[] = new byte[1024]; InputStream inputStream; ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); try { inputStream = context.getAssets().open("sdkarea.json"); while ((len = inputStream.read(buf)) != -1) { outputStream.write(buf, 0, len); } jsonAera = new String(outputStream.toByteArray(), "UTF-8"); outputStream.close(); inputStream.close(); } catch (Exception e) { LogUtils.e(TAG, "", e); } return jsonAera; }
http://blog.csdn.net/lmj623565791/article/details/23382805 Hongyang的实现思路
相关资源我已经打包:
http://download.csdn.net/detail/yaya_soft/9303335