-1

I was having a problem, where setVisibility() would not work properly after applying an animation to a view once.

PROBLEM
public void show():
1. setVisibility(View.VISIBLE) is called on a view
2. View appearance is animated: view.animate().alpha(1.0f).setDuration(3000). etc...

public void hide()
1. View disappearance is animated: view.animate().alpha(0.0f).setDuration(3000). etc...
2. View visibility is set to GONE

  • Very first show() method call displays everything correctly.
  • After calling hide() afterwards, views are hidden correctly.
  • Calling show() now, animates the view appearance up to the very ending of the animation, immediately after which the view disappears (Layout inspector indicates its final visibility is GONE)

This is my initial animation code:

myGridView.animate()
    .alpha(0.0f)
    .setDuration(100);

Surprisingly, adding AnimatorListenerAdapter and overriding onAnimationEnd without doing anything else solved the problem. Recently, I have found other SO solution, where they call clearAnimation() on the view before setVisibility().

So, this in the code worked:

myGridView.clearAnimation();
myGridView.setVisibility(View.VISIBLE);

My final code I sticked with:

myGridView.animate()
        .alpha(0.0f)
        .setDuration(100);
        .setListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                myGridView.clearAnimation();
                myGridView.setVisibility(GridView.GONE);
            }
        });

QUESTION
Why overriding onAnimationEnd worked on its own? Is it somehow related to clearAnimation ?

Morphing Coffee
  • 815
  • 1
  • 10
  • 20
  • I couldn't get what didn't happen previously that now happens. – azizbekian Apr 11 '17 at 13:54
  • @azizbekian Fair point. I have edited the question now. The view used to disappear right after animation finishes, although before starting the animation I set its visibility to VISIBLE. After overriding onAnimationEnd method it just remains VISIBLE not only when animation starts, but also after it finishes executing. – Morphing Coffee Apr 11 '17 at 16:30

1 Answers1

0

Why overriding onAnimationEnd worked on its own? Is it somehow related to clearAnimation?

clearAnimation() would initiate onAnimationCancel() followed by onAnimationEnd() to be called on the Animation that this view has.

azizbekian
  • 60,783
  • 13
  • 169
  • 249