轻量级自定义NumberPicker

作者: 85d4ed0cbe59 | 来源:发表于2018-09-28 15:32 被阅读11次

    前言


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

    Github Link:GitHub - SuperRabbitD/NumberPicker

    控件截图


    image image

    控件说明


    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 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

    相关文章

      网友评论

        本文标题:轻量级自定义NumberPicker

        本文链接:https://www.haomeiwen.com/subject/fejpoftx.html