2

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.

DeltaCap019
  • 6,532
  • 3
  • 48
  • 70

1 Answers1

2

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".

Bö macht Blau
  • 12,820
  • 5
  • 40
  • 61
  • `set.setFillAfter(true); ` thanks for directing me to this , I had this issue of children popping in again. – DeltaCap019 Feb 11 '16 at 07:15
  • Just one question, why I have to start animation manually using `lnrContactContainer.startLayoutAnimation();` in case of hiding the `ViewGroup` but not in case of showing it ? – DeltaCap019 Feb 11 '16 at 07:16
  • 1
    @Null n Void - in my test app, I triggered both types of animation by clicking on buttons. Both times, "lnrContactContainer.setLayoutAnimation(controller);" was called and the animation started right afterwards. OK, the difference is that I tested with a ListView not with a LinearLayout. So I don't know why, but there are some other differences between AdapterViews and "real" layouts . – Bö macht Blau Feb 11 '16 at 07:29