i m trying to get the wheel of the numberPicker animated. I found the corresponding question here:

How to change NumberPicker's value with animation?

But i cannot solve it with the suppposed answers. Can somebody please help me?

When changing the value of the numberPicker from lets say 100 to 200, i want the wheel to increment from the old value to the new one. The wheel should slide to the new value. In the link passsy solved it with reflection but i don t know how to use it in practice..

有帮助吗?

解决方案 2

It's really simple if you want to increment the value by one.

just call changeValueByOne(myNumberPicker, true); to increment the numberpicker by one with animation. (false to decrement)

increment from 100-200 is not possible with this method

            /**
             * using reflection to change the value because
             * changeValueByOne is a private function and setValue
             * doesn't call the onValueChange listener.
             * 
             * @param higherPicker
             *            the higher picker
             * @param increment
             *            the increment
             */
            private void changeValueByOne(final NumberPicker higherPicker, final boolean increment) {

            Method method;
            try {
                // refelction call for
                // higherPicker.changeValueByOne(true);
                method = higherPicker.getClass().getDeclaredMethod("changeValueByOne", boolean.class);
                method.setAccessible(true);
                method.invoke(higherPicker, increment);

            } catch (final NoSuchMethodException e) {
                e.printStackTrace();
            } catch (final IllegalArgumentException e) {
                e.printStackTrace();
            } catch (final IllegalAccessException e) {
                e.printStackTrace();
            } catch (final InvocationTargetException e) {
                e.printStackTrace();
            }
        }

其他提示

Thank @passsy. Inspired his answer: This solution supports multiple increment/decrement steps. Also, can be executed with a start delay and for multiple numberPickers (without extra coding).

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.
    }
}

The usage:

// 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).
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top