将动画添加到ListView以展开/折叠内容

时间:2010-10-03 20:13:06

标签: android listview animation collapse

我有一个列表视图,它使用自定义适配器来显示我的自定义内容。它的布局如下。

<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>    

我希望listview只显示带有ids itemimage和项目描述的视图,保持itemtext隐藏。 我们的想法是在列表的每个项目上都有一个onclicklistener,以便扩展该项目,以便显示itemtext内容。我知道我应该使用Tweening动画来展开/折叠每个项目,但我无法弄清楚如何做到这一点。

任何人都可以帮助我吗?如果您需要更多代码段,请随时询问。

提前致谢。

2 个答案:

答案 0 :(得分: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

上有一个完整的动画示例应用

答案 1 :(得分:0)

尝试过Udinic的解决方案,但最终选择了这个替代方案:

RES /动画/ 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>

Animate 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);

正在使用我的KitKat手机。

相关问题