Android NumberPicker wraps values when displayed values are changed

对着背影说爱祢 提交于 2020-01-03 09:00:13

问题


I have an android Dialog with a 3 numberpickers inside. Changing the 3rd picker triggers a change in the displayed values of the first 2 pickers. However I've noticed when I change the displayed values, and call

setWrapSelectorWheel(false)

it still shows the new values as wrapped visually (I can see the last value above the first one).

If I touch the picker it suddenly snaps into non wrap selector wheel. What's odd is I call

getWrapSelectoWheel()

after setting the displayed values and it returns false... just like I set it. But visually it's wrong.

Any ideas what's going on?

Many thanks!


回答1:


I found a solution, Daniel was on the right track, however it appears that initializeSelectorWheelIndices is actually a bad thing once you've already set your values. Because of this, you need to call setMinValue and setMaxValue BEFORE you set your values. However, if you already have an array set on your picker, if you call setMaxValue with a higher value than the current array, it will give you a ArrayIndexOutOfBounds exception.

The solution then is to clear the old display values, set your max value, then you can call setWrapSelectorWheel and setDisplayValues:

public void updatePickerValues(String[] newValues){
  picker.setDisplayedValues(null);
  picker.setMinValue(0);
  picker.setMaxValue(newValues.length -1);
  picker.setWrapSelectorWheel(false);
  picker.setDisplayedValues(newValues);
}



回答2:


I don't know what version of Android you're running but I would suggest you read the source code for the NumberPicker (v4.2.2). Perhaps you need to trigger a call to the private method initializeSelectorWheelIndices which can be done via a call to some of the public methods.

Although I do think your usage of the picker, by changing the wrapping flag part way through, does seem a little unusual. Don't you want this behaviour to be consistent? In which case make a call to setWrapSelectorWheel once after you've set your min and max values and then leave it alone.




回答3:


I've verified this behavior as well on kitkat. It's a bug. Couldn't find an existing report so I created a new one here: bug# 98319

Per previous answers I found that making setWrapSelectorWheel(false) the last call after all setup calls will work for most scenarios. In my case I'm dynamically changing limits of the NumberPicker as the user is interacting and I just can't get it to behave 100%. I'm opting to live with the wrap selector.



来源:https://stackoverflow.com/questions/16734634/android-numberpicker-wraps-values-when-displayed-values-are-changed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!