Listener for ViewFlipper widget flipping events

前端 未结 5 1519
情话喂你
情话喂你 2020-12-09 09:12

I have a ViewFlipper implementation that needs to be improved. This ViewFlipper has three child views. Basically, I want an indicator on which child view is currently active

相关标签:
5条回答
  • 2020-12-09 09:32

    If you apply animation (out or in animation) on view switching, you can set listener to an animation and, for example, act on animation end.

       viewFlipper.getInAnimation().setAnimationListener(new Animation.AnimationListener() {
          public void onAnimationStart(Animation animation) {}
          public void onAnimationRepeat(Animation animation) {}
          public void onAnimationEnd(Animation animation) {}
       });
    
    0 讨论(0)
  • 2020-12-09 09:33

    I know this question already has an answer. But here's an alternative which is a method ViewFlipper inherited from ViewGroup and which seems to be the best from my experience.

    Code Snippets Below:

    ViewFlipper viewFlipper = findViewById (R.id.myViewFlipperId);
    viewFlipper.setOnHierarchyChangeListener(ViewGroup.OnHierarchyChangeListener);
    

    The onChildViewAdded(View, View) method in the callback listener will be invoked whenever a child view is added to the ViewGroup. Which you can use to detect whenever the ViewFlipper flips.

    See ViewGroup.html#setOnHierarchyChangeListener API Docs

    0 讨论(0)
  • 2020-12-09 09:37

    I have created an extended ViewFlipper which does exactly that: DecentViewFlipper

    0 讨论(0)
  • 2020-12-09 09:47

    While this is an old question I found a decent approach that works.

    public class MainLaunch extends Activity {
    
        ... main setup and code
    
        int currentIndex = 0;
        int maxIndex = 3;
    
        // set specific animations for the view flipper on showNext
        // only showNext while in valid index
        public void showNext() {
            if( currentIndex < maxIndex )
            {
                currentIndex++;
                viewFlipper.setInAnimation(getBaseContext(), R.anim.slide_in_left);
                viewFlipper.setOutAnimation(getBaseContext(), R.anim.slide_out_right);
                viewFlipper.showNext();
            }
        }
    
        // set specific animations for the view flipper on showPrevious
        // only showPrevious while in valid index
        public void showPrevious() {
            if( currentIndex > 0 )
            {
                currentIndex--;
                viewFlipper.setInAnimation(getBaseContext(), R.anim.slide_in_right);
                viewFlipper.setOutAnimation(getBaseContext(), R.anim.slide_out_left);
                viewFlipper.showPrevious();
            }
        }
    
        // get current flipped view
        public View getCurrentView() {
            return viewFlipper.getChildAt(currentIndex);
        }
    
    
    }
    

    Then to use the ViewFlipper you call showNext() or showPrevious anywhere and can get the currently active view by calling getCurrentView(). This helps in setting different animations for left and right flipping and for easily getting current working views.

    0 讨论(0)
  • 2020-12-09 09:49

    I find one way to detect which child is actived :

    addOnLayoutChangeListener to ViewFlipper, and getCurrentView of ViewFlipper, then compare with childs of ViewFlipper.

    remember to removeOnLayoutChangeListener when activity onDestory

    private View page1, page2, page3, page4;
    private ViewFlipper viewFlipper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        setContentView(R.layout.flipper);
    
        page1 = findViewById(R.id.MyFlipper_page01);
        page2 = findViewById(R.id.MyFlipper_page02);
        page3 = findViewById(R.id.MyFlipper_page03);
        page4 = findViewById(R.id.MyFlipper_page04);
    
        viewFlipper = (ViewFlipper) findViewById(R.id.MyFlipper_flipper);
    
        viewFlipper.addOnLayoutChangeListener(onLayoutChangeListener_viewFlipper);
    
    }
    
    View.OnLayoutChangeListener onLayoutChangeListener_viewFlipper = new View.OnLayoutChangeListener() {
        @Override
        public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
    
           if(viewFlipper.getCurrentView() == page1)
               Log.d("test", "change to flipper_page1");
           else if(viewFlipper.getCurrentView() == page2)
               Log.d("test", "change to flipper_page2");
           else if(viewFlipper.getCurrentView() == page3)
               Log.d("test", "change to flipper_page3");
           else if(viewFlipper.getCurrentView() == page4)
               Log.d("test", "change to flipper_page4");
    
    
    
        }
    };
    
    
    @Override
    protected void onDestroy() {
        super.onDestroy();
    
        viewFlipper.removeOnLayoutChangeListener(onLayoutChangeListener_viewFlipper);
    
    }
    
    0 讨论(0)
提交回复
热议问题