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
?