2010-04-24 86 views
5

我在android中有一個簡單的ListView列表結果。點擊每個項目後,我希望它向下滑動展開並顯示內容。有沒有一種簡單的方法在android中做到這一點?在android中滑動展開動畫

任何幫助將不勝感激。

回答

3

看看這個answer。不僅如此,你必須使用粗花呢動畫。檢查ApiDemos/Animation2示例。並且還可以在ApiDemos中查看anim文件夾。它對我很有幫助。根據你的問題slide_top_to_bottom將有所幫助。

5

這是來自Udinic的例子。它已經ListView項擴大與動畫,並要求你需要一個動畫類

/** 
* This animation class is animating the expanding and reducing the size of a view. 
* The animation toggles between the Expand and Reduce, depending on the current state of the view 
* @author Udinic 
* 
*/ 
public class ExpandAnimation extends Animation { 
    private View mAnimatedView; 
    private LayoutParams mViewLayoutParams; 
    private int mMarginStart, mMarginEnd; 
    private boolean mIsVisibleAfter = false; 
    private boolean mWasEndedAlready = false; 

    /** 
* Initialize the animation 
* @param view The layout we want to animate 
* @param duration The duration of the animation, in ms 
*/ 
    public ExpandAnimation(View view, int duration) { 

     setDuration(duration); 
     mAnimatedView = view; 
     mViewLayoutParams = (LayoutParams) view.getLayoutParams(); 

     // decide to show or hide the view 
     mIsVisibleAfter = (view.getVisibility() == View.VISIBLE); 

     mMarginStart = mViewLayoutParams.bottomMargin; 
     mMarginEnd = (mMarginStart == 0 ? (0- view.getHeight()) : 0); 

     view.setVisibility(View.VISIBLE); 
    } 

    @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(); 

     // Making sure we didn't run the ending before (it happens!) 
     } else if (!mWasEndedAlready) { 
      mViewLayoutParams.bottomMargin = mMarginEnd; 
      mAnimatedView.requestLayout(); 

      if (mIsVisibleAfter) { 
       mAnimatedView.setVisibility(View.GONE); 
      } 
      mWasEndedAlready = true; 
     } 
    } 
} 

API級別只4+ 基本上並使用此:

View toolbar = view.findViewById(R.id.toolbar); 

       // Creating the expand animation for the item 
       ExpandAnimation expandAni = new ExpandAnimation(toolbar, 500); 

       // Start the animation on the toolbar 
       toolbar.startAnimation(expandAni); 

ExpandAnimationExample

+0

對於downvoter,你應該離開你投下我的答案的原因。我知道我的答案可以被視爲僅鏈接答案,但你想要什麼?您希望我將該回購中的所有文件複製到此處。因此,Github鏈接是可靠的 – 2013-08-06 01:25:55

+1

請注意,這個例子仍然是越野車。 https://github.com/Udinic/SmallExamples/issues/6當你向上和向下滾動列表視圖時,展開/摺疊狀態會混亂。 – 2013-10-10 18:18:27

+0

@CheokYanCheng任何解決混亂的狀態?很明顯,當我們滑動時存在一個問題,使得視圖滑落,現在我們改變可見性以便動畫。但是有一個閃爍,視圖關閉,然後再次打開向下滑動:( – beerBear 2014-12-10 11:54:31

1

最簡單的方法是使用一個ObjectAnimator

ObjectAnimator animation = ObjectAnimator.ofInt(yourTextView, "maxLines", 40); 
animation.setDuration(200).start(); 

這會將maxLines從TextView更改爲40,超過200毫秒。

要小心使用yourTextView.getLineCount()來確定要擴展到多少行,因爲直到佈局傳遞後它纔會給出準確的數字。我建議你只編寫一個maxLines值,這個值比你期望的文本長。您還可以使用yourTextView.length()除以每行所期望的最小字符數來估計它。