Android:如何编写 layoutAnimation 而不是 xml

Android: How to code layoutAnimation instead of xml

我要给 ListView 的项目添加布局动画,我以前用过 android:layoutAnimation

<LinearLayout
         ...
         android:layoutAnimation="@anim/layout_anim" />

// ../res/anim
<layoutAnimation 
         xmlns:android="http://schemas.android.com/apk/res/android"
         android:animation="@anim/slide_up" />

我尝试在适配器中提供以下代码。

public View getView(int position, View convertView, ViewGroup parent) {
        ...
        Animation animation = AnimationUtils.loadAnimation(context, R.anim.slide_up);
        v.startAnimation(animation);

        return v;
}

它可以工作,但是 slide_up 动画是由完整的 activity 级别而不是项目级别实现的。它出现在 activity 的底部。本来它应该出现在项目的底部。

我做错了什么?

我使用 LayoutAnimationController 解决了。

LinearLayout layout = (LinearLayout) lineView.findViewById(R.id.linearLayout);
LayoutAnimationController anim = AnimationUtils.loadLayoutAnimation(context, R.anim.layout_anim);
layout.setLayoutAnimation(anim);