Android开源项目WheelView使用示例

看了这篇博客,讲的很好很详细!分享一下!!
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();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值