Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Listener for ViewFlipper widget flipping events

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!

like image 847
Shane Oliver Avatar asked Sep 28 '10 13:09

Shane Oliver


2 Answers

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) {}
   });
like image 163
Konstantin Burov Avatar answered Nov 09 '22 09:11

Konstantin Burov


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

}
like image 27
HenryChuang Avatar answered Nov 09 '22 09:11

HenryChuang