Спасибо @passsy. Вдохновленный его ответ: Это решение поддерживает несколько шагов приращения/уменьшения. Кроме того, может быть выполнен с стартовой задержкой и для несколько numberPickers (без дополнительного кодирования).
class IncreaseValue {
private int counter = 0;
private final NumberPicker picker;
private final int incrementValue;
private final boolean increment;
private final Handler handler = new Handler();
private final Runnable fire = new Runnable() { @Override public void run() { fire(); } };
/*public*/ IncreaseValue(final NumberPicker picker, final int incrementValue) {
this.picker = picker;
if (incrementValue > 0) {
increment = true;
this.incrementValue = incrementValue;
} else {
increment = false;
this.incrementValue = -incrementValue;
}
}
/*public*/ void run(final int startDelay) {
handler.postDelayed(fire, startDelay); // This will execute the runnable passed to it (fire)
// after [startDelay in milliseconds], ASYNCHRONOUSLY.
}
private void fire() {
++counter;
if (counter > incrementValue) return;
try {
// refelction call for
// picker.changeValueByOne(true);
final Method method = picker.getClass().getDeclaredMethod("changeValueByOne", boolean.class);
method.setAccessible(true);
method.invoke(picker, increment);
} catch (final NoSuchMethodException | InvocationTargetException |
IllegalAccessException | IllegalArgumentException e) {
Log.d(TAG, "...", e);
}
handler.postDelayed(fire, 120); // This will execute the runnable passed to it (fire)
// after 120 milliseconds, ASYNCHRONOUSLY. Customize this value if necessary.
}
}
Использование:
// Suppose picker1 as a NumberPicker
IncreaseValue increasePicker1 = new IncreaseValue(picker1, 10); // So picker1 will be increased 10 steps.
increasePicker1.run(0); // Increase immediately. If you want to run it from onCreate(), onStart() or ... of your activity, you will probably need to pass a positive value to it (e.g. 100 milliseconds).
Ok спасибо. Я попробовал его с каким-то циклом «за» от 100 до 200, но я не буду обновлять пользовательский интерфейс. Вы думаете, что это возможно с помощью этого метода или нет? – KelvinGradCelsius
нет с этим методом. посмотрите на источник сборщика, чтобы посмотреть, как он работает. Но я думаю, вы должны использовать пользовательский сборщик. – passsy
Ответ на вопрос. Спасибо за быструю поддержку! – KelvinGradCelsius