ViewGroup AnimationListener未触发

时间:2014-12-12 19:57:47

标签: android

我正在使用animateLayoutChanges从父级中删除一个孩子。我想知道动画何时完成,但无法触发听众:

<LinearLayout
    android:id="@+id/parent"
    android:animateLayoutChanges="true">

    <TextView android:id="@+id/child" />

</LinearLayout>

void testRemove() {
    ViewGroup parent = (ViewGroup)findViewById(R.id.parent);
    parent.setLayoutAnimationListener(listener);

    TextView child = findViewById(R.id.child);
    child.setVisibility(View.GONE); // this triggers the animation.
}

Animation.AnimationListener listener = new Animation.AnimationListener() {
    @Override
    public void onAnimationStart(Animation animation) {
        // None of these callback methods fire.
    }
};

听众不会解雇的任何想法?

由于

http://developer.android.com/reference/android/view/ViewGroup.html#setLayoutAnimationListener(android.view.animation.Animation.AnimationListener)

--------更新------------------

TransitionListener似乎有效:

LayoutTransition layoutTransition = parent.getLayoutTransition();
layoutTransition.addTransitionListener(new TransitionListener());

1 个答案:

答案 0 :(得分:0)

使用LinearLayout xml中的布局动画属性

文档:http://developer.android.com/reference/android/view/ViewGroup.html#attr_android:layoutAnimation

将文件添加到fade_in.xml的res / anim文件夹中,这将是您的淡入淡出动画

<?xml version="1.0" encoding="utf-8"?>

<alpha xmlns:android="http://schemas.android.com/apk/res/android"
       android:interpolator="@android:anim/decelerate_interpolator"
       android:fromAlpha="0.0" android:toAlpha="1.0"
       android:duration="@android:integer/config_mediumAnimTime" />

然后将文件添加到layout_fade_in.xml的res / anim文件夹中,该文件夹引用您刚刚制作的淡入淡出动画@anim/fade_in,此文件将用于android:layoutAnimation

<?xml version="1.0" encoding="utf-8"?>
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
        android:delay="0.5"
        android:animation="@anim/fade_in" />

现在,稍微更改LinearLayout xml以包含android:layoutAnimation属性和值

<LinearLayout
    android:id="@+id/parent"
    android:layoutAnimation="@anim/layout_fade_in"
    android:animateLayoutChanges="true">

    <TextView android:id="@+id/child" />

</LinearLayout>