Search code examples
androidviewandroid-animationandroid-transitionsobjectanimator

Android animate view does not resize other view


I have an issue on Android and can't understand why. I have the following layout:

<?xml version="1.0" encoding="utf-8"?>
    <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:custom="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <RelativeLayout
            android:id="@+id/Container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/background_primary">
            <RelativeLayout
                android:id="@+id/RecyclerViewContainer"
                android:layout_below="@+id/UpdatesBanner"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
                <android.support.v7.widget.RecyclerView
                    android:id="@+id/RecyclerView"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical"
                    android:scrollbars="none"
                    android:clipToPadding="false"
                    android:splitMotionEvents="false"
                    android:requiresFadingEdge="none"
                    android:overScrollMode="never" />
                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@android:color/holo_red_dark" />
            </RelativeLayout>
            <Droid.UIElements.UIBanner
                android:id="@+id/UpdatesBanner"
                android:layout_width="match_parent"
                android:layout_height="130dp"
                android:layout_alignParentTop="true"
                android:background="@android:color/holo_green_dark" />
        </RelativeLayout>
    </android.support.design.widget.CoordinatorLayout>

UIBanner is a custom view that content 2 buttons. One of them is binded on a Hide function which will animate the view.

 public void Hide()
    {
        ObjectAnimator animation = ObjectAnimator.OfFloat(this, "TranslationY", TranslationY, TranslationY - Height);
        animation.Start();
    }

This works but the view under the banner keep the same size and does not expand. If i only make a visibility gone on the banner then it works but no animation performed.

Following the states before and after animation:

Before animation

After animation


Solution

  • Best solution is ValueAinmator

    ValueAnimator viewAnim = ValueAnimator.OfInt(view.MeasuredHeight, viewHeight);
    viewAnim.AddUpdateListener(new UpdateListener(view));
    AnimatorSet set = new AnimatorSet();
    set.AnimationEnd += (sender, e) => EndAnim();
    set.Play(viewAnim);
    set.Start();
    

    And the listener

    class UpdateListener : Java.Lang.Object, IAnimatorUpdateListener
    {
         Android.Views.View view;
    
         public UpdateListener(Android.Views.View pView)
         {
             view = pView;
         }
    
         public void OnAnimationUpdate(ValueAnimator animation)
         {
             int value = (int)animation.AnimatedValue;
             ViewGroup.LayoutParams layoutParams = view.LayoutParameters;
             layoutParams.Height = value;
             view.LayoutParameters = layoutParams;
         }
    }