2017-04-26 131 views
1

我在我的android项目中有一个RecyclerView,它在每行都有2 LinearLayout。首先我想让那些布局中的一个变得不复存在。在点击行之后,它会通过动画显示。总之我想实现一个可扩展的RecyclerView而不需要任何外部库。你能给我一个解决方案吗? 我想通过动画hoder.itemView的高度来实现这一点。但实际上我不知道要插入什么,而不是????,因为起初我不知道它的高度。设置可见性从动漫到可见动画在RecyclerView

if(holder.layout2.getVisibility() == View.Gone) { 
      holder.layout2.setVisibility(View.VISIBLE); 
      animateHeight(holder.itemView,?????); 
     } 
     else { 
      animateHeight(holder.itemView,holder.itemView.getHeight - holder.layout2.getHeight()); 
      holder.layout2.setVisibility(View.GONE); 
     } 

public void animateHeight(final View v, final int height) { 

    final int initialHeight = v.getMeasuredHeight(); 
    int duration = 500; 
    Interpolator interpolator = new AccelerateInterpolator(); 

    // I have to set the same height before the animation because there is a glitch 
    // in the beginning of the animation 
    v.getLayoutParams().height = initialHeight; 
    final int diff = height - initialHeight; 
    v.requestLayout(); 

    Animation a = new Animation() { 
     @Override 
     protected void applyTransformation(float interpolatedTime, Transformation t) { 
      v.getLayoutParams().height = initialHeight + (int) (diff * interpolatedTime); 
      v.requestLayout(); 
     } 

     @Override 
     public boolean willChangeBounds() { 
      return true; 
     } 
    }; 

    a.setDuration(duration); 
    a.setInterpolator(interpolator); 
    v.startAnimation(a); 
} 
+0

你可以像这样尝试,而不是上面的,使你的子视图的父布局为线性布局,并应用'android:animateLayoutChanges =“true” '。现在你可以用动画显示和隐藏它! –

+0

@AldrinMathew,我尝试过但没有发生任何事情。我想访问如何动画像持续时间 – Maryam

+1

可能的重复http://stackoverflow.com/questions/22454839/Android%20adding%20simple%20animations%20while%20setvisibility(view.Gone)/22454966 –

回答

0

做这样的事情在你的观点持有者类获取每个孩子的身高,

itemView.getViewTreeObserver().addOnGlobalLayoutListener( 
    new OnGlobalLayoutListener(){ 

     @Override 
     public void onGlobalLayout() { 

      mHeight = mView.getHeight(); 
      mView.getViewTreeObserver().removeGlobalOnLayoutListener(this); 
      mView.setVisibility(View.GONE); 
     } 

}); 

这将是处于扩展状态的儿童的身高,你可以在以后你什么时候使用它正试图为视图添加动画。

+0

我做到了这一点,但它显示为第二秒,然后隐藏它,所以我有一个闪光灯:( – Maryam