fan90910 / android-wheel

Automatically exported from code.google.com/p/android-wheel
0 stars 0 forks source link

Level2 wheel not update when Level1 wheel issue change event #2

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Dear sir, first.thanks for your great work .I get a issue to report,maybe it 
can make this view more helpful.

What steps will reproduce the problem?
1. setup two wheel view called level1 and level2
2. setup a change listener to level1,update the level2's adapter in the 
listener's method
3. scroll up and down of level1 wheel, level2 not update (repaint)

What is the expected output? What do you see instead?

  It should update the level2 wheel.

Code likes below:

                this.level1ItemWheel = (WheelView) findViewById(R.id.level1Item);
        this.level2ItemWheel = (WheelView) findViewById(R.id.level2Item);

                level1ItemWheel.setAdapter(new ArrayWheelAdapter<String>(
                    new String[]{"level1A","level1B"}));

            level1ItemWheel.setVisibleItems(5);
            level1ItemWheel.setCurrentItem(0);

            level2ItemWheel.setAdapter(new ArrayWheelAdapter<String>(
                    new String["child1_of_level1A","child2_of_level1A"]));

        level2ItemWheel.setVisibleItems(5);
        level2ItemWheel.setCurrentItem(0);

                level1ItemWheel.addChangingListener(new OnWheelChangedListener(){
                        public void onChanged(WheelView wheel, int oldValue, int newValue) {
            if (wheel == level1ItemWheel && newValue==1) { //when scroll to levelB,change it's child list.

                String level1Item = wheel.getAdapter().getItem(newValue);

                                //not working!!!!!!!!!!!!!!!!here
                level2ItemWheel.setAdapter(new ArrayWheelAdapter<String>( new String[]{"child1_of_level1B"} ));

                level2ItemWheel.setVisibleItems(5);
                level2ItemWheel.setCurrentItem(0);

                return;
            }  
                   );

How to fix it:
I found that setCurrentItem of WheelView ,check the select index before 
invalidate the ui.

    public void setCurrentItem(int index, boolean forceInvalidate) {

        if (index != currentItem) {
            invalidateLayouts();

            int old = currentItem;
            currentItem = index;

            notifyChangingListeners(old, currentItem);

            invalidate();
        }

    }

This may cause the second wheel not update view in this situation. I overload a 
new method:

public void setCurrentItem(int index, boolean forceInvalidate) {

        if (forceInvalidate || index != currentItem) { //use can choose weather he want to update the ui to new adapter here
            invalidateLayouts();

            int old = currentItem;
            currentItem = index;

            notifyChangingListeners(old, currentItem);

            invalidate();
        }

    }

And modify the code line in the change listener:

level2ItemWheel.setAdapter(new ArrayWheelAdapter<String>( new 
String[]{"child1_of_level1B"} ));

level2ItemWheel.setVisibleItems(5);
level2ItemWheel.setCurrentItem(0,true);///And now it can work now!!!

Hope this can help....

Original issue reported on code.google.com by yongxin...@gmail.com on 31 Oct 2010 at 12:06

GoogleCodeExporter commented 9 years ago
Thank you for the issue.

I think the problem is in WheelView.setAdapter() - it should also invalidate 
layouts (set them to null by calling invalidateLayouts(), like setCurrentItem() 
does).

But you solution has a one good thing - I think there should be also a public 
method for invalidating wheel control in any way.

Thanks you very much!
I will update sources soon.

Original comment by yuri.kan...@gmail.com on 31 Oct 2010 at 7:25

GoogleCodeExporter commented 9 years ago

Original comment by yuri.kan...@gmail.com on 3 Nov 2010 at 7:57