wheelview地址选择器可以修改scrollview 滚动速度的速度吗

&&国之画&&&& &&
版权所有 京ICP备号-2
迷上了代码!&&&&基于wheelView的自定义日期选择器
&基于wheelView的自定义日期选择器
基于wheelView的自定义日期选择器,支持拓展,样式可以多样
若举报审核通过,可奖励20下载分
被举报人:
举报的资源分:
请选择类型
资源无法下载
资源无法使用
标题与实际内容不符
含有危害国家安全内容
含有反动色情等内容
含广告内容
版权问题,侵犯个人或公司的版权
*详细原因:
VIP下载&&免积分60元/年(1200次)
您可能还需要
Q.为什么我点的下载下不了,但积分却被扣了
A. 由于下载人数众多,下载服务器做了并发的限制。若发现下载不了,请稍后再试,多次下载是不会重复扣分的。
Q.我的积分不多了,如何获取积分?
A. 获得积分,详细见。
完成任务获取积分。
论坛可用分兑换下载积分。
第一次绑定手机,将获得5个C币,C币可。
关注并绑定CSDNID,送10个下载分
下载资源意味着您已经同意遵守以下协议
资源的所有权益归上传用户所有
未经权益所有人同意,不得将资源中的内容挪作商业或盈利用途
CSDN下载频道仅提供交流平台,并不能对任何下载资源负责
下载资源中如有侵权或不适当内容,
本站不保证本站提供的资源的准确性,安全性和完整性,同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
移动开发下载排行
您当前C币:0&&&可兑换 0 下载积分
兑换下载分:&
消耗C币:0&
立即兑换&&
兑换成功你当前的下载分为 。前去下载资源
你下载资源过于频繁,请输入验证码
如何快速获得积分?
你已经下载过该资源,再次下载不需要扣除积分
基于wheelView的自定义日期选择器
所需积分:0
剩余积分:0
扫描微信二维码精彩活动、课程更新抢先知
VIP会员,免积分下载
会员到期时间:日
剩余下载次数:1000
基于wheelView的自定义日期选择器
剩余次数:&&&&有效期截止到:
你还不是VIP会员VIP会员享免积分 . 专属通道极速下载
VIP下载次数已满VIP会员享免积分 . 专属通道极速下载,请继续开通VIP会员
你的VIP会员已过期VIP会员享免积分 . 专属通道极速下载,请继续开通VIP会员使用第三方WheelView制作日期选择器
需求:实现一年月日选择器,默认为当前日期,三者联动,并且在切换年月时,根据是否闰年或者月份天数动态改变日。多说无益,请看下图:
使用的第三方库(/helloJp/WheelView)
此库可兼容到低版本,定制性很强,获取选中数据很方便。详情请戳链接,感谢helloJp的分享。
activity_main.xml放置一个封装好WheelView的自定义控件,没有什么自定义属性
&RelativeLayout xmlns:android="/apk/res/android"
xmlns:tools="/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"&
&test.oubowu.com.datepicker.DatePicker
android:id="@+id/birthday_picker"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_margin="5dp"
android:gravity="center_horizontal" /&
&/RelativeLayout&
date_picker.xml线性布局放置三个WheelView,分别对应年月日:
&?xml version="1.0" encoding="utf-8"?&
&LinearLayout
xmlns:android="/apk/res/android"
xmlns:app="/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/rectangle_bg"
android:padding="2dp"
android:gravity="center_vertical"
android:orientation="horizontal"
&.datepicker.WheelView
android:id="@+id/year"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
app:itemNumber="5"
app:lineColor="@android:color/darker_gray"
app:lineHeight="2dp"
app:maskHight="32dp"
app:noEmpty="true"
app:normalTextColor="@color/material_green_300"
app:normalTextSize="14sp"
app:selectedTextColor="@color/material_light_blue_a700"
app:selectedTextSize="22sp"
app:unitHight="50dp"
&.datepicker.WheelView
android:id="@+id/month"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
app:itemNumber="5"
app:lineColor="@android:color/darker_gray"
app:lineHeight="2dp"
app:maskHight="32dp"
app:noEmpty="true"
app:normalTextColor="@color/material_light_blue_a100"
app:normalTextSize="14sp"
app:selectedTextColor="@color/material_red_300"
app:selectedTextSize="22sp"
app:unitHight="50dp"
&.datepicker.WheelView
android:id="@+id/day"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
app:itemNumber="5"
app:lineColor="@android:color/darker_gray"
app:lineHeight="2dp"
app:maskHight="32dp"
app:noEmpty="true"
app:normalTextColor="@color/material_orange_500"
app:normalTextSize="14sp"
app:selectedTextColor="@color/material_deep_purple_300"
app:selectedTextSize="22sp"
app:unitHight="50dp"
&/LinearLayout&
自定义属性如下,定制性相当强:
Attributes
There are several attributes you can set:
attr 属性 description 描述
divider line color 颜色
lineHeight
divider line height 高度
itemNumber
wheelview show item count 此wheelView显示item的个数
mask height 蒙版高度(normalText的位置)
noEmpty if set true select area can't be null(empty),or could be empty 设置true则选中不能为空,否则可以是空
normalTextColor unSelected Text color 未选中文本颜色
normalTextSize
unSelected Text size 未选中文本字体大小
selectedTextColor
selected Text color 选中文本颜色
selectedTextSize
selected Text size 选中文本字体大小
item unit height 每个item单元的高度
主要看下DatePicker 代码是如何实现联动的:
public class DatePicker extends LinearLayout implements WheelView.OnSelectListener {
public DatePicker(Context context) {
this(context, null);
public DatePicker(Context context, AttributeSet attrs) {
super(context, attrs);
* 获取选择的年
public String getYear() {
return mWheelYear.getSelectedText();
* 获取选择的月
public String getMonth() {
return mWheelMonth.getSelectedText();
* 获取选择的日
public String getDay() {
return mWheelDay.getSelectedText();
private WheelView mWheelY
private WheelView mWheelM
private WheelView mWheelD
protected void onFinishInflate() {
super.onFinishInflate();
LayoutInflater.from(getContext()).inflate(R.layout.date_picker, this);
mWheelYear = (WheelView) findViewById(R.id.year);
mWheelMonth = (WheelView) findViewById(R.id.month);
mWheelDay = (WheelView) findViewById(R.id.day);
// 格式化当前时间,并转换为年月日整型数据
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
String[] split = sdf.format(new Date()).split("-");
int currentYear = Integer.parseInt(split[0]);
int currentMonth = Integer.parseInt(split[1]);
int currentDay = Integer.parseInt(split[2]);
// 设置默认年月日为当前日期
mWheelYear.setData(getYearData(currentYear));
mWheelYear.setDefault(0);
mWheelMonth.setData(getMonthData());
mWheelMonth.setDefault(currentMonth - 1);
mWheelDay.setData(getDayData(getLastDay(currentYear, currentMonth)));
mWheelDay.setDefault(currentDay - 1);
mWheelYear.setOnSelectListener(this);
mWheelMonth.setOnSelectListener(this);
mWheelDay.setOnSelectListener(this);
* 年范围在:1900~今年
* @param currentYear
private ArrayList&String& getYearData(int currentYear) {
ArrayList&String& list = new ArrayList&&();
for (int i = currentY i &= 1900; i--) {
list.add(String.valueOf(i));
private ArrayList&String& getMonthData() {
ArrayList&String& list = new ArrayList&&();
for (int i = 1; i &= 12; i++) {
list.add(String.valueOf(i));
* 日范围在1~lastDay
* @param lastDay
private ArrayList&String& getDayData(int lastDay) {
//ignore condition
ArrayList&String& list = new ArrayList&&();
for (int i = 1; i &= lastD i++) {
list.add(String.valueOf(i));
* 判断是否闰年
* @param year
private boolean isLeapYear(int year) {
return (year % 100 == 0 && year % 400 == 0) || (year % 100 != 0 && year % 4 == 0);
* 获取特定年月对应的天数
* @param year
* @param month
private int getLastDay(int year, int month) {
if (month == 2) {
// 2月闰年的话返回29,防止28
return isLeapYear(year) ? 29 : 28;
// 一三五七八十腊,三十一天永不差
return month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12 ? 31 : 30;
public void endSelect(View view, int id, String text) {
// 滚轮滑动停止后调用
switch (view.getId()) {
case R.id.year:
case R.id.month:
// 记录当前选择的天数
int selectDay = Integer.parseInt(getDay());
// 根据当前选择的年月获取对应的天数
int lastDay = getLastDay(Integer.parseInt(getYear()), Integer.parseInt(getMonth()));
// 设置天数
mWheelDay.setData(getDayData(lastDay));
// 如果选中的天数大于实际天数,那么将默认天数设为实际天数;否则还是设置默认天数为选中的天数
if (selectDay & lastDay) {
mWheelDay.setDefault(lastDay - 1);
mWheelDay.setDefault(selectDay - 1);
public void selecting(View view, int id, String text) {
DatePicker继承于线性布局,在onFinishInflate填充date_picker.xml布局,然后拿到三个wheelview,格式化时间,取出年月日。
WheelView的方法:
1. setData(ArrayList data)
set WheelView data
设置WheelView的数据
2. resetData(ArrayList data)
reset WheelView data ,if you has setData
重置 WheelView的数据,如果已经设置过的话
3. int getSelected()
get selected item index
获取选中项的index
4. String getSelectedText()
get selected item text
获取选中项的文本信息
5. boolean isScrolling
is WheelView is scrolling
获取WheelView是否在滚动中
6. boolean isEnable()
is WheelView is enable
获取wheelView是否可用
7. void setEnable(boolean isEnable)
set WheelView enable
设置WheelView是否可用
8. void setDefault(int index)
set default selected index
设置默认选中项的index
9. int getListSize()
get WheelView item count
获取WheelView的item项个数
10. String getItemText(int index)
get the text by index
获取index位置上的文本数据
11. void setOnSelectListener(OnSelectListener onSelectListener)
set listener on WheelView that can get info when WheelView is scrolling or stop scroll.
对WheelView设置监听,在 滑动过程 或者 滑动停止 返回数据信息。
年的话是以当前年作为最开始选中的,因为我实际用来做生日选择器的;月就是12个月没什么好说的。
mWheelYear.setData(getYearData(currentYear));
private ArrayList&String& getYearData(int currentYear) {
ArrayList&String& list = new ArrayList&&();
for (int i = currentY i &= 1900; i--) {
list.add(String.valueOf(i));
return list;
日的话稍微复杂点,因为月份不同日数不同,闰年二月天数多一天,所以做多判断。
getLastDay(currentYear, currentMonth)就是根据年月算出日数
mWheelDay.setData(getDayData(getLastDay(currentYear, currentMonth)));
private ArrayList&String& getDayData(int lastDay) {
//ignore condition
ArrayList&String& list = new ArrayList&&();
for (int i = 1; i &= lastD i++) {
list.add(String.valueOf(i));
private int getLastDay(int year, int month) {
if (month == 2) {
// 2月闰年的话返回29,防止28
return isLeapYear(year) ? 29 : 28;
// 一三五七八十腊,三十一天永不差
return month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12 ? 31 : 30;
private boolean isLeapYear(int year) {
return (year % 100 == 0 && year % 400 == 0) || (year % 100 != 0 && year % 4 == 0);
我们要联动的话就需要监听三个WheelView的滚动停止对应的数据了,WheelView提供setOnSelectListener给我们监听,这里只需在停止的时候拿到数据做联动即可。
为了直观一点,我们选中的天数会根据年月的不同计算出的实际天数做对比,若选中的天数大于实际天数,会回退到实际天数。
public void endSelect(View view, int id, String text) {
// 滚轮滑动停止后调用
switch (view.getId()) {
case R.id.year:
case R.id.month:
// 记录当前选择的天数
int selectDay = Integer.parseInt(getDay());
// 根据当前选择的年月获取对应的天数
int lastDay = getLastDay(Integer.parseInt(getYear()), Integer.parseInt(getMonth()));
// 设置天数
mWheelDay.setData(getDayData(lastDay));
// 如果选中的天数大于实际天数,那么将默认天数设为实际天数;否则还是设置默认天数为选中的天数
if (selectDay & lastDay) {
mWheelDay.setDefault(lastDay - 1);
mWheelDay.setDefault(selectDay - 1);
实在是简单实用,比官方的好用何止一丢丢。
Demo下载链接如下:
http://download.csdn.net/detail/oushangfeng123/9034101
> 本站内容系网友提交或本网编辑转载,其目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。如涉及作品内容、版权和其它问题,请及时与本网联系,我们将在第一时间删除内容!
基于wheelView的自定义日期选择器 项目要求效果图: 要求 &6月20 星期五& 这一项作为一个整体可以滑动,&7时&.&48分&分别作为一个滑动整体. 系统自带的DatePicker.TimePicker大家都知道,只有这种效果: 百度了很多,试了NumberPicker等都不行,本来打算自 ...
公司的项目要用到时间选择器的一个插件,百度了很久,没有找到合适的,自己就看了下jqueryUI,自己整合了一个比较好的日期选择器,以便后面遇到同样的问题能够解决.下面就贴出部分使用的代码,比较简单,但是很实用!!!&!doctype html& &html lang=&en&& &head& &m ...
在开发系统的调查功能时,需要设计一个可以联动的开始日期/结束日期选择器用来设定用户的调查的起始/结束日期.对于这个联动的日期选择器,我们的设计要求是这样的: 在新增调查时,该调查的开始日期默认为今天,而结束日期则不能选择成今天之前的日期: 当用户修改开始日期时,将判断当前用户设定的结束日期,如果结束日期在修改后的开始日期之后,则不修改结束日期,而如果结束日期 ...
在移动端进行日期选择想必困扰过不少小伙伴,在PC端我们比较丰富的选择,如jQueryUI的datepicker,但这些插件都比较臃肿,而且还要依赖一个同样臃肿的库.或许PC上可以忍受比较大的文件,网速相对快嘛.然而在移动端也面临同样的问题,想用一个日期选择器还得依赖一个庞大的UI库,如mobiscroll.但移动端的网速慢啊,而且要耗流量,用这么一个大家伙就 ...
UIDatePicker是一个可以用来选择日期和时间的控件.除此之外,它也可作为倒计时控件. 日期选择器(UIDatePicker)继承了UIControl,因此UIDatePicker可以作为活动控件使用,能与用户交互,既可在Interface Builder中为UIDatePicker的Value Changed事件绑定IBAction事件处理方法,也可 ...
1.layout &RelativeLayout xmlns:android=&/apk/res/android& xmlns:tools=&/tools& android:layout_width=&quot ...
在构建用户界面时,经常用到日期的输入和选择.
输入日期时,用户可以使用键盘输入,也可以在类似于日历的弹出式对话框中,通过鼠标单击所选日期.
在Flex中可以通过日期输入控件实现输入和选择日期.Flex内置了两种日期输入控件:日期选择器控件(DateChooser Control)和日期区域控件(DateFiel ...
效果如下: ViewController.h 1 #import &UIKit/UIKit.h& 2 3 @interface ViewController : UIViewController 4 @property (strong, nonatomic) UIDatePicker *datePC 5 6 @end ViewCont ...Android自定义可循环的滚动选择器CycleWheelView
替代TimePicker/NumberPicker/WheelView
最近碰到个项目要使用到滚动选择器,原生的NumberPicker可定制性太差,不大符合UI要求。&网上开源的WheelView是用ScrollView写的,不能循环滚动,而且当数据量很大时要加载的Item太多,性能非常低。&然后,还是自己写一个比较靠谱,用的是ListView实现的。写完自己体验了一下,性能不错,再大的数据也不怕了。&感觉不错,重新封装了一下,提供了一些接口可以直接按照自己的需求定制,调用方法在MainActivity中。&不多说了,直接上代码:&CycleWheelView.java:/** * Copyright (C) 2015 * * CycleWheelView.java * * Description:
* * Author: Liao Longhui
* * Ver 1.0, , Liao Longhui, Create file */package com.example.import android.content.Cimport android.graphics.Cimport android.graphics.Cimport android.graphics.ColorFimport android.graphics.Pimport android.graphics.drawable.Dimport android.os.Himport android.util.AttributeSimport android.view.LayoutIimport android.view.Vimport android.view.ViewGimport android.widget.AbsListVimport android.widget.BaseAimport android.widget.ListVimport android.widget.TextVimport java.util.ArrayLimport java.util.L/** * 可循环滚动的选择器 * @author Liao Longhui * */public class CycleWheelView extends ListView {
public static final String TAG = CycleWheelView.class.getSimpleName();
private static final int COLOR_DIVIDER_DEFALUT = Color.parseColor("#747474");
private static final int HEIGHT_DIVIDER_DEFAULT = 2;
private static final int COLOR_SOLID_DEFAULT = Color.parseColor("#3e4043");
private static final int COLOR_SOLID_SELET_DEFAULT = Color.parseColor("#323335");
private static final int WHEEL_SIZE_DEFAULT = 3;
private Handler mH
private CycleWheelViewAdapter mA
private List&String& mL
* Color Of Selected Label
private int mLabelSelectColor = Color.WHITE;
* Color Of Unselected Label
private int mLabelColor = Color.GRAY;
* Gradual Alph
private float mAlphaGradual = 0.7f;
* Color Of Divider
private int dividerColor = COLOR_DIVIDER_DEFALUT;
* Height Of Divider
private int dividerHeight = HEIGHT_DIVIDER_DEFAULT;
* Color of Selected Solid
private int seletedSolidColor = COLOR_SOLID_SELET_DEFAULT;
* Color of Unselected Solid
private int solidColor = COLOR_SOLID_DEFAULT;
* Size Of Wheel , it should be odd number like 3 or greater
private int mWheelSize = WHEEL_SIZE_DEFAULT;
* res Id of Wheel Item Layout
private int mItemLayoutId;
* res Id of Label TextView
private int mItemLabelTvId;
* Height of Wheel Item
private int mItemH
private boolean cylceE
private int mCurrentP
private WheelItemSelectedListener mItemSelectedL
public CycleWheelView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
public CycleWheelView(Context context, AttributeSet attrs) {
super(context, attrs);
public CycleWheelView(Context context) {
super(context);
private void init() {
mHandler = new Handler();
mItemLayoutId = R.layout.item_
mItemLabelTvId = R.id.tv_label_item_
mAdapter = new CycleWheelViewAdapter();
setVerticalScrollBarEnabled(false);
setScrollingCacheEnabled(false);
setCacheColorHint(Color.TRANSPARENT);
setFadingEdgeLength(0);
setOverScrollMode(OVER_SCROLL_NEVER);
setDividerHeight(0);
setAdapter(mAdapter);
setOnScrollListener(new OnScrollListener() {
public void onScrollStateChanged(AbsListView view, int scrollState) {
if (scrollState == SCROLL_STATE_IDLE) {
View itemView = getChildAt(0);
if (itemView != null) {
float deltaY = itemView.getY();
if (deltaY == 0) {
if (Math.abs(deltaY) & mItemHeight / 2) {
smoothScrollBy(getDistance(deltaY), 50);
smoothScrollBy(getDistance(mItemHeight + deltaY), 50);
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
int totalItemCount) {
refreshItems();
private int getDistance(float scrollDistance) {
if (Math.abs(scrollDistance) &= 2) {
return (int) scrollD
} else if (Math.abs(scrollDistance) & 12) {
return scrollDistance & 0 ? 2 : -2;
return (int) (scrollDistance / 6);
private void refreshItems() {
int offset = mWheelSize / 2;
int firstPosition = getFirstVisiblePosition();
int position = 0;
if (getChildAt(0) == null) {
if (Math.abs(getChildAt(0).getY()) &= mItemHeight / 2) {
position = firstPosition +
position = firstPosition + offset + 1;
if (position == mCurrentPositon) {
mCurrentPositon =
if (mItemSelectedListener != null) {
mItemSelectedListener.onItemSelected(getSelection(), getSelectLabel());
resetItems(firstPosition, position, offset);
private void resetItems(int firstPosition, int position, int offset){
for (int i = position - offset - 1; i & position + offset + 1; i++) {
View itemView = getChildAt(i - firstPosition);
if (itemView == null) {
TextView labelTv = (TextView) itemView.findViewById(mItemLabelTvId);
if (position == i) {
labelTv.setTextColor(mLabelSelectColor);
itemView.setAlpha(1f);
labelTv.setTextColor(mLabelColor);
int delta = Math.abs(i - position);
double alpha = Math.pow(mAlphaGradual, delta);
itemView.setAlpha((float) alpha);
* 设置滚轮的刻度列表
* @param labels
public void setLabels(List&String& labels) {
mAdapter.setData(mLabels);
mAdapter.notifyDataSetChanged();
initView();
* 设置滚轮滚动监听
* @param mItemSelectedListener
public void setOnWheelItemSelectedListener(WheelItemSelectedListener mItemSelectedListener) {
this.mItemSelectedListener = mItemSelectedL
* 获取滚轮的刻度列表
public List&String& getLabels() {
* 设置滚轮是否为循环滚动
* @param enable true-循环 false-单程
public void setCycleEnable(boolean enable) {
if (cylceEnable != enable) {
cylceEnable =
mAdapter.notifyDataSetChanged();
setSelection(getSelection());
* 滚动到指定位置
public void setSelection(final int position) {
mHandler.post(new Runnable() {
public void run() {
CycleWheelView.super.setSelection(getPosition(position));
private int getPosition(int positon) {
if (mLabels == null || mLabels.size() == 0) {
if (cylceEnable) {
int d = Integer.MAX_VALUE / 2 / mLabels.size();
return positon + d * mLabels.size();
* 获取当前滚轮位置
public int getSelection() {
if (mCurrentPositon == 0) {
mCurrentPositon = mWheelSize / 2;
return (mCurrentPositon - mWheelSize / 2) % mLabels.size();
* 获取当前滚轮位置的刻度
public String getSelectLabel() {
int position = getSelection();
position = position & 0 ? 0 :
return mLabels.get(position);
} catch (Exception e) {
return "";
* 如果需要自定义滚轮每个Item,调用此方法设置自定义Item布局,自定义布局中需要一个TextView来显示滚轮刻度
* @param itemResId 布局文件Id
* @param labelTvId 刻度TextView的资源Id
public void setWheelItemLayout(int itemResId, int labelTvId) {
mItemLayoutId = itemResId;
mItemLabelTvId = labelTvId;
mAdapter = new CycleWheelViewAdapter();
mAdapter.setData(mLabels);
setAdapter(mAdapter);
initView();
* 设置未选中刻度文字颜色
* @param labelColor
public void setLabelColor(int labelColor) {
this.mLabelColor = labelC
resetItems(getFirstVisiblePosition(), mCurrentPositon, mWheelSize/2);
* 设置选中刻度文字颜色
* @param labelSelectColor
public void setLabelSelectColor(int labelSelectColor) {
this.mLabelSelectColor = labelSelectC
resetItems(getFirstVisiblePosition(), mCurrentPositon, mWheelSize/2);
* 设置滚轮刻度透明渐变值
* @param alphaGradual
public void setAlphaGradual(float alphaGradual) {
this.mAlphaGradual = alphaG
resetItems(getFirstVisiblePosition(), mCurrentPositon, mWheelSize/2);
* 设置滚轮可显示的刻度数量,必须为奇数,且大于等于3
* @param wheelSize
* @throws CycleWheelViewException 滚轮数量错误
public void setWheelSize(int wheelSize) throws CycleWheelViewException
if (wheelSize & 3 || wheelSize % 2 != 1) {
throw new CycleWheelViewException("Wheel Size Error , Must Be 3,5,7,9...");
mWheelSize = wheelS
initView();
* 设置块的颜色
* @param unselectedSolidColor 未选中的块的颜色
* @param selectedSolidColor 选中的块的颜色
public void setSolid(int unselectedSolidColor, int selectedSolidColor){
this.solidColor = unselectedSolidC
this.seletedSolidColor = selectedSolidC
initView();
* 设置分割线样式
* @param dividerColor
分割线颜色
* @param dividerHeight 分割线高度(px)
public void setDivider(int dividerColor, int dividerHeight){
this.dividerColor = dividerC
this.dividerHeight = dividerH
@SuppressWarnings("deprecation")
private void initView() {
mItemHeight = measureHeight();
ViewGroup.LayoutParams lp = getLayoutParams();
lp.height = mItemHeight * mWheelS
mAdapter.setData(mLabels);
mAdapter.notifyDataSetChanged();
Drawable backgroud = new Drawable() {
public void draw(Canvas canvas) {
int viewWidth = getWidth();
Paint dividerPaint = new Paint();
dividerPaint.setColor(dividerColor);
dividerPaint.setStrokeWidth(dividerHeight);
Paint seletedSolidPaint = new Paint();
seletedSolidPaint.setColor(seletedSolidColor);
Paint solidPaint = new Paint();
solidPaint.setColor(solidColor);
canvas.drawRect(0, 0, viewWidth, mItemHeight * (mWheelSize / 2), solidPaint);
canvas.drawRect(0, mItemHeight * (mWheelSize / 2 + 1), viewWidth, mItemHeight
* (mWheelSize), solidPaint);
canvas.drawRect(0, mItemHeight * (mWheelSize / 2), viewWidth, mItemHeight
* (mWheelSize / 2 + 1), seletedSolidPaint);
canvas.drawLine(0, mItemHeight * (mWheelSize / 2), viewWidth, mItemHeight
* (mWheelSize / 2), dividerPaint);
canvas.drawLine(0, mItemHeight * (mWheelSize / 2 + 1), viewWidth, mItemHeight
* (mWheelSize / 2 + 1), dividerPaint);
public void setAlpha(int alpha) {
public void setColorFilter(ColorFilter cf) {
public int getOpacity() {
setBackgroundDrawable(backgroud);
private int measureHeight() {
View itemView = LayoutInflater.from(getContext()).inflate(mItemLayoutId, null);
itemView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
itemView.measure(w, h);
int height = itemView.getMeasuredHeight();
// int width = view.getMeasuredWidth();
public interface WheelItemSelectedListener {
public void onItemSelected(int position, String label);
public class CycleWheelViewException extends Exception {
private static final long serialVersionUID = 1L;
public CycleWheelViewException(String detailMessage) {
super(detailMessage);
public class CycleWheelViewAdapter extends BaseAdapter {
private List&String& mData = new ArrayList&String&();
public void setData(List&String& mWheelLabels) {
mData.clear();
mData.addAll(mWheelLabels);
public int getCount() {
if (cylceEnable) {
return Integer.MAX_VALUE;
return mData.size() + mWheelSize - 1;
public Object getItem(int position) {
return "";
public long getItemId(int position) {
public boolean isEnabled(int position) {
return false;
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(mItemLayoutId, null);
TextView textView = (TextView) convertView.findViewById(mItemLabelTvId);
if (position & mWheelSize / 2
|| (!cylceEnable && position &= mData.size() + mWheelSize / 2)) {
textView.setText("");
convertView.setVisibility(View.INVISIBLE);
textView.setText(mData.get((position - mWheelSize / 2) % mData.size()));
convertView.setVisibility(View.VISIBLE);
return convertV
}}&MainActivity.java:public class MainActivity extends Activity {
private CycleWheelView cycleWheelView0,cycleWheelView1, cycleWheelView2;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cycleWheelView0 = (CycleWheelView) findViewById(R.id.cycleWheelView);
List&String& labels = new ArrayList&&();
for (int i = 0; i & 12; i++) {
labels.add("" + i);
cycleWheelView0.setLabels(labels);
cycleWheelView0.setAlphaGradual(0.5f);
cycleWheelView0.setOnWheelItemSelectedListener(new WheelItemSelectedListener() {
public void onItemSelected(int position, String label) {
Log.d("test", label);
cycleWheelView1 = (CycleWheelView) findViewById(R.id.cycleWheelView1);
List&String& labels1 = new ArrayList&&();
for (int i = 0; i & 24; i++) {
labels1.add("" + i);
cycleWheelView1.setLabels(labels1);
cycleWheelView1.setWheelSize(5);
} catch (CycleWheelViewException e) {
e.printStackTrace();
cycleWheelView1.setSelection(2);
cycleWheelView1.setWheelItemLayout(R.layout.item_cyclewheel_custom, R.id.tv_label_item_wheel_custom);
cycleWheelView1.setOnWheelItemSelectedListener(new WheelItemSelectedListener() {
public void onItemSelected(int position, String label) {
Log.d("test", label);
cycleWheelView2 = (CycleWheelView) findViewById(R.id.cycleWheelView2);
List&String& labels2 = new ArrayList&&();
for (int i = 0; i & 60; i++) {
labels2.add("" + i);
cycleWheelView2.setLabels(labels2);
cycleWheelView2.setWheelSize(7);
} catch (CycleWheelViewException e) {
e.printStackTrace();
cycleWheelView2.setCycleEnable(true);
cycleWheelView2.setSelection(30);
cycleWheelView2.setAlphaGradual(0.6f);
cycleWheelView2.setDivider(Color.parseColor("#abcdef"), 2);
cycleWheelView2.setSolid(Color.WHITE,Color.WHITE);
cycleWheelView2.setLabelColor(Color.BLUE);
cycleWheelView2.setLabelSelectColor(Color.RED);
cycleWheelView2.setOnWheelItemSelectedListener(new WheelItemSelectedListener() {
public void onItemSelected(int position, String label) {
Log.d("test", label);
}}Item_cyclewheel.xml:&?xml version="1.0" encoding="utf-8"?&&RelativeLayout xmlns:android="/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:background="@android:color/transparent" &
android:id="@+id/tv_label_item_wheel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:singleLine="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" /&&/RelativeLayout&Item_cyclewheel_custom.xml:&?xml version="1.0" encoding="utf-8"?&&RelativeLayout xmlns:android="/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:background="@android:color/transparent" &
android:id="@+id/tv_label_item_wheel_custom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true" /&
&ImageView
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:src="@drawable/ic_launcher" /&&/RelativeLayout&activity_main.xml:&LinearLayout xmlns:android="/apk/res/android"
xmlns:tools="/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" &
&com.example.wheelviewdemo.CycleWheelView
android:id="@+id/cycleWheelView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" &
&/com.example.wheelviewdemo.CycleWheelView&
&com.example.wheelviewdemo.CycleWheelView
android:id="@+id/cycleWheelView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" &
&/com.example.wheelviewdemo.CycleWheelView&
&com.example.wheelviewdemo.CycleWheelView
android:id="@+id/cycleWheelView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" &
&/com.example.wheelviewdemo.CycleWheelView&
&/LinearLayout&&
最新教程周点击榜
微信扫一扫

我要回帖

更多关于 wheelview 滚动监听 的文章

 

随机推荐