2010-10-03 76 views
5

我有一個使用自定義適配器來顯示我的自定義內容的列表視圖。它的佈局如下。將動畫添加到ListView以展開/摺疊內容

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="1"> 
     <ImageView 
      android:id="@+id/itemimage" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="5" 
      android:scaleType="fitCenter"/> 
     <TextView 
      android:id="@+id/itemdescription" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:padding="10dp" 
      android:textSize="16sp" 
      android:layout_weight="1"/> 
    </LinearLayout>  
    <TextView 
      android:id="@+id/itemtext" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:text="TEXT CONTENT" 
      android:layout_weight="1"/> 
</LinearLayout>  

我想列表視圖只顯示了視圖與ids itemimage和項目描述,保持itemtext隱藏。 這個想法是在列表中的每個項目上都有一個onclicklistener,以便展開該項目以顯示itemtext內容。我知道我應該使用補間動畫來展開/摺疊每個項目,但我無法弄清楚如何做到這一點。

任何人都可以幫助我嗎?如果您需要更多代碼片段,請隨時詢問。

在此先感謝。

+0

嗨,你終於明白了嗎?我會對解決方案感興趣。 – dragoon 2013-06-27 22:34:36

回答

7

爲此,我構建了一個Animation類,它將邊距動畫化爲負值,使項目消失。

動畫看起來是這樣的:

public class ExpandAnimation extends Animation { 
@Override 
protected void applyTransformation(float interpolatedTime, Transformation t) { 
    super.applyTransformation(interpolatedTime, t); 

    if (interpolatedTime < 1.0f) { 

     // Calculating the new bottom margin, and setting it 
     mViewLayoutParams.bottomMargin = mMarginStart 
       + (int) ((mMarginEnd - mMarginStart) * interpolatedTime); 

     // Invalidating the layout, making us seeing the changes we made 
     mAnimatedView.requestLayout(); 
    } 
} 
} 

我有這個動畫整個例子應用在我的博客post

0

試圖Udinic的解決方案,但最終選擇了這種替代:

RES/anim/scale_down.xml

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android" > 
<scale 
    android:duration="700" 
    android:fromXScale="1.0" 
    android:fromYScale="1.0" 
    android:pivotX="50%" 
    android:pivotY="0%" 
    android:toXScale="1.0" 
    android:toYScale="0.0" /> 
</set> 

An imate的ListView實現:

protected void animateView(final View v, final int animResId, final int endVisibility){ 
    Animation anim = AnimationUtils.loadAnimation(getApplicationContext(), 
      animResId); 
    anim.setAnimationListener(new Animation.AnimationListener() { 
     public void onAnimationStart(Animation animation) { 
      v.setVisibility(View.VISIBLE); 
     } 
     public void onAnimationEnd(Animation animation) { 
      v.setVisibility(endVisibility); 
     } 
     public void onAnimationRepeat(Animation animation) {} 
    }); 
    v.startAnimation(anim); 
} 

調用示例動畫我的ListView(或任何View子類):

animateView(listView1, R.anim.scale_down, View.GONE); 
animateView(listView1, R.anim.scale_up, View.VISIBLE); 

這是我的工作電話奇巧上。