|
|
@ -9,6 +9,8 @@ import android.view.View; |
|
|
|
import android.widget.EditText; |
|
|
|
import android.widget.NumberPicker; |
|
|
|
|
|
|
|
import com.henninghall.date_picker.Utils; |
|
|
|
|
|
|
|
import java.lang.reflect.Field; |
|
|
|
import java.lang.reflect.InvocationTargetException; |
|
|
|
import java.lang.reflect.Method; |
|
|
@ -21,6 +23,7 @@ public class AndroidNative extends NumberPicker implements Picker { |
|
|
|
private Picker.OnValueChangeListener onValueChangedListener; |
|
|
|
private int state = SCROLL_STATE_IDLE; |
|
|
|
private OnValueChangeListenerInScrolling listenerInScrolling; |
|
|
|
private boolean isAnimating; |
|
|
|
|
|
|
|
public AndroidNative(Context context) { |
|
|
|
super(context); |
|
|
@ -86,7 +89,7 @@ public class AndroidNative extends NumberPicker implements Picker { |
|
|
|
public void setDividerHeight(int height) { |
|
|
|
// not supported |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
public void setItemPaddingHorizontal(int padding) { |
|
|
|
// Not needed for this picker |
|
|
@ -94,39 +97,31 @@ public class AndroidNative extends NumberPicker implements Picker { |
|
|
|
|
|
|
|
@Override |
|
|
|
public boolean isSpinning() { |
|
|
|
return state == SCROLL_STATE_FLING; |
|
|
|
return state == SCROLL_STATE_FLING || isAnimating; |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void smoothScrollToValue(final int value) { |
|
|
|
final AndroidNative self = this; |
|
|
|
|
|
|
|
int currentValue = self.getValue(); |
|
|
|
if (value == currentValue) return; |
|
|
|
int shortestScrollOption = Utils.getShortestScrollOption(currentValue, value, getMaxValue(), getWrapSelectorWheel()); |
|
|
|
final int moves = Math.abs(shortestScrollOption); |
|
|
|
int timeBetweenScrollsMs = 100; |
|
|
|
int willStopScrollingInMs = timeBetweenScrollsMs * moves; |
|
|
|
isAnimating = true; |
|
|
|
new Handler().postDelayed(new Runnable() { |
|
|
|
@Override |
|
|
|
public void run() { |
|
|
|
int currentValue = self.getValue(); |
|
|
|
if (value == currentValue) return; |
|
|
|
int shortestScrollOption = getShortestScrollOption(currentValue, value); |
|
|
|
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, i == moves - 1); |
|
|
|
} |
|
|
|
isAnimating = false; |
|
|
|
} |
|
|
|
// since the SCROLL_STATE_IDLE event is dispatched before the wheel actually has stopped |
|
|
|
// an extra delay has to be added before starting to scroll to correct value |
|
|
|
}, 500); |
|
|
|
} |
|
|
|
}, willStopScrollingInMs); |
|
|
|
|
|
|
|
private int getShortestScrollOption(int currentValue, int value) { |
|
|
|
final int maxValue = getMaxValue(); |
|
|
|
int option1 = value - currentValue; |
|
|
|
int option2 = maxValue + 1 - Math.abs(option1); |
|
|
|
if (getWrapSelectorWheel()) { |
|
|
|
return Math.abs(option1) < Math.abs(option2) ? option1 : option2; |
|
|
|
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 * timeBetweenScrollsMs, i == moves - 1); |
|
|
|
} |
|
|
|
if (currentValue + option1 > maxValue) return option2; |
|
|
|
if (currentValue + option1 < 0) return option2; |
|
|
|
return option1; |
|
|
|
} |
|
|
|
|
|
|
|
private void changeValueByOne(final NumberPicker higherPicker, final boolean increment) { |
|
|
@ -185,7 +180,7 @@ public class AndroidNative extends NumberPicker implements Picker { |
|
|
|
// to send event during scrolling we make sure wheel is still. This particular |
|
|
|
// case happens when wheel is tapped, not scrolled. |
|
|
|
if(state == SCROLL_STATE_IDLE) { |
|
|
|
sendEventIn500ms(); |
|
|
|
sendEventIn500ms(); |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
@ -193,8 +188,8 @@ public class AndroidNative extends NumberPicker implements Picker { |
|
|
|
super.setOnScrollListener(new OnScrollListener() { |
|
|
|
@Override |
|
|
|
public void onScrollStateChange(NumberPicker numberPicker, int nextState) { |
|
|
|
sendEventIfStopped(nextState); |
|
|
|
state = nextState; |
|
|
|
sendEventIfStopped(nextState); |
|
|
|
state = nextState; |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|