博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
轻量级自定义NumberPicker
阅读量:6684 次
发布时间:2019-06-25

本文共 4517 字,大约阅读时间需要 15 分钟。

前言

一款自定义的WheelPicker控件,它的实现基于Android原生NumberPicker,有占用内存小,滚动灵敏的优点。不同于默认风格NumberPicker的是,这款NumberPicker支持用户通过设置Adapter的方式来实现各种不同的需求,比如循环显示周一至周日,日期选择等。

Github Link:

控件截图

控件说明

WheelPicker的特性:

1.支持自定义字体,文字颜色,文字大小,文字对齐方式。

2.支持设置可见选项个数。

3.支持负数。

4.支持无限滚动,循环滚动。

5.支持用户设置Adapter以满足不同的需求,如:文字选择,时间选择,日期选择等。

6.支持设置摩擦值以改变滑动速度。

7.提供更好的动画效果,支持选中项放大,支持仿IOS的OverScrolling等。

8.兼容NumberPicker的重要方法和接口。

使用方法

1.Gradle:

    compile 'com.super_rabbit.wheel_picker:NumberPicker:1.0.1'复制代码

2.自定义属性

attribute name attribute description
fadingEdgeEnabled enable/disable the fading edge attribute.
max The max index(value) of the picker, can be negative, default is Integer.MAX_VALUE.
min The minimum index(value) of the picker, can be negative, default is Integer.MIN_VALUE
selectedTextColor The text color of selected item.
textColor The text color of unselected item.
textSize Text size.
typeface Text font typeface.
wheelItemCount The visible item count of the picker.
wrapSelectorWheel true if the picker is rounded wrapped, default is false.
align The text alignment, can be LEFT, CENTER, RIGHT.

3.接口文档

function name function description
String getValue(position: Int) return the current value of the picker.
void setValue(value: String) set the current value of the picker.
void setWrapSelectorWheel(wrap: Boolean) Sets whether the selector wheel shown during flinging/scrolling should wrap around the min and max values.
Boolean getWrapSelectorWheel() Gets whether the selector wheel wraps when reaching the min/max value.
void setWheelItemCount(count: Int) Set how many visible item show in the picker.
void setSelectedTextColor(colorId: Int) Set color for current selected item.
void setUnselectedTextColor(colorId: Int) Set color for unselected item.
scrollTo(position: Int) Scroll to a specific position, without animation.
smoothScrollTo(position: Int) Scroll to a specific position, with animation.
scrollToValue(value: String) Scroll to a specific value, without animation.
smoothScrollToValue(value: String) Scroll to a specific value, with animation.
setOnValueChangedListener(onValueChangeListener: OnValueChangeListener) Set listener listening value change.
setAdapter(adapter: WheelAdapter?, indexRangeBasedOnAdapterSize: Boolean = true) Set user define adapter, @indexRangeBasedOnAdapterSize specific if the picker's min~max range is based on adapter's size

4.代码举例

以下代码使用WheelPicker实现了一个简单的日期选择控件:

// Set rounded wrap enablenumberPicker.setSelectorRoundedWrapPreferred(true)// Set wheel item countnumberPicker.setWheelItemCount(5)// Set wheel max indexnumberPicker.setMax(1000)// Set wheel min indexnumberPicker.setMax(1000)// Set selected text colornumberPicker.setSelectedTextColor(R.color.color_4_blue)// Set unselected text colornumberPicker.setUnselectedTextColor(R.color.color_3_dark_blue)// Set user defined adapternumberPicker.setAdapter(WPDayPickerAdapter())// OnValueChangeListenerval context = thisnumberPicker.setOnValueChangeListener(object : OnValueChangeListener{  override fun onValueChange(picker: WheelPicker, oldVal: String, newVal: String) {    val out = String.format("Current: %s", newVal)    Toast.makeText(context, out, Toast.LENGTH_SHORT).show()  }})// Adapter sample /** * Custom wheel picker adapter for implementing a date picker */class WPDayPickerAdapter : WheelAdapter {
    //get item value based on item position in wheel    override fun getValue(position: Int): String {        if (position == 0)            return "Today"        if(position == -1)            return "Yesterday"        if (position == 1)            return "Tomorrow"        val curDate = Date(System.currentTimeMillis())        val rightNow = Calendar.getInstance()        rightNow.time = curDate;        rightNow.add(Calendar.DATE, position)        val simpleDateFormat = SimpleDateFormat("MMM d, yyyy")        return simpleDateFormat.format(rightNow.time)    }    //get item position based on item string value    override fun getPosition(vale: String): Int {        return 0    }    //return a string with the approximate longest text width, for supporting WRAP_CONTENT    override fun getTextWithMaximumLength(): String {        return "Mmm 00, 0000"    }    //return the maximum index    override fun getMaxIndex(): Int {        return Integer.MAX_VALUE    }    //return the minimum index    override fun getMinIndex(): Int {        return Integer.MIN_VALUE    }}复制代码

总结

WheelPicker实现基于原生Android NumberPicker, 除了实现NumberPicker的标准方法外,还提供了更多自定义的属性,项目中如要替换,仅需要将NumberPicker替换成WheelPicker即可。

总之:给个Star吧?

Github Link:

转载地址:http://djsao.baihongyu.com/

你可能感兴趣的文章
安装Sql Server 2005出现“性能监视器计数器要求”错误解决方法。
查看>>
[.NET领域驱动设计实战系列]专题八:DDD案例:网上书店分布式消息队列和分布式缓存的实现...
查看>>
Icomparer和Icomparable集合排序
查看>>
【poi xlsx报错】使用POI创建xlsx无法打开
查看>>
UNIX环境高级编程笔记之文件I/O
查看>>
DIV+CSS规范命名
查看>>
我的2013 Q.E.D
查看>>
2017 Multi-University Training Contest - Team 9 1002&&HDU 6162 Ch’s gift【树链部分+线段树】...
查看>>
4.5. Rspamd
查看>>
ArcMap中的名称冲突问题
查看>>
(转) 一张图解AlphaGo原理及弱点
查看>>
美联邦调查局 FBI 网站被黑,数千特工信息泄露
查看>>
掉电引起的ORA-1172错误解决过程(二)
查看>>
在网站建设过程中主要在哪几个方面为后期的网站优打好根基?
查看>>
【MOS】RAC 环境中最常见的 5 个数据库和/或实例性能问题 (文档 ID 1602076.1)
查看>>
新年图书整理和相关的产品
查看>>
Struts2的核心文件
查看>>
Spring Boot集成Jasypt安全框架
查看>>
GIS基础软件及操作(十)
查看>>
HDOJ 2041 超级楼梯
查看>>