Listener for ViewFlipper widget flipping events

試著忘記壹切 提交于 2019-11-28 08:08:39

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

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

}

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

Moses Katsina

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!