android view动画和无动画的行为不同

android view动画和无动画的行为不同,android,animation,view,Android,Animation,View,当我使用案例1时,它按预期工作,但使用动画(案例2)时,视图将永远不可见 案例1> viewstub.setVisibility(View.GONE); mContentLoaded = false; showContentOrLoadingIndicator(mContentLoaded); 在此视图中,将再次变得不可见和可见 案例2> viewstub.setVisibility(View.GONE); viewstub.animate().alpha(0f).setDuratio

当我使用案例1时,它按预期工作,但使用动画(案例2)时,视图将永远不可见

案例1>

viewstub.setVisibility(View.GONE);
mContentLoaded = false;
showContentOrLoadingIndicator(mContentLoaded);  
在此视图中,将再次变得不可见和可见

案例2>

viewstub.setVisibility(View.GONE); 

viewstub.animate().alpha(0f).setDuration(mShortAnimationDuration)
            .setListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    viewstub.setVisibility(View.GONE);
                }
            });
在该视图中,将变得不可见,并且不再可见!有什么解决方案或原因吗?

private void showContentOrLoadingIndicator(boolean contentLoaded) {
        // Decide which view to hide and which to show.
        final View showView = contentLoaded ? listView : mLoadingView;
        final View hideView = contentLoaded ? mLoadingView : listView;

        // Set the "show" view to 0% opacity but visible, so that it is visible
        // (but fully transparent) during the animation.
        showView.setAlpha(0f);
        showView.setVisibility(View.VISIBLE);

        // Animate the "show" view to 100% opacity, and clear any animation
        // listener set on
        // the view. Remember that listeners are not limited to the specific
        // animation
        // describes in the chained method calls. Listeners are set on the
        // ViewPropertyAnimator object for the view, which persists across
        // several
        // animations.
        showView.animate().alpha(1f).setDuration(mShortAnimationDuration)
                .setListener(null);

        // Animate the "hide" view to 0% opacity. After the animation ends, set
        // its visibility
        // to GONE as an optimization step (it won't participate in layout
        // passes, etc.)
        hideView.animate().alpha(0f).setDuration(mShortAnimationDuration)
                .setListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        hideView.setVisibility(View.GONE);
                    }
                });
    }
View.Gone
()将视图设置为从UI中消失。它仍然在那里,但任何其他UI元素都显示在它上面。消失的视图不占用任何空间


要再次显示,您应该将可见性设置回
视图。Visible
()。

我不完全理解确切原因,也没有深入研究文档,但我想我可以为您指出正确的方向

问题似乎来自代码的这一部分:

showView.animate().alpha(1f).setDuration(mShortAnimationDuration)
            .setListener(null);
这就好像动画可以运行并完成,然后才将侦听器设置为
null
(这与此相反。因此,如果列表中设置了
AnimatorListenerAdapter
,则在调用
showContentOrLoadingIndicator(true);

@Override
public void onAnimationEnd(Animator animation) {
    hideView.setVisibility(View.GONE);
}
方法将在侦听器设置为
null
之前隐藏列表。 我通过在
onAnimationEnd
中记录设置为
GONE
的视图来发现这一点

因此,解决方案是在方法链的前面将侦听器设置为null:

showView.animate()
    .setListener(null)
    .alpha(1f)
    .setDuration(mShortAnimationDuration)
如果有人能详细解释这是如何发生的,我会有兴趣接受进一步的教育

希望这有帮助