Git Product home page Git Product logo

numberpickerview's Introduction

NumberPickerView

Android Arsenal

Another NumberPicker with more flexible attributes on Android platform

Chinese

Forewords

some android projects use the android.widget.NumberPicker to provide alternative choices, but the default style of NumberPicker has some inflexible attibutes, and complicated to be customized.This NumberPickerView extends from View, and provide a friendly experience.

ScreenShot

Example Image
picking an item dynamically

Example Image

Example Image

Example Image
a small project powered by NumberPickerView, a view which can pick or jump to a certain date with two modes, in Gregorian mode or in Chinese Lunar mode. The gif seem to be a little lag, but actually it runs smoothly. Check the project here: https://github.com/Carbs0126/GregorianLunarCalendar

Introduction

NumberPickerViewextends from View and has almost all functions of android.widget.NumberPicker except inputting fuction by EditText, but it has some advanced features, here are these two views' differences below:

Features of android.widget.NumberPicker

  1. the NumberPicker's viewport can only show three items;
  2. the value of friction is big, you can not pick a new value smoothly by fling;
  3. no animation if you use setValue() in java code to set to a new value;
  4. no animation if you use setDisplayValues() to change the content of NumberPicker;
  5. has a bug if you set wrap mode by using setWrapSelectorWheel(), sometimes NumberPicker will not refresh canvas after setWrapSelectorWheel() until it receive a new MotionEvent;
  6. no text hint at the center position;
  7. cannot control NumberPicker to smoothly scroll to a certain item (position);
  8. NumberPicker class in early version of some customized framework has bugs when changing maxValue and displayedValues.

Capabilities of NumberPickerView

  1. the NumberPickerView's viewport can show more than three items;
  2. able to set the value of friction in java code, you can pick a new value smoothly by fling, in java code, you can use the code below to make friction be twice as former
    mNumberPickerView.setFriction(2 * ViewConfiguration.get(mContext).getScrollFriction());
  3. items' texts has animation between selected mode and normal mode, including Gradient textColor and Gradient textSize;
  4. able to choose if use animation when changing displayedValues;
  5. able to setWrapSelectorWheel() dynamically in java code or in xml;
  6. able to set a hint text at the center position, default is empty; can change the hint text's color and textSize;
  7. able to scroll smoothly to a centain item (position);
  8. support wrap_content mode,support item's padding
  9. has some other attibutes to refine UI
  10. not respond onValueChanged() during scrolling
  11. press the certain item, NumberPickerView will scroll to this item automatically
  12. you can set if the onValueChanged callbacks invoked in main thread or in sub thread;
  13. NumberPickerView has some same compatible fuctions and interfaces with NumberPicker, this makes it easier to change NumberPicker to NumberPickerView in project:
    //compatible fuctions
    setOnValueChangedListener()
    setOnScrollListener()
    setDisplayedValues()/getDisplayedValues()
    setWrapSelectorWheel()/getWrapSelectorWheel()
    setMinValue()/getMinValue()
    setMaxValue()/getMaxValue()
    setValue()/getValue()

    //compatible interfaces
    OnValueChangeListener
    OnScrollListener

How to use

1.import to project

    implementation 'cn.carbswang.android:NumberPickerView:1.2.0'

or

    <dependency>
      <groupId>cn.carbswang.android</groupId>
      <artifactId>NumberPickerView</artifactId>
      <version>1.2.0</version>
      <type>pom</type>
    </dependency>

2.add a NumberPickerView in xml

    <cn.carbswang.android.numberpickerview.library.NumberPickerView
        android:id="@+id/picker"
        android:layout_width="wrap_content"
        android:layout_height="240dp"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:background="#11333333"
        android:contentDescription="test_number_picker_view"
        app:npv_RespondChangeOnDetached="false"
        app:npv_ItemPaddingHorizontal="5dp"
        app:npv_ItemPaddingVertical="5dp"
        app:npv_ShownCount="5"
        app:npv_TextSizeNormal="16sp"
        app:npv_TextSizeSelected="20sp"
        app:npv_WrapSelectorWheel="true"/>

3.control NumberPickerView in Java code 1)if the displayedValues in NumberPickerView will NOT change, you can set data by this way: (same as using NumberPicker)

        picker.setMinValue(minValue);
        picker.setMaxValue(maxValue);
        picker.setValue(value);

2)if the displayedValues in NumberPickerView will change, you can set data by this way: (same as using NumberPicker)

        int minValue = getMinValue();
        int oldMaxValue = getMaxValue();
        int oldSpan = oldMaxValue - minValue + 1;
        int newMaxValue = display.length - 1;
        int newSpan = newMaxValue - minValue + 1;
        if (newSpan > oldSpan) {
            setDisplayedValues(display);
            setMaxValue(newMaxValue);
        } else {
            setMaxValue(newMaxValue);
            setDisplayedValues(display);
        }

OR use NumberPickerView's method:
refreshByNewDisplayedValues(String[] display)
but make sure the minValue will NOT change before and after using this method, and display should not be null, and its length should be greater than 0.

4.NumberPickerView also have methods to scroll smoothly
public void smoothScrollToValue(int fromValue, int toValue, boolean needRespond)

the same point between this method and setValue(int)is you can set the current picked item dynamically, the difference is this method can make NumberPickerView scroll smoothly from fromValue to toValue by choosing short distance, the third argument needRespond is a boolean flag used to set if you want NumberPickerView invoke its onValueChanged callback when scrolling finished this time, because if several NumberPickerViews have interconnections, the early stopped NumberPickerView invoking callbacks will effect the latter stopped ones. So you can set this flag to be false to avoid invoking onValueChanged callback this time.

and you'd better not use this method in onCreate(Bundle savedInstanceState), if have to do this, you can use in this way:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //some code ...
        mNumberPickerView.post(new Runnable() {
            @Override
            public void run() {
                //call smoothScrollToValue()
            }
        });
    }

5.introduction of attibutes in xml

    <declare-styleable name="NumberPickerView">
        <attr name="npv_ShownCount" format="reference|integer" />//the count of shown items , default is 3
        <attr name="npv_ShowDivider" format="reference|boolean" />//if show dividers
        <attr name="npv_DividerColor" format="reference|color" />//color of two dividers
        <attr name="npv_DividerMarginLeft" format="reference|dimension" />//divider's margin to the left
        <attr name="npv_DividerMarginRight" format="reference|dimension" />//divider's margin to the right
        <attr name="npv_DividerHeight" format="reference|dimension" />//divider's height
        <attr name="npv_TextColorNormal" format="reference|color" />//unselected textColor
        <attr name="npv_TextColorSelected" format="reference|color" />//selected textColor
        <attr name="npv_TextColorHint" format="reference|color" />//hint text color (the text in the center item)
        <attr name="npv_TextSizeNormal" format="reference|dimension" />//unselected textSize
        <attr name="npv_TextSizeSelected" format="reference|dimension" />//selected textColor
        <attr name="npv_TextSizeHint" format="reference|dimension" />//hint text size
        <attr name="npv_TextArray" format="reference" />//displayedValues
        <attr name="npv_MinValue" format="reference|integer" />//minValue, see as setMinValue()
        <attr name="npv_MaxValue" format="reference|integer" />//maxValue, see as setMaxValue()
        <attr name="npv_WrapSelectorWheel" format="reference|boolean" />//if set wrap mode, see as setWrapSelectorWheel(boolean)
        <attr name="npv_HintText" format="reference|string" />//hint text
        <attr name="npv_EmptyItemHint" format="reference|string" />//empty item's text,only shown when WrapSelectorWheel==false or displayedValues length not large than showCount
        <attr name="npv_MarginStartOfHint" format="reference|dimension" />//distance between hint and the right side of the max wide text in displayedValues
        <attr name="npv_MarginEndOfHint" format="reference|dimension" />//distance between hint and the right side of the view
        <attr name="npv_ItemPaddingHorizontal" format="reference|dimension" />//item's horizontal padding, used for wrap_content mode
        <attr name="npv_ItemPaddingVertical" format="reference|dimension" />//item's vertical padding, used for wrap_content mode
        <attr name="npv_RespondChangeOnDetached" format="reference|boolean" />//for reusable `Dialog/PopupWindow`.
        <attr name="npv_SelectedItemBackground" format="reference" />//for selected item background.
        //If `Dialog/PopupWindow` is hiding meanwhile `NumberPickerView` is still scrolling, then we need it to stop scrolling 
        //and respond (or not) `OnValueChange` callbacks and change the previous picked value. 
        //Add a new attr `npv_RespondChangeOnDetached` as a flag to set if respondding `onValueChange` callbacks, 
        //mainly for multi linked NumberPickerViews to correct other NumberPickerView's position or value.
        //But I highly recommend every time showing a `Dialog/PopupWindow` please set certain data for NumberPickerView, 
        //and set `npv_RespondChangeOnDetached` false to avoid respondding `onValueChange` callbacks. 
        //See dialog in my `GregorianLunarCalendar` project. 

        <attr name="npv_RespondChangeInMainThread" format="reference|boolean" />//set if the `onValueChanged` callbacks invoked 
        // in mainThread or in subThread, default is true, in mainThread. set it false if you want to run `onValueChanged` in 
        // subThread.

    //these attibutes below are used under wrap_content mode, 
    //and if you want to change displayedValues with out making NumberPickerView changing its original position(four points of this view), 
    //then you should added these attrs to set a max width
        <!--just used to measure maxWidth for wrap_content without hint,
            the string array will never be displayed.
            you can set this attr if you want to keep the wraped numberpickerview
            width unchanged when alter the content list-->
        <attr name="npv_AlternativeTextArrayWithMeasureHint" format="reference" />//represents the maxWidth of displayedValues item plus hint width, including hint, the maxWidth used in onMeasure fuction must equal or be larger than this
        <attr name="npv_AlternativeTextArrayWithoutMeasureHint" format="reference" />//represents the maxWidth of displayedValues item, exclude hint text.
        <!--the max length of hint content-->
        <attr name="npv_AlternativeHint" format="reference|string" />//represents the maxWidth of hint text
    </declare-styleable>

Version

1.2.0

1.merge some pull requests
2.delete some dependence in library

1.1.0

1.refine the duration of position rewising
2.refine the interval of sending refreshing message
3.refine the sample's UI

1.0.9

1.add attr app:npv_RespondChangeInMainThread="true" to set if the onValueChanged callbacks invoked in mainThread or in subThread, default is true, in mainThread. set it false if you want to run onValueChanged in subThread. 2.update TimePickerActivity example, to give a How-To-Use of app:npv_RespondChangeInMainThread="true". 3.fix bug: when change displayed values, if it is scrolling, then the new displayed values' position is not rewised

1.0.8

1.modify method stopScrolling, add scroll to current Y method before abortAnimation() is invoked
2.modify npv_RespondChangeOnDetached's default value to false

1.0.7

1.refine code in onDetachToWindow() to respond callbacks or not, for reusable Dialog/PopupWindow.
If Dialog/PopupWindow is hiding meanwhile NumberPickerView is still scrolling, then we need it to stop scrolling and respond (or not) OnValueChange callbacks and change the previous picked value.
Add a new attr npv_RespondChangeOnDetached as a flag to set if respondding onValueChange callbacks, mainly for multi linked NumberPickerViews to correct other NumberPickerView's position or value.
But I highly recommend every time showing a Dialog/PopupWindow please set certain data for NumberPickerView, and set npv_RespondChangeOnDetached false to avoid respondding onValueChange callbacks. See dialog in my GregorianLunarCalendar project.
These codes are not elegant, If you have any idea, please let me know, thank you.

1.0.6

1.add code in onDetachToWindow() to respond callbacks, for reusable Dialog/PopupWindow.

1.0.5

1.in method onAttachToWindow(), add code to judge if mHandlerThreadhas been quit(), this is to avoid of 'can not correct position when show the same Dialog(or PopupWindow) twice '

1.0.4

1.modify some attrs' name

1.0.3

1.fix bug : cannot scroll in ScrollView. Thanks Elektroktay's and anjiao's issues

Mechanisms

1.how to generate scrolling animation

Scroller + VelocityTracker + onDraw(Canvas canvas)

2.how to correct position automatically when scrolling finished

Handler refresh current position

3.how to generate Gradient effection

by calculating the current coordinate, get the items which should be shown and the positions of each shown items, comparing the positions and center coordinate of NumberPickerView, get the current color and size of item's text

how to CHANGE NumberPicker to NumberPickerView

just modify NumberPicker text into NumberPickerView in java code and xml, keep the methods and interfaces called by NumberPicker the same.

And something important

UI design inspired by Meizu company, a mobile phone Manufacturer in china.
A big thanks to google and meizu

enjoy

License

Copyright 2016 Carbs.Wang (NumberPickerView)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

numberpickerview's People

Contributors

carbs0126 avatar diegocstn avatar valerasetrakov avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

numberpickerview's Issues

用这个做datepicker限制时间时,如2015。10.13-10.16.10.13时,有个坑

private void setPicker(NumberPickerView picker, int start, int end) {
String[] texts = new String[end - start + 1];
for (int i = start; i <= end; ++i) {
texts[i - start] = String.valueOf(i);
}

int size = Math.abs(Math.min(start, picker.getMinValue()) - Math.max(end, picker.getMaxValue())) + 1;
picker.setDisplayedValues(new String[size]);
picker.setMinValue(start);
picker.setMaxValue(end);
picker.setValue(start);
picker.setDisplayedValues(texts);

}
这样的话没问题,setMaxValue如果和setMinValue对调时,滚动就会划出边界,还有闪退,建议给点提示。

Stop scrolling animation when button press

THANK YOU SO MUCH for sharing and your hard work. Would like to add a bit more enhancement :)

Maybe, it's better to keep this method public so we can call when user press a button

Use case -
User (fling) scrolling > Quickly press OK/Pick button > number picker should stop at that value and stop scrolling animation

Making this method public already does stop the scroll animation and stop at picked value. 👍

public void stopScrolling() {
        if (mScroller != null) {
           ...
        }
    }

這個庫支援不連續的數字嗎?

你好,沒想到你這個庫我已經用了兩個項目,這個需求在第三個項目才出現。

比如說我想用你的庫做一個時間的選擇器,分鐘只可以選 0 和 30,

那我基本上是這樣做:

        minutePicker.setDisplayedValues(new String[2]{"00", "30"});
        minutePicker.setMinValue(0);
        minutePicker.setMaxValue(30);

可是這樣會拋異常,因為你會檢查 (maxValue - mMinValue + 1 > mDisplayedValues.length),如果為true會拋異常。這樣看來你好似只支援連續的數字,是這樣嗎?

(當然了我可以自己弄一個List,只監聽picker返回的position。是這樣嗎?)

字数过多时显示不完整

我在项目中加了三个该View用作省市区三级联动 每个宽度都是屏幕的1/3 但是当遇到某些市名过长的时候 文字左右会显示不下 直接被切掉了

Error causing NumberPicker to not snap to the closest value after a fling/scroll

Hey it's me again! 😄

I'm using the NumberPicker inside a RecyclerView, inside a NestedScrollView. I have the version 1.0.3. I noticed that when I call nofifyItemChanged(int) on the item that contains the NumberPicker, the NumberPicker sometimes goes "numb", as in it doesn't attach to the closest value when I scroll anymore. When this happens, it gives me the following exception:
07-09 14:49:25.379 7829-7829/com.*****.******** W/MessageQueue: Handler (cn.carbswang.android.numberpickerview.library.NumberPickerView$1) {d43ee1b} sending message to a Handler on a dead thread java.lang.IllegalStateException: Handler (cn.carbswang.android.numberpickerview.library.NumberPickerView$1) {d43ee1b} sending message to a Handler on a dead thread at android.os.MessageQueue.enqueueMessage(MessageQueue.java:325) at android.os.Handler.enqueueMessage(Handler.java:631) at android.os.Handler.sendMessageAtTime(Handler.java:600) at android.os.Handler.sendMessageDelayed(Handler.java:570) at cn.carbswang.android.numberpickerview.library.NumberPickerView.onTouchEvent(NumberPickerView.java:933) at android.view.View.dispatchTouchEvent(View.java:8547) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2406) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2406) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2406) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2406) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2406) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2406) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2406) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2406) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2406) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2406) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2406) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2406) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2406) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2406) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2406) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2406) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107) at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2372) at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1722) at android.app.Activity.dispatchTouchEvent(Activity.java:2742) at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:60) at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:60) at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2333) at android.view.View.dispatchPointerEvent(View.java:8742) at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4136) at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4002) at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3557) at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3610) at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3576) at an

Here is how I initialize the NumberPicker:

final String[] servings = new String[]{"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20"}; //holder.servings is the NumberPicker. holder.servings.setMinValue(1); holder.servings.setDisplayedValues(servings); holder.servings.setMaxValue(20); if (currentRecipe.serves > 0) holder.servings.setValue(currentRecipe.serves); else holder.servings.setValue(1); holder.servings.setOnValueChangedListener(new NumberPickerView.OnValueChangeListener() { @Override public void onValueChange(NumberPickerView picker, int oldVal, int newVal) { RecipeIngredientsAdapter.this.servings = newVal; RecipeIngredientsAdapter.this.notifyItemRangeChanged(0, getItemCount()-1); } });

Here's the layout:
<cn.carbswang.android.numberpickerview.library.NumberPickerView android:id="@+id/recipe_ingredients_servings" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@android:color/transparent" app:npv_ItemPaddingHorizental="5dp" app:npv_TextColorNormal="@android:color/white" app:npv_TextColorSelected="@android:color/white" android:layout_gravity="center_horizontal|top" app:npv_ShowCount="1" app:npv_MinValue="1" app:npv_MaxValue="20" app:npv_ShowDivider="false" app:npv_TextSizeNormal="40sp" app:npv_TextSizeSelected="40sp" app:npv_WrapSelectorWheel="false"/>

(By the way, I noticed you mistyped npv_ItemPaddingHorizental)

初始化时要使用setDisplayedValues

只想显示数字时用setMinValue()和setMaxValue()而不用setDisplayedValues()时会报错
Java代码:
NumberPickerView numberPicker = (NumberPickerView) findViewById(R.id.picker);
numberPicker.setMaxValue(10);
numberPicker.setMinValue(0);
numberPicker.setValue(2);
报错信息:
(maxValue - mMinValue + 1) should not be greater than mDisplayedValues.length now (maxValue - mMinValue + 1) is 11 and mDisplayedValues.length is 1
而原生的NumberPicker做同样的设置不会报错

Tap to edit

Can we edit numberpicker middle value from keyboard value?

onValueChangeInScrolling 回傳不正確的value

我超喜歡這個控制,你簡直救了我一命啊!
Android自5.0之後的Date Picker逼你顯示日曆,如果我只是想用戶選月份和年份,日曆根本無法接受;
原生的NumberPicker又完全不能客制…

不過我發現一個問題,onValueChangeInScrolling回傳的newVal不正確,
而具體如何不正確則很難描述。
我有兩個NumberPickerView,一個是月份,一個是年份;
月份有12個數值,1到12
年份從1935到2100

我用debugger查看newVal的回傳數值;月份總是比最終停留的數值少2;
年份則是回傳了position,比如說我是1935開始,掃過1936,newVal回傳1。

如果這個可以解決就完美了~~ 我現在只能靠原本的onValueSelectedListener,沒有完全停定的話它就不懂得更新數值了

謝謝!

设置动态显示数据时可以更换最小值么?

在README.MD中:

2)若设置的数据(String[] mDisplayedValues)会改变,可以使用如下组合方式进行设置:(与NumberPicker的更改数据方式一致)

        int minValue = getMinValue();
        int oldMaxValue = getMaxValue();
        int oldSpan = oldMaxValue - minValue + 1;
        int newMaxValue = display.length - 1;
        int newSpan = newMaxValue - minValue + 1;
        if (newSpan > oldSpan) {
            setDisplayedValues(display);
            setMaxValue(newMaxValue);
        } else {
            setMaxValue(newMaxValue);
            setDisplayedValues(display);
        }

或者直接使用NumberPickerView提供的方法:

refreshByNewDisplayedValues(String[] display)

使用此方法时需要注意保证数据改变前后的minValue值不变,以及设置的display不能够为null,且长度不能够为0。

如果使用与NumberPicker的更改数据方式一致的方法,可以更改最小值么?

refreshByNewDisplayedValues的问题

我用这个库做省市区三级联动时遇到的问题,有的省份或者城市下没有区,通过refreshByNewDisplayedValues方法设置一个空的String数组,然后就会抛异常,能否处理这样的问题呢?
java.lang.IllegalArgumentException: mMaxValue - mMinValue + 1 should not be greater than mDisplayedValues.length, now ((mMaxValue - mMinValue + 1) is 1 newDisplayedValues.length is 0, you need to set MaxValue and MinValue before setDisplayedValues(String[])
at cn.carbswang.android.numberpickerview.library.NumberPickerView.setDisplayedValues(NumberPickerView.java:505)
at cn.microants.merchants.app.store.fragment.CityPickerDialog.refreshByNewDisplayedValues(CityPickerDialog.java:173)
at cn.microants.merchants.app.store.fragment.CityPickerDialog.updateCities(CityPickerDialog.java:134)
at cn.microants.merchants.app.store.fragment.CityPickerDialog.onValueChange(CityPickerDialog.java:120)
at cn.carbswang.android.numberpickerview.library.NumberPickerView.respondPickedValueChanged(NumberPickerView.java:627)
at cn.carbswang.android.numberpickerview.library.NumberPickerView.access$1200(NumberPickerView.java:24)
at cn.carbswang.android.numberpickerview.library.NumberPickerView$1.handleMessage(NumberPickerView.java:362)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.os.HandlerThread.run(HandlerThread.java:61)

能不能不要循环显示

当待显示的数到一定范围时,就会被循环显示。比如,待显示的数为1,2,3,4,5,6,7,8,9,10,11,12,显示效果是在12下面会出现1,现在我希望12下面宁愿是空白,我要选1时,我宁愿往下滑。

Drag to scroll doesn't work

I can't drag to scroll, and I don't see anything about dragging or flinging in the readme file even though it promises better flinging by adjusting friction...

Can't disable NumberPickerView

I'm trying to disable NumberPickerView when a certain condition is met. For this, I used NumberPickerView#setEnabled(false);, but I got no change in behaviour: the view is still scrollable. Is there a way to really disable the NumberPickerView?

控件的不可用状态没有处理

当调用setEnabled(false)把控件设置为不可用状态后,内部没有做相关的处理。

  1. onTouchEvent 事件应该在不可用时不处理
  2. setSelectedTextColor,setNormalTextColor, 应该可以设置selector
  3. 字体大小,代码中无法设置,只可以在xml设置,有点不方便。

How to align text to right? 如何将文本对齐?

Per default, the text is centered in the time picker. Is there a way to change the alignment to the left or right side? Thanks!

默认情况下,文本在时间选择器中居中。有没有办法改变左侧或右侧的对齐方式?谢谢!

Divider drawable (not only color)

Hi! Is there somehow a way to set drawable as background of divider?
I see you use private method drawLine(Canvas canvas) to draw simple divider-lines. So is it ok to just call any other 'draw' method on canvas instead?

颜文字(Emoji)支持

粗略的看下源代码,貌似不能简单的实现颜文字支持?
不知道是否可以提供些思路?
多谢!

Fade top and bottom of view

Thanks again for such a great library. You got any idea how I can fade/alpha top and bottom of view. So just like native number picker but I want to be able to increase/decrease that fade.

Trying / playing with mPaintText right now. Not sure if this would do or I may need to create another paint object. Let me know if you got any idea. :)

不知道是不是BUG

在xml文件中设置最小值和最大值的时候,如果没有设置npv_TextArray,会闪退,空指针异常。看了代码,发现最小值在里面只是用于数组下标的作用。而不是展示的作用。不知道是作者故意如此,还是一个bug

setMinValue的bug

final String[] mDisplayValues=new String[]{"今天","明天","后天"};
numberPicker.setDisplayedValues(mDisplayValues);
numberPicker.setMinValue(0);
numberPicer.setMaxValue(2);
显示正常。

但是,
numberPicker.setMinValue(1);//代码中需要动态调节minValue进行限位
发生了两个问题:
1、由于限位造成的value改变,并没有触发onValueChanged事件
2、在numberPicker中,预期效果应该显示为 明天、后天,实际显示为 今天、明天
并且,UI显示的"今天"所对应的value为“明天”(1),也就是说,UI显示的值与实际的value出现了不一致的情况,实际的值是正确的,而UI显示的是错误的。
一个好用的控件,其显示范围应该能由setMinValue()和setMaxValue()来决定。

监听器失效

当改变 displayedValues 的时候,监听器会失效,等一会才会有反应,希望能够改进

setMinValue and setMaxValue bug

in demo ActivityTimePicker
line 65:
replace
setData(mPickerViewH, 0, 11, h);
with
setData(mPickerViewH, 3, 4, 4);

hour picker show 1 and 12
NOT 3 and 4

I think it's a bug.
if setMinValue is not 0, number picker view will not work.

报个bug

app:npv_ShowCount="当前加载的数组的长度“ 时,smoothScrollToValue()这个函数就不起作用了

Set Default value on initialize

Enhancement:

I am trying to find a way to set default value.

use case :-
Current time is 3:06. When user go to specific screen, dev can take system time and feed that into numberpicker, so number picker would be set like 3:06 instead of showing the first element of the array.

我想修改里面的字体

现在列表中显示的10:30,中间的“:”这个符号太靠近下面了。我在软件中用了全局的字体。但是这儿没有自动替换。怎么样能给中间的“:”这个符号让处于中间呢?
注:
如果输入中文符的“:”,显示的时候效果会变成:“10: 30”
中间距离太大,求方案!谢谢各位大神!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.