Question

I'm using a custom animation to replace fragments, and I'd like to disable some buttons when the animation starts and enable then when the animation ends. How can I do this?

Was it helpful?

Solution

What I would suggest is to make some base class that all of your Fragments extend from, and within it, define a few methods that can be overridden to handle the animation events. Then, override onCreateAnimation() (assuming you are using the support library) to send an event on animation callbacks. For example:

protected void onAnimationStarted () {}

protected void onAnimationEnded () {}

protected void onAnimationRepeated () {}

@Override
public Animation onCreateAnimation (int transit, boolean enter, int nextAnim) {
    //Check if the superclass already created the animation
    Animation anim = super.onCreateAnimation(transit, enter, nextAnim);

    //If not, and an animation is defined, load it now
    if (anim == null && nextAnim != 0) {
        anim = AnimationUtils.loadAnimation(getActivity(), nextAnim);
    }

    //If there is an animation for this fragment, add a listener.
    if (anim != null) {
        anim.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart (Animation animation) {
                onAnimationStarted();
            }

            @Override
            public void onAnimationEnd (Animation animation) {
                onAnimationEnded();
            }

            @Override
            public void onAnimationRepeat (Animation animation) {
                onAnimationRepeated();
            }
        });
    }

    return anim;
}

Then, for your Fragment subclass, just override onAnimationStarted() to disable the buttons, and onAnimationEnded() to enable the buttons.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top