2011-06-30 43 views
20

我有一个LinearLayout,我希望能够通过单击“更多详细信息”链接来显示/隐藏。我通过调用动画和setVisibility

moreDetailsSection.setVisibility(View.VISIBLE); 

moreDetailsSection.setVisibility(View.GONE); 

显示/隐藏它做到这一点。这工作正常,但我想添加一个动画,使布局字段很好地滑动,但这只在第一次使字段变得可见时运行,如果我隐藏它并再次显示该字段只是突然出现。这里是动画代码(moreDetailsS​​ection是有问题的布局):

 AnimationSet set = new AnimationSet(true); 

     Animation animation = new AlphaAnimation(0.0f, 1.0f); 
     animation.setDuration(250); 
     set.addAnimation(animation); 

     animation = new TranslateAnimation(
      Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF, 0.0f, 
      Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f 
    ); 
     animation.setDuration(150); 
     set.addAnimation(animation); 

     LayoutAnimationController controller = 
      new LayoutAnimationController(set, 0.25f); 
     moreDetailsSection.setLayoutAnimation(controller); 

如何使这个运行每次我展示的布局,而不是仅在第一次有什么建议?

回答

14

我假设moreDetailsS​​ection起初是INVISIBLE。 您只需创建Animation对象并在单击更多详细信息链接时调用以下代码。

moreDetailsSection.startAnimation(animation); 
moreDetailsSection.setVisibility(View.VISIBLE); 
+0

事实证明,该视图将无法测得,而它的知名度是'GONE',所以你需要设置当您使用需要了解视图边界的动画时,首先会看到“VISIBLE”。 – milosmns

2

您可以使用此行的视图,而且布局:

android:animateLayoutChanges="true" 
+0

对于自动和简单的动画,这是要走的路。 – milosmns