Listener for ViewFlipper widget flipping events

18,823

Solution 1

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) {}
   });

Solution 2

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);

}
Share:
18,823

Related videos on Youtube

Shane Oliver
Author by

Shane Oliver

Updated on November 27, 2020

Comments

  • Shane Oliver
    Shane Oliver over 3 years

    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. My ViewFlipper is just a part of a complex layout which also has list views, etc.

    Switching of views is also automatic and done in a specified interval.

    From Android's SDK reference, I haven't seen any listener for when the ViewFlipper changes the child view.

    Do you guys know of a way I can have a listener for that event?

    Or are there alternative ways I can implement this feature besides using ViewFlipper ?

    Thanks!

  • Shane Oliver
    Shane Oliver almost 14 years
    +1 Thanks! That nailed it. I was using xml based animation parameters so I totally missed that. So I now created the ViewFlipper animation parameters inside my Activity instead of having it in my xml.
  • slinden77
    slinden77 almost 8 years
    in general I find it most convenient to set more in code then in xml
  • Konrad Morawski
    Konrad Morawski almost 7 years
    It only works if you want (and have) an animation in the first place :) Otherwise it's null
  • Pat Lee
    Pat Lee about 3 years
    I like the name of your class :-)