看了这篇博客,讲的很好很详细!分享一下!!
http://blog.csdn.net/ywl5320/article/details/44730457
2017.5.3补充:
前言
Github上边现在有好多WheelView相关的开源项目,以star数最多的AndroidPicker为例,说明一下级联选择。
项目地址:https://github.com/gzu-liyujiang/AndroidPicker
AndroidPicker里边也是写的有级联选择的—LinkagePicker。
下面说一下,自己在xml中实现一个二级级联的例子,我们可以直接将library里边的核心WheelView拷贝到项目中即可!
简单级联使用
因为要二级级联选择,首先肯定的是我们需要两个WheelView,看一下布局:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<com.xxx.views.WheelView
android:id="@+id/wheel_line"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="@dimen/widget_padding" />
<com.xxx.views.WheelView
android:id="@+id/wheel_section"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="@dimen/widget_padding" />
</LinearLayout>
然后在代码中也很简单:
//初始化数据
ArrayList<String> lineList = new ArrayList<>();
HashMap<String, ArrayList<String>> map = new HashMap<>();
lineList .add("A");
lineList .add("B")
for(String section : lineList ){
ArrayList<String> sectionList= new ArrayList<>();
sectionList.add("01");
sectionList.add("02");
map.put(section,sectionList);
}
//设置数据
WheelView wheelView_line = (WheelView) view.findViewById(R.id.wheel_line);
WheelView wheelView_section = (WheelView) view.findViewById(R.id.wheel_section);
WheelView.LineConfig config = new WheelView.LineConfig();
config.setColor(Color.BLUE);//设置分割线颜色
wheelView_line.setLineConfig(config);
wheelView_line.setTextColor(Color.BLUE);//设置选中字体颜色
wheelView_section.setLineConfig(config);
wheelView_section.setTextColor(Color.BLUE);
wheelView_line.setItems(lineList);//设置第一个WheelView
if (!TextUtils.isEmpty(xxxx)) {//如果有值,则初始化选中,这里随便给一值
wheelView_line.setSelectedItem(line);
wheelView_section.setItems(map.get(line));
wheelView_section.setSelectedItem(section);
} else{
//如果没有,则第二个默认选中第一项
wheelView_section.setItems(map.get(lineList.get(0)));
}
wheelView_line.setOnWheelListener(new WheelView.OnWheelListener() {
@Override
public void onSelected(boolean isUserScroll, int index, String item) {
//这里一定要加上判断,如果不是用户滑动的情况,则直接return
//因为这个方法在初始化setSelectItem的时候,也会回调,造成默认选中0;
if (!isUserScroll) {
return;
}
wheelView_section.setItems(map.get(item));
}
});
//最后可以得到两个WheelView选择的值
String content = wheelView_line.getSelectedItem() + wheelView_section.getSelectedItem();