Forum Xamarin Xamarin.Android

Expand and collapse animation of a layout in xamarin studio

e1den02.0415e1den02.0415 USMember
edited November 2013 in Xamarin.Android

well i just going to put this post because it was a problem i already solve it and i think it can help to someone else this code here is my solution in android for xamarin studio
this part go on my activity
`protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);

        // Set our view from the "main" layout resource
        SetContentView (Resource.Layout.expandable_layout);
        mLinearLayout = FindViewById<LinearLayout>(Resource.Id.expandable);
        mLinearLayout.Visibility = ViewStates.Gone;
        mLinearLayoutHeader = FindViewById<LinearLayout> (Resource.Id.header);
        mLinearLayoutHeader.Click += (s, e) => {
            if (mLinearLayout.Visibility.Equals (ViewStates.Gone)) {
                //set Visible
                mLinearLayout.Visibility = ViewStates.Visible;
                int widthSpec = View.MeasureSpec.MakeMeasureSpec (0, MeasureSpecMode.Unspecified);
                int heightSpec = View.MeasureSpec.MakeMeasureSpec (0, MeasureSpecMode.Unspecified);
                mLinearLayout.Measure (widthSpec, heightSpec);

                ValueAnimator mAnimator = slideAnimator (0, mLinearLayout.MeasuredHeight);
                mAnimator.Start ();

            } else {
                //collapse();
                int finalHeight = mLinearLayout.Height;

                ValueAnimator mAnimator = slideAnimator (finalHeight, 0);
                mAnimator.Start ();
                mAnimator.AnimationEnd += (object IntentSender,EventArgs arg) => {
                    mLinearLayout.Visibility = ViewStates.Gone;
                };//mLinearLayout.Visibility = ViewStates.Gone;

            }

        };


    }

    private ValueAnimator slideAnimator(int start, int end) {

        ValueAnimator animator = ValueAnimator.OfInt(start, end);
        //ValueAnimator animator2 = ValueAnimator.OfInt(start, end);
        //  animator.AddUpdateListener (new ValueAnimator.IAnimatorUpdateListener{
        animator.Update +=
            (object sender, ValueAnimator.AnimatorUpdateEventArgs e) => { 
            //  int newValue = (int)
                           //e.Animation.AnimatedValue; // Apply this new value to the object being animated.
            //  myObj.SomeIntegerValue = newValue; 
            var value = (int)animator.AnimatedValue;
            ViewGroup.LayoutParams layoutParams = mLinearLayout.LayoutParameters;
            layoutParams.Height = value;
            mLinearLayout.LayoutParameters=layoutParams;

        }; 


            //      });
        return animator;
    }`






this is de layout

<LinearLayout
    android:id="@+id/header"
    android:layout_width="fill_parent"
    android:layout_height="64dp"
    android:background="#FFF"
    android:orientation="horizontal" >

    .....

</LinearLayout>

<LinearLayout
    android:id="@+id/expandable"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#FFF"
    android:orientation="vertical" >

    .....

</LinearLayout>

here i post a video of how it should see the code running

youtube.com/watch?v=Xtd88n9N-eo

Answers

Sign In or Register to comment.