轻量级自定义NumberPicker

基于Android原生NumberPicker的自定义WheelPicker控件,支持无限滚动、循环滚动及自定义字体、颜色等,适用于多种场景如日期、时间选择等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言

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

Github Link:GitHub - SuperRabbitD/NumberPicker

控件截图

控件说明

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 nameattribute description
fadingEdgeEnabledenable/disable the fading edge attribute.
maxThe max index(value) of the picker, can be negative, default is Integer.MAX_VALUE.
minThe minimum index(value) of the picker, can be negative, default is Integer.MIN_VALUE
selectedTextColorThe text color of selected item.
textColorThe text color of unselected item.
textSizeText size.
typefaceText font typeface.
wheelItemCountThe visible item count of the picker.
wrapSelectorWheeltrue if the picker is rounded wrapped, default is false.
alignThe text alignment, can be LEFT, CENTER, RIGHT.
3.接口文档
function namefunction 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 enable

numberPicker.setSelectorRoundedWrapPreferred(true)

// Set wheel item count

numberPicker.setWheelItemCount(5)

// Set wheel max index

numberPicker.setMax(1000)

// Set wheel min index

numberPicker.setMax(1000)

// Set selected text color

numberPicker.setSelectedTextColor(R.color.color_4_blue)

// Set unselected text color

numberPicker.setUnselectedTextColor(R.color.color_3_dark_blue)

// Set user defined adapter

numberPicker.setAdapter(WPDayPickerAdapter())

// OnValueChangeListener

val context = this

numberPicker.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:GitHub - SuperRabbitD/NumberPicker

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值