我在我的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);
}
你可以像这样尝试,而不是上面的,使你的子视图的父布局为线性布局,并应用'android:animateLayoutChanges =“true” '。现在你可以用动画显示和隐藏它! –
@AldrinMathew,我尝试过但没有发生任何事情。我想访问如何动画像持续时间 – Maryam
可能的重复http://stackoverflow.com/questions/22454839/Android%20adding%20simple%20animations%20while%20setvisibility(view.Gone)/22454966 –