LayoutAnimationController 在隐藏视图组时不起作用,但在显示视图组时起作用

LayoutAnimationController not working when hiding View Group but works when Showing view group

LayoutAnimationController 用于为视图组的子项设置动画

我使用LayoutAnimationControllerLinearLayout中用下面的代码一个一个地显示动画效果的元素。

     Animation fadeIn = AnimationUtils.loadAnimation(context, R.anim.anim_fade_in);
//lnrContactContainer is LinearLayout.
            AnimationSet set = new AnimationSet(true);
            set.addAnimation(fadeIn);
            set.setDuration(500);
            controller = new LayoutAnimationController(set, 1f);
            lnrContactContainer.setLayoutAnimation(controller);          
            lnrContactContainer.setVisibility(View.VISIBLE);

但是当我在隐藏 LinearLayout lnrContactContainer.setVisibility(View.GONE) 时使用它来显示淡出动画时,同样的方法不起作用;

它隐藏了父项,而不是一个一个地隐藏子项。

Instead of hiding children one by one it hides the parent.

要仅在 Animation 应用于所有子项后才隐藏父项,请使用 AnimationListener:

lnrContactContainer.setLayoutAnimationListener(new Animation.AnimationListener()
        {
            @Override
            public void onAnimationStart(Animation animation){}

            @Override
            public void onAnimationEnd(Animation animation)
            {
                lnrContactContainer.setVisibility(View.GONE)
            }

            @Override
            public void onAnimationRepeat(Animation animation){}
        });

顺便说一下,我的淡出动画需要

set.setFillAfter(true);

尽管我的动画 xml 文件(在 res/anim 中)已经包含 android:fillAfter="true".

,但要防止项目在褪色后再次弹出