NumberPicker freezes app when changing visibility or height while scrolling

192 Views Asked by At

I am working on an app and I have a layout with items that when pressed, reveal a NumberPicker below them, like this:

enter image description here

I am using the native Android NumberPicker. The NumberPickers start with visibility GONE and their visibility is toggled when pressing the item above them. However, I have discovered that if the user attempts to close a picker in this way when it is still scrolling (for example a hard swipe on the picker and then immediately press the item to close that picker while it's still scrolling) causes the picker to get stuck in an infinite loop, freezing and eventually crashing the app. Logging the onValueChangedListener for the NumberPicker reveals these logs:

2020-03-25 16:13:39.545 30393-30393/com.example.expandablepicker.debug I/ExpandingPickerItem: value change, 4
2020-03-25 16:13:39.545 30393-30393/com.example.expandablepicker.debug I/ExpandingPickerItem: value change, 5
2020-03-25 16:13:39.545 30393-30393/com.example.expandablepicker.debug I/ExpandingPickerItem: value change, 6
2020-03-25 16:13:39.545 30393-30393/com.example.expandablepicker.debug I/ExpandingPickerItem: value change, 7
2020-03-25 16:13:39.558 30393-30393/com.example.expandablepicker.debug W/View: requestLayout() improperly called by android.widget.NumberPicker$CustomEditText{f549769 IFED..CL. ......I. 0,0-1080,10 #1020386 android:id/numberpicker_input} during layout: running second layout pass
2020-03-25 16:13:39.560 30393-30393/com.example.expandablepicker.debug I/ExpandingPickerItem: value change, 6
2020-03-25 16:13:39.561 30393-30393/com.example.expandablepicker.debug I/ExpandingPickerItem: value change, 5
2020-03-25 16:13:39.561 30393-30393/com.example.expandablepicker.debug I/ExpandingPickerItem: value change, 4
2020-03-25 16:13:39.561 30393-30393/com.example.expandablepicker.debug I/ExpandingPickerItem: value change, 3
2020-03-25 16:13:39.561 30393-30393/com.example.expandablepicker.debug I/ExpandingPickerItem: value change, 2
2020-03-25 16:13:39.561 30393-30393/com.example.expandablepicker.debug I/ExpandingPickerItem: value change, 1
2020-03-25 16:13:39.561 30393-30393/com.example.expandablepicker.debug I/ExpandingPickerItem: value change, 0
2020-03-25 16:13:39.569 30393-30393/com.example.expandablepicker.debug I/chatty: uid=10345(com.example.expandablepicker.debug) identical 283 lines
2020-03-25 16:13:39.569 30393-30393/com.example.expandablepicker.debug I/ExpandingPickerItem: value change, 0
2020-03-25 16:13:39.569 30393-30393/com.example.expandablepicker.debug I/ExpandingPickerItem: value change, 0
2020-03-25 16:13:39.569 30393-30393/com.example.expandablepicker.debug I/ExpandingPickerItem: value change, 0
2020-03-25 16:13:39.572 30393-30393/com.example.expandablepicker.debug I/chatty: uid=10345(com.example.expandablepicker.debug) identical 93 lines

I have also implemented a version of this where the visibility change of the NumberPicker is animated, so it expands out and in instead of just being revealed. I implemented this with an Animation that changed the height of the NumberPicker. When this is the case, the picker ends up stalling about halfway through the closing animation, in the same way as it stalls with changing the visibility.

I have tried a couple of different things to halt the scrolling of the NumberPicker before I try to close it. None so far have worked. I have tried (in no particular order)

  • calling setValue to the last known value of the NumberPicker, thinking that might just halt the scroll
  • calling setEnabled(false) before hiding the NumberPicker
  • calling performClick() before hiding the NumberPicker
  • calling dispatchTouchEvent(...) before hiding the NumberPicker
  • calling jumpDrawablesToCurrentState() on the NumberPicker and its parent view
  • calling scrollBy(0, 0) before hiding the NumberPicker

In all cases the scroll animation continued unaffected, and the app would freeze when trying to hide the picker.

Is there any way I can halt the scroll of the NumberPicker so that I can hide the view without this looping problem? Or maybe there is a way to safely hide and reveal pickers without causing them to loop in this way?

Thanks

0

There are 0 best solutions below