Browse Source

event stability

master
Henning Hall 5 years ago
parent
commit
1f8821ad5a
1 changed files with 19 additions and 10 deletions
  1. +19
    -10
      android/src/main/java/com/henninghall/date_picker/pickers/AndroidNative.java

+ 19
- 10
android/src/main/java/com/henninghall/date_picker/pickers/AndroidNative.java View File

@ -12,6 +12,8 @@ import java.lang.reflect.Method;
public class AndroidNative extends NumberPicker implements Picker { public class AndroidNative extends NumberPicker implements Picker {
private Picker.OnValueChangeListener onValueChangedListener;
public AndroidNative(Context context) { public AndroidNative(Context context) {
super(context); super(context);
} }
@ -56,10 +58,9 @@ public class AndroidNative extends NumberPicker implements Picker {
@Override @Override
public void smoothScrollToValue(final int value) { public void smoothScrollToValue(final int value) {
Handler handler = new Handler();
final AndroidNative self = this; final AndroidNative self = this;
handler.postDelayed(new Runnable() {
new Handler().postDelayed(new Runnable() {
public void run() { public void run() {
int currentValue = self.getValue(); int currentValue = self.getValue();
if (value == currentValue) return; if (value == currentValue) return;
@ -67,7 +68,7 @@ public class AndroidNative extends NumberPicker implements Picker {
final int moves = Math.abs(shortestScrollOption); final int moves = Math.abs(shortestScrollOption);
for (int i = 0; i < moves; i++) { for (int i = 0; i < moves; i++) {
// need some delay between each scroll step to make sure it scrolls to correct value // 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 // 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); }, 500);
} }
private int getShortestScrollOption(int currentValue, int value){
private int getShortestScrollOption(int currentValue, int value) {
final int maxValue = getMaxValue(); final int maxValue = getMaxValue();
int option1 = value - currentValue; int option1 = value - currentValue;
int option2 = maxValue + 1 - Math.abs(option1); int option2 = maxValue + 1 - Math.abs(option1);
if(getWrapSelectorWheel()){
if (getWrapSelectorWheel()) {
return Math.abs(option1) < Math.abs(option2) ? option1 : option2; return Math.abs(option1) < Math.abs(option2) ? option1 : option2;
} }
if (currentValue + option1 > maxValue) return 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 method = getClass().getSuperclass().getDeclaredMethod("changeValueByOne", boolean.class);
method.setAccessible(true); method.setAccessible(true);
method.invoke(higherPicker, increment); method.invoke(higherPicker, increment);
} catch (Exception e) {
} catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
// make step without animation if failed to use reflection method // make step without animation if failed to use reflection method
setValue((getValue() + (increment ? 1 : -1)) % getMaxValue()); 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; final AndroidNative self = this;
handler.postDelayed(new Runnable() {
new Handler().handler.postDelayed(new Runnable() {
@Override @Override
public void run() { public void run() {
changeValueByOne(self, increment); 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); }, ms);
} }
@Override @Override
public void setOnValueChangeListenerInScrolling(final OnValueChangeListenerInScrolling listener) { public void setOnValueChangeListenerInScrolling(final OnValueChangeListenerInScrolling listener) {
final Picker self = this; final Picker self = this;
@ -125,6 +133,7 @@ public class AndroidNative extends NumberPicker implements Picker {
@Override @Override
public void setOnValueChangedListener(final Picker.OnValueChangeListener listener) { public void setOnValueChangedListener(final Picker.OnValueChangeListener listener) {
this.onValueChangedListener = listener;
super.setOnScrollListener(new OnScrollListener() { super.setOnScrollListener(new OnScrollListener() {
int previousState = SCROLL_STATE_IDLE; int previousState = SCROLL_STATE_IDLE;

Loading…
Cancel
Save