輕量級自定義NumberPicker
前言
一款自定義的WheelPicker控制元件,它的實現基於Android原生NumberPicker,有佔用記憶體小,滾動靈敏的優點。不同於預設風格NumberPicker的是,這款NumberPicker支援使用者通過設定Adapter的方式來實現各種不同的需求,比如迴圈顯示週一至週日,日期選擇等。
Github Link: ofollow,noindex">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