collapse ViewGroup - Android User Interface

Android examples for User Interface:ViewGroup

Description

collapse ViewGroup

Demo Code


//package com.java2s;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.Transformation;

import java.lang.reflect.Method;

public class Main {
    public static void collapse(final ViewGroup v, int duration) {
        slide(v, duration, false);/*  w ww.  j  a v a 2  s .com*/
    }

    private static void slide(final ViewGroup v, int duration,
            final boolean expand) {
        try {
            //onmeasure method is protected
            Method m = v.getClass().getDeclaredMethod("onMeasure",
                    int.class, int.class);
            m.setAccessible(true);
            m.invoke(v, View.MeasureSpec.makeMeasureSpec(
                    ((View) v.getParent()).getMeasuredWidth(),
                    View.MeasureSpec.AT_MOST), View.MeasureSpec
                    .makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
        } catch (Exception e) {
            Log.e("slideAnimation", e.getMessage(), e);
        }

        final int initialHeight = v.getMeasuredHeight();

        if (expand) {
            v.getLayoutParams().height = 0;
        } else {
            v.getLayoutParams().height = initialHeight;
        }
        v.setVisibility(View.VISIBLE);

        Animation a = new Animation() {
            @Override
            protected void applyTransformation(float interpolatedTime,
                    Transformation t) {
                int newHeight = 0;
                if (expand) {
                    newHeight = (int) (initialHeight * interpolatedTime);
                } else {
                    newHeight = (int) (initialHeight * (1 - interpolatedTime));
                }
                v.getLayoutParams().height = newHeight;
                v.requestLayout();

                if (interpolatedTime == 1 && !expand) {
                    v.setVisibility(View.GONE);
                }
            }

            @Override
            public boolean willChangeBounds() {
                return true;
            }
        };

        a.setDuration(duration);
        v.startAnimation(a);
    }
}

Related Tutorials