WheelView Android滚轮控件

来源:互联网 发布:linux mysql dump 编辑:程序博客网 时间:2024/05/21 13:57

GitHub地址:https://github.com/venshine/WheelView

在AndroidStudio上使用WheelView的方法:
在Build.Gradle中添加
compile 'com.wx.wheelview:wheelview:1.3.3'

在布局文件中使用WheelView:

<com.wx.wheelview.widget.WheelView            android:id="@+id/wheelview"            android:layout_width="match_parent"            android:layout_height="wrap_content" />

在Activity中使用WheelView:

public class MainActivity extends Activity {        @Override        protected void onCreate(Bundle savedInstanceState) {            super.onCreate(savedInstanceState);            setContentView(R.layout.activity_main)            WheelView wheelView = (WheelView) findViewById(R.id.wheelview);            wheelView.setWheelAdapter(new ArrayWheelAdapter(this)); // 文本数据源            wheelView.setSkin(WheelView.Skin.Common); // common皮肤            wheelView.setWheelData( ?);  // 数据集合        }    }

WheelView的方法:

WheelView的方法

WheelView方法示例:

wheelView.setSkin(WheelView.Skin.Common); // common皮肤

通过看代码,WheelView 皮肤有3种:

public enum Skin { // 滚轮皮肤        Common, Holo, None    }

Common皮肤:
这里写图片描述

Holo皮肤:
这里写图片描述

None皮肤:
这里写图片描述

设置滚轮样式 void setStyle(WheelViewStyle style)
源码如下:

 public static class WheelViewStyle {        public int backgroundColor = -1; // 背景颜色        public int holoBorderColor = -1;   // holo样式边框颜色        public int textColor = -1; // 文本颜色        public int selectedTextColor = -1; // 选中文本颜色        public int textSize = -1;// 文本大小        public int selectedTextSize = -1;   // 选中文本大小        public float textAlpha = -1;  // 文本透明度(0f ~ 1f)        public float selectedTextZoom = -1; // 选中文本放大倍数        public WheelViewStyle() {        }        public WheelViewStyle(WheelViewStyle style) {            this.backgroundColor = style.backgroundColor;            this.holoBorderColor = style.holoBorderColor;            this.textColor = style.textColor;            this.selectedTextColor = style.selectedTextColor;            this.textSize = style.textSize;            this.selectedTextSize = style.selectedTextSize;            this.textAlpha = style.textAlpha;            this.selectedTextZoom = style.selectedTextZoom;        }    }

在Activity中可以这样设置样式:

        WheelView.WheelViewStyle style =  new WheelView.WheelViewStyle();        style.backgroundColor = Color.GREEN;// 背景颜色 绿色        style.holoBorderColor =Color.RED;   // holo样式边框颜色 红色        style.textColor = Color.argb(255,125,125,125); // 文本颜色 灰色        style.selectedTextColor = Color.BLACK; // 选中文本颜色 黑色        style.textSize = 22;// 文本大小//        style.selectedTextSize = 50;   // 选中文本大小        style.textAlpha = 0.5f;  // 文本透明度(0f ~ 1f)        style.selectedTextZoom = 1.5f; // 选中文本放大倍数        wheelView.setStyle(style);

效果如下:
这里写图片描述

需要注意的是:
1、如果同时设置selectedTextSize 和selectedTextZoom ,效果将展示selectedTextSize的值;
2、textAlpha只改变未选中文本的透明度;

void setSelection(final int selection) 设置滚轮位置
int getSelection() 获取滚轮位置

wheelView.setSelection(4);

效果如下:
这里写图片描述
需要注意的是:
这里设置的是滚轮位置的初始值,
当设置为0的时,滚轮位置的文字为“ 第一项”。
当设置为4的时,滚轮位置的文字为“ 第五项”
以此类推。

在程序中,如果没有再次调用 void setSelection(final int selection);方法,每次 getSelection() 获取的值都是一样的。

那我们怎么获取当前滚轮的位置呢?

WheelView 为我们提供了下面2个方法:
int getCurrentPosition() 获取当前滚轮位置
T getSelectionItem() 获取当前滚轮位置的数据

我们在页面上添加一个按钮,点击这个按钮时,弹出一个Toast,上面显示滚轮当前的位置和滚轮当前位置的数据。布局页面比较简单,就不贴出来了。
点击事件如下:

 public void onCilck(View view) {        switch (view.getId()){            case R.id.btn:                Toast.makeText(getApplicationContext()                        ,"wheelView.getCurrentPosition()="+wheelView.getCurrentPosition()                                +"\nwheelView.getSelectionItem()="+wheelView.getSelectionItem()                        ,Toast.LENGTH_LONG).show();                break;            default:                break;        }    }

效果如下:
这里写图片描述

到此位置,我们基本可以使用WheelView 控件了。
当然还有几个问题:
1、数据的类型是泛型,所以我们可以使用自定义的类型。
2、我们通过自定义Adapter,可以让每一项显示更美观或者更丰富。
3、当WheelView 用在Dialog中时,如何把选中的数据返回到Activity中?(需要自定义Listener)。
下一篇博客将解决上述问题。

0 0
原创粉丝点击