Android ViewGroup AnimationListener未启动
我正在从启用animateLayoutChanges的父对象中删除子对象。我想知道动画何时完成,但无法启动侦听器:Android ViewGroup AnimationListener未启动,android,Android,我正在从启用animateLayoutChanges的父对象中删除子对象。我想知道动画何时完成,但无法启动侦听器: <LinearLayout android:id="@+id/parent" android:animateLayoutChanges="true"> <TextView android:id="@+id/child" /> </LinearLayout> void testRemove() { ViewGr
<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.
}
};
在LinearLayout xml中使用布局动画属性 此处的文档: 将文件添加到
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" />
现在,稍微更改LinearLayoutXML以包含android:layoutImation
属性和值
<LinearLayout
android:id="@+id/parent"
android:layoutAnimation="@anim/layout_fade_in"
android:animateLayoutChanges="true">
<TextView android:id="@+id/child" />
</LinearLayout>
进行了这些更改,但动画侦听器仍然没有启动。我试着添加一个TransitionListener,结果被激活了。用它更新了问题。
<?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
android:id="@+id/parent"
android:layoutAnimation="@anim/layout_fade_in"
android:animateLayoutChanges="true">
<TextView android:id="@+id/child" />
</LinearLayout>