There is an application with a registration screen. As a result of pressing the button, the animation of the appearance of a white screen should start, then the animation of another element should start, and then the animation of the disappearance of this element should start up and all this one after the other. Otherwise, the principle can be described as:

The button -> start of the appearance of the white screen animation (essentially RelativeView with background = white ) -> at the end of this animation is pressed, the view (which lies in RelativeLayout ) is alpha = 1 and the next animation starts -> at the end of the view animation, it should fade away slowly. Accordingly, this is a new fade animation.

Thus it turns out 3 nested listeners of animations (in the example code while 2).

 @Override public void showDoneView(){ Animator animator = Flubber.with() .animation(Flubber.AnimationPreset.FADE_IN) .duration(1000) .createFor(doneLayout); animator.addListener(new Animator.AnimatorListener(){ @Override public void onAnimationStart(Animator animation){ } @Override public void onAnimationEnd(Animator animation){ doneView.setAlpha(1f); doneView.setSpeed(0.7f); doneView.playAnimation(); doneView.addAnimatorListener(new Animator.AnimatorListener(){ @Override public void onAnimationStart(Animator animation){ } @Override public void onAnimationEnd(Animator animation){ MyApp.INSTANCE.getRouter().newRootScreen(FEED_FRAGMENT_TAG); } @Override public void onAnimationCancel(Animator animation){ } @Override public void onAnimationRepeat(Animator animation){ } }); } @Override public void onAnimationCancel(Animator animation){ } @Override public void onAnimationRepeat(Animator animation){ } }); animator.start(); } 

I need some kind of pattern that will simplify work with nested listeners.

The only simplification that I see here so far is the removal of the creation of animation listeners into a separate method for each instance.

    1 answer 1

    Create a class that implements Animator.AnimatorListener with empty methods, then when you need some specific one, then redefine it and that's it.

    Example:

     public class SimpleAnimatorListener implements Animator.AnimatorListener { @Override public void onAnimationStart(Animator animation){ } @Override public void onAnimationEnd(Animator animation){ } @Override public void onAnimationCancel(Animator animation){ } @Override public void onAnimationRepeat(Animator animation){ } } 

    Next, use this:

     animator.addListener(new SimpleAnimatorListener() { @Override public void onAnimationEnd(Animator animation){ MyApp.INSTANCE.getRouter().newRootScreen(FEED_FRAGMENT_TAG); } });