我在我的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 –