出现这种情况很可能是因为View设置了animation,并且调用了setFillAfter(true),这就会导致setVisibility无效,只需要调用一下clearAnimation()方法或者去掉setFillAfter(true)语句即可。
实验证明只要在setVisibility之前设置View的mCurrentAnimation为null就可以,因此调用setAnimation(null)也是可以的。从源码来看setFillAfter为false时也会间接导致调用clearAnimation,而clearAnimation在这里使setVisibility有效的原因也是置mCurrentAnimation为null了。
setFillAfter为false时间接导致clearAnimation:
ViewGroup 中 finishAnimatingView(final View view, Animation animation)
if (animation != null && !animation.getFillAfter()) {
view.clearAnimation();
}
而mCurrentAnimation使GONE无效的初步分析很可能是ViewGroup 中 dispatchDraw(Canvas canvas)
transientChild.getAnimation() != null
if ((transientChild.mViewFlags & VISIBILITY_MASK) == VISIBLE ||
transientChild.getAnimation() != null) {
more |= drawChild(canvas, transientChild, drawingTime);
}
在dispatchDraw子View的时候,尽管不为VISIBLE,由于是||的关系,也会导致drawChild被调用
protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
return child.draw(canvas, this, drawingTime);
}