Search code examples
androidanimationlayout-animation

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


LayoutAnimationController is used to animate children of view group

I used LayoutAnimationController to show elements in LinearLayout with animation effect one by one using following code.

     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);

But same approach does not work when I use it to show fadeout animation while hiding LinearLayout lnrContactContainer.setVisibility(View.GONE);

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


Solution

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

    To hide the parent only after the Animation has been applied to all children, use an 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){}
            });
    

    By the way, my fadeout animation needed

    set.setFillAfter(true);
    

    to keep the items from popping in again after fading although my animation xml file (in res/anim) already contained android:fillAfter="true".