开发者

Switch between two listeners

In my program I would like to use two sets of wheels like the following:

hours = (WheelView) findViewById(R.id.hour);
NumericWheelAdapter hourAdapter = new NumericWheelAdapter(this, 0, 23, "%02d");
hourAdapter.setItemResource(R.layout.wheel_text_item);
hourAdapter.setItemTextResource(R.id.text);
hours.setViewAdapter(hourAdapter);
hours.setCyclic(true);

These wheels have a scrollinglistener 开发者_Python百科hours.addScrollingListener(scrolledListener);

The scrollinglistener looks like this:

// Wheel scrolled listener
OnWheelScrollListener scrolledListener = new OnWheelScrollListener() {
    public void onScrollingStarted(WheelView wheel) {
    }
    public void onScrollingFinished(WheelView wheel) {
    update();
    }
};

I would like to set one wheel with the value of the other but when I instantiate two listeners and scroll the first one it goes on setting wheel one and then wheel two and then wheel one again.

Is it possible to disable a one of the two scrolledListener?

I've tried this:

    hours.addScrollingListener(null);
    hours.setEnabled(false);

But this gave an error and the program had to be stopped.

Thanks for your help!

PS: the wheels are from * Android Wheel Control. * https://code.google.com/p/android-wheel/


From the sounds of it you are adding both OnWheelScrollListener to the same WheelView listener list which callsback to every listener it contains which would explain your behavior. The listeners should be added in a similar fashion to this...

hours = (WheelView) findViewById(R.id.hour);
        hours.addScrollingListener(new OnWheelScrollListener() {
            public void onScrollingStarted(WheelView wheel) {
            }
            public void onScrollingFinished(WheelView wheel) {
            update();
            }
        });

        hours2 = (WheelView) findViewById(R.id.hour2);
        hours2.addScrollingListener(new OnWheelScrollListener() {
            public void onScrollingStarted(WheelView wheel) {
            }
            public void onScrollingFinished(WheelView wheel) {
            update();
            }
        });

Also, you want to use the removeScrollingListener(OnWheelScrollListener) method to remove a listener...hours.addScrollingListener(null); is just adding a null item to your list which is causing your NullPointerException i presume.

Edit

    OnWheelScrollListener listener = new OnWheelScrollListener() {
                public void onScrollingStarted(WheelView wheel) {
                }
                public void onScrollingFinished(WheelView wheel) {
                if (wheel.getId() == R.id.hour) {
    //do wheel 1 logic
    } else if (wheel.getId() == R.id.hour2) {
    //do wheel 2 logic
    }
            };

hours.addScrollingListener(listener);
hours2.addScrollingListener(listener);


I've contacted the designer, Yuri, of the wheel that i'm using and he gave me the following solution which works:

// Wheel scrolled listener
OnWheelScrollListener scrolledListener = new OnWheelScrollListener() {
    boolean syncInProgress = false;

    public void onScrollingStarted(WheelView wheel) {
    }
    public void onScrollingFinished(WheelView wheel) {

        if (syncInProgress) {
            syncInProgress = false;
            return;
        }

        syncInProgress = true;
        if (wheel.getId() == R.id.wheel1) {
            // do something
        } else if (wheel.getId() == R.id.wheel2) {
            // do something else
        }
    }
};

Thank you all for your help!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜