2014-01-30 51 views
0

当我使用案例1时,它按预期工作,但使用动画(case2)视图永远不会变得可见!android视图动画和没有动画的行为不同

情况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); 
        } 
       }); 
    } 

回答

1

我并不完全明白确切的原因,也没有深入研究文档,但我认为我可以指出您正确的方向。

看来,这个问题来自于这部分代码:

showView.animate().alpha(1f).setDuration(mShortAnimationDuration) 
      .setListener(null); 

这是因为如果动画可以运行并完成,然后才听众设置为null(这违背the auto-start property所以,如果您的列表。有一个AnimatorListenerAdapter集来,呼吁showContentOrLoadingIndicator(true);当那么

@Override 
public void onAnimationEnd(Animator animation) { 
    hideView.setVisibility(View.GONE); 
} 

方法之前监听器设置为null将隐藏列表。 我发现这个通过日志记录获取onAnimationEnd

设置为GONE视图,以便解决办法是设置监听器为空早些时候方法链:

showView.animate() 
    .setListener(null) 
    .alpha(1f) 
    .setDuration(mShortAnimationDuration) 

如果有人可以提供有关详细的解释如何发生这种情况,我有兴趣得到进一步的教育。

希望这会有所帮助

0

View.Gonesource)设置要从UI不见了视图。它仍然存在,但其他任何UI元素都显示在其上。消失的视图不占用任何空间。

要再次显示它,您应该将可见性设置回View.Visiblesource)。