ShawnLin013 / NumberPicker

:slot_machine: The android library that provides a simple and customizable NumberPicker.
MIT License
1.07k stars 240 forks source link

onValueChanged called several times, with alternating values #201

Open MlsCf opened 1 year ago

MlsCf commented 1 year ago

Hello, I have found this issue: when dragging to choose a value, the onValueChanged handler gets called several times.

For example, when dragging from value "1" to "2" , this occurs

oldVal:1 newVal:2
oldVal:2 newVal:1
oldVal:1 newVal:2
oldVal:2 newVal:1
oldVal:1 newVal:2
oldVal:2 newVal:1
oldVal:1 newVal:2

this is not a big problem, since the last value is correct. However, in case we need other actions to be performed on value changed (i.e. persisting value to storage, or calling other functions) this can create problems.

I checked the behaviour of the original Android widget and this issue does not occur.

As a temporary workaround, I tried to track the timestamps atwhich the value changes occur, discarding every second one in case the timestamp difference is less than a few ms. This - however - in not the optimal solution since it sets sometimes the wrong value (in case the item is dragged back and forth).

Regards.

MlsCf commented 1 year ago

Could this issue be related to https://github.com/ShawnLin013/NumberPicker/issues/183 ? I haven't tried to integrate the change proposed by the user, yet.