2012-08-17 120 views
13

我收到了一個帶有項目的ListView。 當用戶點擊一個項目時,它的高度應該縮放爲零,並且下面的所有項目都應該向上滾動。 我的代碼在下面不起作用。 隨着我的代碼被點擊的項目縮放正確,但下面的項目不向上滾動,他們留在相同的位置。 我也試過用LinearLayout,但也有同樣的問題。ListView動畫單個項目

有一個應用程序可以做到這一點。它叫做Tasks

This little image should explain the problem

我當前的實現看起來是這樣的:

@Override 
public void onItemClick(AdapterView<?> arg0, View v, final int index, 
     long id) { 
    Animation anim = AnimationUtils.loadAnimation(getActivity(), 
      R.anim.scaleup); 
    v.startAnimation(anim); 
} 

<set android:shareInterpolator="false" > 
    <scale 
     android:duration="700" 
     android:fillAfter="false" 
     android:fillBefore="false" 
     android:fromXScale="1.0" 
     android:fromYScale="1.0" 
     android:interpolator="@android:anim/accelerate_decelerate_interpolator" 
     android:pivotY="0%" 
     android:toXScale="1.0" 
     android:toYScale="0.0" /> 
</set> 

+1

你目前的實現是什麼樣的? – wsanville 2012-08-17 16:38:11

+0

@wsanville新增源碼 – 2012-08-17 16:42:04

+0

它真的很難理解你所要求的 – 2012-08-17 17:15:26

回答

7

這裏的我做的一個類(從source I found here修改),可能會給你你想要的功能。

public class FadeUpAnimation extends Animation { 

int mFromHeight; 
View mView; 

public FadeUpAnimation(View view) { 
    this.mView = view; 
    this.mFromHeight = view.getHeight(); 
} 

@Override 
protected void applyTransformation(float interpolatedTime, Transformation t) { 
    int newHeight; 
    newHeight = (int) (mFromHeight * (1 - interpolatedTime)); 
    mView.getLayoutParams().height = newHeight; 
    mView.setAlpha(1 - interpolatedTime); 
    mView.requestLayout(); 
} 

@Override 
public void initialize(int width, int height, int parentWidth, 
     int parentHeight) { 
    super.initialize(width, height, parentWidth, parentHeight); 
} 

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

那麼這就是我如何使用它

View tv = ... 
Animation a = new FadeUpAnimation(tv); 
a.setInterpolator(new AccelerateInterpolator()); 
a.setDuration(300); 
tv.setAnimation(a); 
tv.startAnimation(a); 

你可以用它玩,看看你能得到它,以適應您的需求。

+0

嘿,我正在使用它來摺疊listitem,但是當用戶按下撤消時,我需要項目回來。任何方式我可以扭轉動畫?我試過'newHeight =(int)(mFromHeight *(1 + interpolatedTime));'將minus改爲+但這會產生奇怪的效果 – Zen 2014-04-14 06:32:41

+0

'interpolatedTime'從0到1插值,所以刪除'0-'應該產生相反的效果。您還必須更改'mFromHeight'(可能重命名爲'mToHeight')以初始化爲視圖的自然高度。例如'this.mToHeight = view.getMeasuredHeight();' – devnate 2014-04-24 03:01:30

+0

有一件事:如果height設置爲0,視圖會再次將其全高(至少使用我的代碼),所以請執行諸如'mView.getLayoutParams()。height =數學。max(newHeight,1);' – bluewhile 2014-08-24 13:29:00

0

而Android不會像HTML,當你改變寬度,高度或視圖中的可見性,其他視圖不會更新他們的位置。 如果你需要這樣做,你必須單獨更新所有的listview和你的視圖。這是一個複雜的代碼。

+0

錯誤。正如你可以在@devnate中看到的答案,你可以強制查看邊界更新非常簡單。 – bluewhile 2014-08-24 13:27:33

3

你會再次使用點擊的項目嗎?如果沒有,那麼你可以

  1. 等到動畫結束
  2. 無論你存儲在墊層數據刪除項目
  3. 然後打電話給你的listadapter的notifyDataSetChanged()方法
+0

這是正確的。使用附加到「onAnimationEnd」動畫的動畫監聽器,然後按照建議進行操作。 – 2013-09-10 12:45:33

+0

這並沒有回答這個問題 - 當目標視圖縮放爲空時,較低的視圖不會向上移動,只有在動畫結束後,它們纔會卡入到位。 – Tom 2014-11-25 23:01:22