对于android初学者,最烦的就是学习自定义控件。原生的控件不是丑就是无法满足需求,不得以我们只好自己重写控件的方法,下面我就为大家介绍一下某大神自定义的wheel控件Android滚轮控件,基于ListView实现,可以自定义样式。,此控件可以实现城市选择的多级联动,时间选择等多种功能,具体自己DIY呗!
效果图:
首先看一下布局main_layout
android:orientation="vertical">
接着看MainActivity
/**
* 联动WheelView
*/
private void initWheel1() {
mainWheelView = (WheelView) findViewById(R.id.main_wheelview);
mainWheelView.setWheelAdapter(new ArrayWheelAdapter(this)); //设置滚轮数据适配器s
mainWheelView.setSkin(WheelView.Skin.Holo); //设置背景颜色
mainWheelView.setWheelData(createMainDatas()); //设置滚轮数据
WheelView.WheelViewStyle style = new WheelView.WheelViewStyle(); //设置选中与未选中字体的样式
style.selectedTextSize = 20;
style.textSize = 16;
mainWheelView.setStyle(style);
subWheelView = (WheelView) findViewById(R.id.sub_wheelview);
subWheelView.setWheelAdapter(new ArrayWheelAdapter(this));
subWheelView.setSkin(WheelView.Skin.Holo);
subWheelView.setWheelData(createSubDatas().get(createMainDatas().get(mainWheelView.getSelection())));
subWheelView.setStyle(style);
mainWheelView.join(subWheelView); //连接副WheelView 市
mainWheelView.joinDatas(createSubDatas()); //副WheelView 市数据
childWheelView = (WheelView) findViewById(R.id.child_wheelview);
childWheelView.setWheelAdapter(new ArrayWheelAdapter(this));
childWheelView.setSkin(WheelView.Skin.Holo);
childWheelView.setWheelData(createChildDatas().get(createSubDatas().get(createMainDatas().get(mainWheelView
.getSelection())).get(subWheelView.getSelection())));
childWheelView.setStyle(style);
subWheelView.join(childWheelView); //连接副WheelView 区县
subWheelView.joinDatas(createChildDatas()); //区县 数据
}
private List如此便可以使用城市选择器了,同时你也可通过setSkin()方法更改WheelView的样式createMainDatas() { String[] strings = {"黑龙江", "吉林", "辽宁"}; return Arrays.asList(strings); } private HashMap > createSubDatas() { HashMap > map = new HashMap >(); String[] strings = {"黑龙江", "吉林", "辽宁"}; String[] s1 = {"哈尔滨", "齐齐哈尔", "大庆"}; String[] s2 = {"长春", "吉林"}; String[] s3 = {"沈阳", "大连", "鞍山", "抚顺"}; String[][] ss = {s1, s2, s3}; for (int i = 0; i < strings.length; i++) { map.put(strings[i], Arrays.asList(ss[i])); } return map; } private HashMap > createChildDatas() { HashMap > map = new HashMap >(); String[] strings = {"哈尔滨", "齐齐哈尔", "大庆", "长春", "吉林", "沈阳", "大连", "鞍山", "抚顺"}; String[] s1 = {"道里区", "道外区", "南岗区", "香坊区"}; String[] s2 = {"龙沙区", "建华区", "铁锋区"}; String[] s3 = {"红岗区", "大同区"}; String[] s11 = {"南关区", "朝阳区"}; String[] s12 = {"龙潭区"}; String[] s21 = {"和平区", "皇姑区", "大东区", "铁西区"}; String[] s22 = {"中山区", "金州区"}; String[] s23 = {"铁东区", "铁西区"}; String[] s24 = {"新抚区", "望花区", "顺城区"}; String[][] ss = {s1, s2, s3, s11, s12, s21, s22, s23, s24}; for (int i = 0; i < strings.length; i++) { map.put(strings[i], Arrays.asList(ss[i])); } return map; }
主要方法有
method 方法 | description 描述 |
---|---|
void setWheelAdapter(BaseWheelAdapter adapter) | 设置滚轮数据源适配器(required) |
void setWheelData(List list) | 设置滚轮数据(required) |
void setLoop(boolean loop) | 设置滚轮是否循环滚动 |
void setWheelSize(int wheelSize) | 设置滚轮个数 |
void setSkin(Skin skin) | 设置皮肤风格 |
Skin getSkin() | 获得皮肤风格 |
void setStyle(WheelViewStyle style) | 设置滚轮样式 |
WheelViewStyle getStyle() | 获得滚轮样式 |
void setWheelClickable(boolean clickable) | 设置滚轮选中项是否可点击 |
void setSelection(final int selection) | 设置滚轮位置 |
int getSelection() | 获取滚轮位置 |
void join(WheelView wheelView) | 连接副WheelView(联动设置) |
void joinDatas(HashMap> map) | 副WheelView数据(联动设置) |
int getCurrentPosition() | 获取当前滚轮位置 |
T getSelectionItem() | 获取当前滚轮位置的数据 |
void setExtraText(String text, int textColor, int textSize, int margin) | 设置选中行附加文本 |
int getWheelCount() | 获得滚轮数据总数 |
void setOnWheelItemSelectedListener(OnWheelItemSelectedListener onWheelItemSelectedListener) | 设置滚轮滑动停止时事件,监听滚轮选中项 |
void setOnWheelItemClickListener(OnWheelItemClickListener onWheelItemClickListener) | 设置滚轮选中项点击事件 |
WheelViewDialog setDialogStyle(int color) | 设置Dialog外观颜色 |
接着让我给大家分析一下作者如何实现的,你通过github或我的分享下载作者的源码,解压可在WheelView-master\wheelview\src\main\java\com\wx\wheelview下看到如图
1.adapter 是数据源适配器,它是数据和UI(View)之间一个重要的纽带。在常见的View(ListView,GridView)等地方都需要用到Adapter。如图1直观的表达了Data、Adapter、View三者的关系。
2.作者定义的常量,bean类及错误类
3.作者定义的方法类
4.自定义的WheelView类
感兴趣的朋友可以进一步下载源码阅读