Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop Android ViewFlipper from looping?

I have a ViewFlipper set to auto-flip every 5 seconds. Leaving out some of the details, it looks like this and works fine:

ViewFlipper flipper = (ViewFlipper) findViewById(R.id.myflipperid);

flipper.setFlipInterval(5000);                              
flipper.setInAnimation(inFromRightAnimation());
flipper.setOutAnimation(outToLeftAnimation());

flipper.startFlipping();

However, I have a case where I want the auto-flipping to stop at the last view, rather than looping around to start over again. It doesn't seem that ViewFlipper or any of the classes it inherits from have a looping control method.

How can I get ViewFlipper to stop looping through its child views when it hits the last one?

Note: the answer given here doesn't apply to my case, as I need to catch the ViewFlipper at the end of its list, i.e., without depending on user input. Thanks.

like image 663
gcl1 Avatar asked Sep 26 '12 20:09

gcl1


2 Answers

Here's the solution I used. As suggested here, the trick is to listen for the end of animation event, and then check to see if the flipper is on the last view.

flipper.getInAnimation().setAnimationListener(new Animation.AnimationListener() {

    public void onAnimationStart(Animation animation) {}
    public void onAnimationRepeat(Animation animation) {}
    public void onAnimationEnd(Animation animation) {

        int displayedChild = flipper.getDisplayedChild();
        int childCount = flipper.getChildCount();

        if (displayedChild == childCount - 1) {
            flipper.stopFlipping();
        }
    }
});

Thanks for your answers.

like image 164
gcl1 Avatar answered Oct 24 '22 02:10

gcl1


I haven't tried this my self but I hope this will help.

First, Try to listen to your viewflipper's flip events. Since you are using an animation. You may use the solution posted here: https://stackoverflow.com/a/3813179/1594522

Then, onAnimationEnd(), you can check if the viewflipper is on its last child view. If it is already on its last child view, then call flipper.stopFlipping().

Hope that helps! :)

like image 40
kdroider Avatar answered Oct 24 '22 04:10

kdroider