diff --git a/android/src/main/java/com/henninghall/date_picker/pickers/AndroidNative.java b/android/src/main/java/com/henninghall/date_picker/pickers/AndroidNative.java index 796955d..c48e331 100644 --- a/android/src/main/java/com/henninghall/date_picker/pickers/AndroidNative.java +++ b/android/src/main/java/com/henninghall/date_picker/pickers/AndroidNative.java @@ -12,6 +12,8 @@ import java.lang.reflect.Method; public class AndroidNative extends NumberPicker implements Picker { + private Picker.OnValueChangeListener onValueChangedListener; + public AndroidNative(Context context) { super(context); } @@ -56,10 +58,9 @@ public class AndroidNative extends NumberPicker implements Picker { @Override public void smoothScrollToValue(final int value) { - Handler handler = new Handler(); final AndroidNative self = this; - handler.postDelayed(new Runnable() { + new Handler().postDelayed(new Runnable() { public void run() { int currentValue = self.getValue(); if (value == currentValue) return; @@ -67,7 +68,7 @@ public class AndroidNative extends NumberPicker implements Picker { final int moves = Math.abs(shortestScrollOption); for (int i = 0; i < moves; i++) { // need some delay between each scroll step to make sure it scrolls to correct value - changeValueByOne(shortestScrollOption > 0, i * 100); + changeValueByOne(shortestScrollOption > 0, i * 100, i == moves - 1); } } // since the SCROLL_STATE_IDLE event is dispatched before the wheel actually has stopped @@ -75,11 +76,11 @@ public class AndroidNative extends NumberPicker implements Picker { }, 500); } - private int getShortestScrollOption(int currentValue, int value){ + private int getShortestScrollOption(int currentValue, int value) { final int maxValue = getMaxValue(); int option1 = value - currentValue; int option2 = maxValue + 1 - Math.abs(option1); - if(getWrapSelectorWheel()){ + if (getWrapSelectorWheel()) { return Math.abs(option1) < Math.abs(option2) ? option1 : option2; } if (currentValue + option1 > maxValue) return option2; @@ -93,25 +94,32 @@ public class AndroidNative extends NumberPicker implements Picker { Method method = getClass().getSuperclass().getDeclaredMethod("changeValueByOne", boolean.class); method.setAccessible(true); method.invoke(higherPicker, increment); - } catch (Exception e) { + } catch (Exception e) { e.printStackTrace(); // make step without animation if failed to use reflection method setValue((getValue() + (increment ? 1 : -1)) % getMaxValue()); } } - private void changeValueByOne(final boolean increment, int ms) { - Handler handler = new Handler(); + private void changeValueByOne(final boolean increment, final int ms, final boolean isLast) { final AndroidNative self = this; - handler.postDelayed(new Runnable() { + new Handler().handler.postDelayed(new Runnable() { @Override public void run() { changeValueByOne(self, increment); + if (isLast) { + new Handler().postDelayed(new Runnable() { + @Override + public void run() { + onValueChangedListener.onValueChange(); + } + // the delay make sure the wheel has stopped before sending the value change event + }, 500); + } } }, ms); } - @Override public void setOnValueChangeListenerInScrolling(final OnValueChangeListenerInScrolling listener) { final Picker self = this; @@ -125,6 +133,7 @@ public class AndroidNative extends NumberPicker implements Picker { @Override public void setOnValueChangedListener(final Picker.OnValueChangeListener listener) { + this.onValueChangedListener = listener; super.setOnScrollListener(new OnScrollListener() { int previousState = SCROLL_STATE_IDLE;