让ListView在动画中增加高度

时间:2014-04-24 13:15:13

标签: android android-layout android-listview android-animation objectanimator

我有这个布局用于测试一些Animations

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/frame1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#00ff00"
        android:orientation="vertical">
        <TextView
            android:id="@+id/text1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="1"/>
        <TextView
            android:id="@+id/text2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="2"/>
    </LinearLayout>
    <LinearLayout
        android:id="@+id/frame2"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#0000ff">

        <ListView
            android:id="@+id/list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/edit_style"/>

    </LinearLayout>
</LinearLayout>

我想要实现的是,LinearLayout list中的ListView frame2应该增长到屏幕顶部,而LinearLayout frame1会缩小。

这可以通过ObjectAnimatorAnimatorSet这样轻松完成:

ObjectAnimator animframe1 = ObjectAnimator.ofInt(frame1, "bottom", 0);
ObjectAnimator animframe2 = ObjectAnimator.ofInt(frame2, "top", 0);
ObjectAnimator listview = ObjectAnimator.ofInt(list, "bottom", frame2.getBottom());

AnimatorSet set = new AnimatorSet();
set.setDuration(1000L);
set.playTogether(animframe1, animframe2, listview);
set.start();

我所做的是操纵视图的topbottom属性以获得增长/缩小效果。

它的工作方式与我想要的一样,但有一个缺陷:当ListView正在增长时,附加项目(在那里)是不可见的。房间应该在那里,但只有在动画后点击ListView才会显示其他项目。

如何在动画运行时说ListView应该重新加载其他项?

我尝试在list.invalidate()中拨打AnimationUpdateListener,但没有成功。如果我拨打list.requestLayout(),则不会有动画。

1 个答案:

答案 0 :(得分:1)

我遗漏的是在修改height属性后设置ListView的top。如果没有这个,ListView将保持相同的旧高度,这就是不会显示其他项目的原因。

我切换到了ValueAnimator,因为在CustomListView类中不需要为height添加额外的setter。

代码是这样的:

public void animateListHeight() {
    PropertyValuesHolder topList = PropertyValuesHolder.ofInt("top", mListView.getTop(), 5);

    ValueAnimator animList = ValueAnimator.ofPropertyValuesHolder(topList);
    animList.setDuration(300L);

    AnimatorUpdateListener listUpdater = new AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            int top = ((Integer)animation.getAnimatedValue("top")).intValue();
            mListView.setTop(top);
            ViewGroup.LayoutParams params = (ViewGroup.LayoutParams) mListView.getLayoutParams();
            params.height = mListView.getBottom() - top;
            mListView.setLayoutParams(params);
        }
    };

    animList.addUpdateListener(listUpdater);
    animList.start();
}