Example usage for android.view View setTop

List of usage examples for android.view View setTop

Introduction

In this page you can find the example usage for android.view View setTop.

Prototype

public final void setTop(int top) 

Source Link

Document

Sets the top position of this view relative to its parent.

Usage

From source file:Main.java

static public void setFrame(View v, Rect frame) {
    v.setTop(frame.top);
    v.setLeft(frame.left);//ww  w . j  a va2 s .  com
    v.setBottom(frame.bottom);
    v.setRight(frame.right);
}

From source file:Main.java

static public void setPosition(View v, PointF p) {
    int w = v.getWidth();
    int h = v.getHeight();
    v.setLeft((int) p.x);
    v.setTop((int) p.y);
    v.setRight((int) p.x + w);
    v.setBottom((int) p.y + h);
}

From source file:com.chromium.fontinstaller.util.ViewUtils.java

public static void reveal(Activity activity, View view, View sourceView, int colorRes) {
    if (activity == null || view == null || sourceView == null)
        return;//from  w ww. j  av a 2  s .c  om
    if (isLollipop()) {
        final ViewGroupOverlay groupOverlay = (ViewGroupOverlay) activity.getWindow().getDecorView()
                .getOverlay();

        final Rect displayRect = new Rect();
        view.getGlobalVisibleRect(displayRect);

        // Make reveal cover the display and status bar.
        final View revealView = new View(activity);
        revealView.setTop(displayRect.top);
        revealView.setBottom(displayRect.bottom);
        revealView.setLeft(displayRect.left);
        revealView.setRight(displayRect.right);
        revealView.setBackgroundColor(ContextCompat.getColor(activity, colorRes));
        groupOverlay.add(revealView);

        final int[] clearLocation = new int[2];
        sourceView.getLocationInWindow(clearLocation);
        clearLocation[0] += sourceView.getWidth() / 2;
        clearLocation[1] += sourceView.getHeight() / 2;

        final int revealCenterX = clearLocation[0] - revealView.getLeft();
        final int revealCenterY = clearLocation[1] - revealView.getTop();

        final double x1_2 = Math.pow(revealView.getLeft() - revealCenterX, 2);
        final double x2_2 = Math.pow(revealView.getRight() - revealCenterX, 2);
        final double y_2 = Math.pow(revealView.getTop() - revealCenterY, 2);
        final float revealRadius = (float) Math.max(Math.sqrt(x1_2 + y_2), Math.sqrt(x2_2 + y_2));

        try {
            final Animator revealAnimator = ViewAnimationUtils.createCircularReveal(revealView, revealCenterX,
                    revealCenterY, 0.0f, revealRadius);
            revealAnimator
                    .setDuration(activity.getResources().getInteger(android.R.integer.config_mediumAnimTime));

            final Animator alphaAnimator = ObjectAnimator.ofFloat(revealView, View.ALPHA, 0.0f);
            alphaAnimator
                    .setDuration(activity.getResources().getInteger(android.R.integer.config_shortAnimTime));
            alphaAnimator.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    super.onAnimationEnd(animation);
                    view.startAnimation(AnimationUtils.loadAnimation(activity, R.anim.abc_fade_in));
                    view.setVisibility(View.VISIBLE);
                }
            });

            final AnimatorSet animatorSet = new AnimatorSet();
            animatorSet.play(revealAnimator).before(alphaAnimator);
            animatorSet.setInterpolator(new AccelerateDecelerateInterpolator());
            animatorSet.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animator) {
                    groupOverlay.remove(revealView);
                }
            });

            animatorSet.start();
        } catch (IllegalStateException e) {
            Timber.i("View is detached - not animating");
        }
    } else {
        view.setVisibility(View.VISIBLE);
    }
}

From source file:com.android.clear.reminder.AnimatorUtils.java

/**
 * Returns an animator that animates the bounds of a single view.
 *//*from www . ja  v a  2  s  .  co m*/
public static Animator getBoundsAnimator(View view, int fromLeft, int fromTop, int fromRight, int fromBottom,
        int toLeft, int toTop, int toRight, int toBottom) {
    view.setLeft(fromLeft);
    view.setTop(fromTop);
    view.setRight(fromRight);
    view.setBottom(fromBottom);

    return ObjectAnimator.ofPropertyValuesHolder(view, PropertyValuesHolder.ofInt(VIEW_LEFT, toLeft),
            PropertyValuesHolder.ofInt(VIEW_TOP, toTop), PropertyValuesHolder.ofInt(VIEW_RIGHT, toRight),
            PropertyValuesHolder.ofInt(VIEW_BOTTOM, toBottom));
}

From source file:com.xujun.contralayout.utils.AnimatorUtil.java

public static void show(final View view, int start, int end) {
    final int height = view.getHeight();
    final ValueAnimator animator = ValueAnimator.ofInt(start, end);
    animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override//from ww  w . ja  v a2s . co m
        public void onAnimationUpdate(ValueAnimator valueAnimator) {
            int value = (Integer) animator.getAnimatedValue();
            Log.i(TAG, "onAnimationUpdate: value=" + value);
            view.setTop(value);
            view.setBottom(value + height);
        }
    });
    view.setVisibility(View.VISIBLE);
    animator.setDuration(200);
    animator.setInterpolator(FAST_OUT_SLOW_IN_INTERPOLATOR);
    animator.start();
}

From source file:com.rockspoon.rockandui.Components.RelativeLayoutWithSwipe.java

public void setTopView(View view) {
    id = view.getId();//from w ww  . j  a  va2s.com
    view.addOnLayoutChangeListener(new OnLayoutChangeListener() {
        @Override
        public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop,
                int oldRight, int oldBottom) {
            if (isMoving()) {
                v.setTop(oldTop);
                v.setBottom(oldBottom);
                v.setLeft(oldLeft);
                v.setRight(oldRight);
            }
        }
    });
}

From source file:graphic.expand_graphic.ExpandingListView.java

private void init() {
    setOnItemClickListener(mItemClickListener);

    oldCoordinates = new HashMap<View, int[]>();
    collapseLayoutParams = new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT,
            mCollapsedHeight);//  ww w .j a  va  2  s  .  c o m
    generalLayoutParams = new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT,
            AbsListView.LayoutParams.WRAP_CONTENT);

    expandPreDrawListener = new ViewTreeObserver.OnPreDrawListener() {

        @Override
        public boolean onPreDraw() {
            /* Determine if this is the first or second pass.*/
            if (!mShouldRemoveObserver) {
                mShouldRemoveObserver = true;

                /* Calculate what the parameters should be for setSelectionFromTop.
                 * The ListView must be offset in a way, such that after the animation
                 * takes place, all the cells that remain visible are rendered completely
                 * by the ListView.*/
                int newTop = mView.getTop();
                int newBottom = mView.getBottom();

                int newHeight = newBottom - newTop;
                int oldHeight = oldBottom - oldTop;
                int delta = newHeight - oldHeight;

                getTopAndBottomTranslations(oldTop, oldBottom, delta, true);

                int currentTop = mView.getTop();
                int futureTop = oldTop - mTranslate[0];

                int firstChildStartTop = getChildAt(0).getTop();
                int firstVisiblePosition = getFirstVisiblePosition();
                int deltaTop = currentTop - futureTop;

                int i;
                int childCount = getChildCount();
                for (i = 0; i < childCount; i++) {
                    View v = getChildAt(i);
                    int height = v.getBottom() - Math.max(0, v.getTop());
                    if (deltaTop - height > 0) {
                        firstVisiblePosition++;
                        deltaTop -= height;
                    } else {
                        break;
                    }
                }

                if (i > 0) {
                    firstChildStartTop = 0;
                }

                setSelectionFromTop(firstVisiblePosition, firstChildStartTop - deltaTop);

                /* Request another layout to update the layout parameters of the cells.*/
                requestLayout();

                /* Return false such that the ListView does not redraw its contents on
                 * this layout but only updates all the parameters associated with its
                 * children.*/
                return false;
            }

            /* Remove the predraw listener so this method does not keep getting called. */
            mShouldRemoveObserver = false;
            observer.removeOnPreDrawListener(this);

            int yTranslateTop = mTranslate[0];
            int yTranslateBottom = mTranslate[1];

            //   ArrayList <Animator> animations = new ArrayList<Animator>(); optimize

            int index = indexOfChild(mView);

            /* Loop through all the views that were on the screen before the cell was
             *  expanded. Some cells will still be children of the ListView while
             *  others will not. The cells that remain children of the ListView
             *  simply have their bounds animated appropriately. The cells that are no
             *  longer children of the ListView also have their bounds animated, but
             *  must also be added to a list of views which will be drawn in dispatchDraw.*/
            for (View v : oldCoordinates.keySet()) {
                int[] old = oldCoordinates.get(v);
                v.setTop(old[0]);
                v.setBottom(old[1]);
                if (v.getParent() == null) {
                    mViewsToDraw.add(v);
                    int delta = old[0] < oldTop ? -yTranslateTop : yTranslateBottom;
                    animations.add(getAnimation(v, delta, delta));
                } else {
                    int i = indexOfChild(v);
                    if (v != mView) {
                        int delta = i > index ? yTranslateBottom : -yTranslateTop;
                        animations.add(getAnimation(v, delta, delta));
                    }
                    ViewCompat.setHasTransientState(v, false);
                }
            }

            /* Adds animation for expanding the cell that was clicked. */
            animations.add(getAnimation(mView, -yTranslateTop, yTranslateBottom));

            /* Adds an animation for fading in the extra content. */
            animations.add(ObjectAnimator.ofFloat(holder.expandingView, //mView.findViewById(R.id.expanding_layout), optimize
                    View.ALPHA, 0, 1));

            /* Disabled the ListView for the duration of the animation.*/
            setEnabled(false);
            setClickable(false);

            /* Play all the animations created above together at the same time. */
            //   AnimatorSet s = new AnimatorSet(); optimize 
            s.playTogether(animations);
            s.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    //   viewObject.setExpanded(true); optimize
                    viewObject.mIsExpanded = true;
                    setEnabled(true);
                    setClickable(true);
                    s.removeAllListeners();
                    oldCoordinates.clear();
                    animations.clear();
                    if (mViewsToDraw.size() > 0) {
                        for (View v : mViewsToDraw) {
                            ViewCompat.setHasTransientState(v, false);
                        }
                    }
                    mViewsToDraw.clear();
                }
            });
            s.start();
            return true;
        }
    };

    collapsePreDrawListener = new ViewTreeObserver.OnPreDrawListener() {

        @Override
        public boolean onPreDraw() {

            if (!mShouldRemoveObserver) {
                /*Same as for expandingView, the parameters for setSelectionFromTop must
                 * be determined such that the necessary cells of the ListView are rendered
                 * and added to it.*/
                mShouldRemoveObserver = true;

                int newTop = mView.getTop();
                int newBottom = mView.getBottom();

                int newHeight = newBottom - newTop;
                int oldHeight = oldBottom - oldTop;
                int deltaHeight = oldHeight - newHeight;

                getTopAndBottomTranslations(oldTop, oldBottom, deltaHeight, false);

                int currentTop = mView.getTop();
                int futureTop = oldTop + mTranslate[0];

                int firstChildStartTop = getChildAt(0).getTop();
                int firstVisiblePosition = getFirstVisiblePosition();
                int deltaTop = currentTop - futureTop;

                int i;
                int childCount = getChildCount();
                for (i = 0; i < childCount; i++) {
                    View v = getChildAt(i);
                    int height = v.getBottom() - Math.max(0, v.getTop());
                    if (deltaTop - height > 0) {
                        firstVisiblePosition++;
                        deltaTop -= height;
                    } else {
                        break;
                    }
                }

                if (i > 0) {
                    firstChildStartTop = 0;
                }

                setSelectionFromTop(firstVisiblePosition, firstChildStartTop - deltaTop);

                requestLayout();

                return false;
            }

            mShouldRemoveObserver = false;
            observer.removeOnPreDrawListener(this);

            int yTranslateTop = mTranslate[0];
            int yTranslateBottom = mTranslate[1];

            int index = indexOfChild(mView);
            int childCount = getChildCount();
            for (int i = 0; i < childCount; i++) {
                View v = getChildAt(i);
                int[] old = oldCoordinates.get(v);
                if (old != null) {
                    /* If the cell was present in the ListView before the collapse and
                     * after the collapse then the bounds are reset to their old values.*/
                    v.setTop(old[0]);
                    v.setBottom(old[1]);
                    ViewCompat.setHasTransientState(v, false);
                } else {
                    /* If the cell is present in the ListView after the collapse but
                     * not before the collapse then the bounds are calculated using
                     * the bottom and top translation of the collapsing cell.*/
                    int delta = i > index ? yTranslateBottom : -yTranslateTop;
                    v.setTop(v.getTop() + delta);
                    v.setBottom(v.getBottom() + delta);
                }
            }

            final View expandingLayout = holder.expandingView;// mView.findViewById (R.id.expanding_layout); optimize

            /* Animates all the cells present on the screen after the collapse. */
            //   ArrayList <Animator> animations = new ArrayList<Animator>(); optimize
            for (int i = 0; i < childCount; i++) {
                View v = getChildAt(i);
                if (v != mView) {
                    float diff = i > index ? -yTranslateBottom : yTranslateTop;
                    animations.add(getAnimation(v, diff, diff));
                }
            }

            /* Adds animation for collapsing the cell that was clicked. */
            animations.add(getAnimation(mView, yTranslateTop, -yTranslateBottom));

            /* Adds an animation for fading out the extra content. */
            animations.add(ObjectAnimator.ofFloat(expandingLayout, View.ALPHA, 1, 0));

            /* Disabled the ListView for the duration of the animation.*/
            setEnabled(false);
            setClickable(false);

            /* Play all the animations created above together at the same time. */
            //   AnimatorSet s = new AnimatorSet();
            s.playTogether(animations);
            s.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    expandingLayout.setVisibility(View.GONE);
                    mView.setLayoutParams(generalLayoutParams);
                    viewObject.mIsExpanded = false; //     viewObject.setExpanded(false);
                    s.removeAllListeners();
                    animations.clear();
                    oldCoordinates.clear();
                    setEnabled(true);
                    setClickable(true);
                    /* Note that alpha must be set back to 1 in case this view is reused
                     * by a cell that was expanded, but not yet collapsed, so its state
                     * should persist in an expanded state with the extra content visible.*/
                    expandingLayout.setAlpha(1);
                }
            });
            s.start();

            return true;
        }
    };
}

From source file:com.modprobe.profit.ExpandingListView.java

/**
 * This method collapses the view that was clicked and animates all the views
 * around it to close around the collapsing view. There are several steps required
 * to do this which are outlined below./*from www .ja v  a  2  s  . c o m*/
 *
 * 1. Update the layout parameters of the view clicked so as to minimize its height
 *    to the original collapsed (default) state.
 * 2. After invoking a layout, the listview will shift all the cells so as to display
 *    them most efficiently. Therefore, during the first predraw pass, the listview
 *    must be offset by some amount such that given the custom bound change upon
 *    collapse, all the cells that need to be on the screen after the layout
 *    are rendered by the listview.
 * 3. On the second predraw pass, all the items are first returned to their original
 *    location (before the first layout).
 * 4. The collapsing view's bounds are animated to what the final values should be.
 * 5. The bounds above the collapsing view are animated downwards while the bounds
 *    below the collapsing view are animated upwards.
 * 6. The extra text is faded out as its contents become visible throughout the
 *    animation process.
 */

private void collapseView(final View view) {
    final SuggestionExpandingListViewItem viewObject = (SuggestionExpandingListViewItem) getItemAtPosition(
            getPositionForView(view));

    /* Store the original top and bottom bounds of all the cells.*/
    final int oldTop = view.getTop();
    final int oldBottom = view.getBottom();

    final HashMap<View, int[]> oldCoordinates = new HashMap<View, int[]>();

    int childCount = getChildCount();
    for (int i = 0; i < childCount; i++) {
        View v = getChildAt(i);
        ViewCompat.setHasTransientState(v, true);
        oldCoordinates.put(v, new int[] { v.getTop(), v.getBottom() });
    }

    /* Update the layout so the extra content becomes invisible.*/
    view.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT,
            viewObject.getCollapsedHeight()));

    /* Add an onPreDraw listener. */
    final ViewTreeObserver observer = getViewTreeObserver();
    observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {

        @Override
        public boolean onPreDraw() {

            if (!mShouldRemoveObserver) {
                /*Same as for expandingView, the parameters for setSelectionFromTop must
                * be determined such that the necessary cells of the ListView are rendered
                * and added to it.*/
                mShouldRemoveObserver = true;

                int newTop = view.getTop();
                int newBottom = view.getBottom();

                int newHeight = newBottom - newTop;
                int oldHeight = oldBottom - oldTop;
                int deltaHeight = oldHeight - newHeight;

                mTranslate = getTopAndBottomTranslations(oldTop, oldBottom, deltaHeight, false);

                int currentTop = view.getTop();
                int futureTop = oldTop + mTranslate[0];

                int firstChildStartTop = getChildAt(0).getTop();
                int firstVisiblePosition = getFirstVisiblePosition();
                int deltaTop = currentTop - futureTop;

                int i;
                int childCount = getChildCount();
                for (i = 0; i < childCount; i++) {
                    View v = getChildAt(i);
                    int height = v.getBottom() - Math.max(0, v.getTop());
                    if (deltaTop - height > 0) {
                        firstVisiblePosition++;
                        deltaTop -= height;
                    } else {
                        break;
                    }
                }

                if (i > 0) {
                    firstChildStartTop = 0;
                }

                setSelectionFromTop(firstVisiblePosition, firstChildStartTop - deltaTop);

                requestLayout();

                return false;
            }

            mShouldRemoveObserver = false;
            observer.removeOnPreDrawListener(this);

            int yTranslateTop = mTranslate[0];
            int yTranslateBottom = mTranslate[1];

            int index = indexOfChild(view);
            int childCount = getChildCount();
            for (int i = 0; i < childCount; i++) {
                View v = getChildAt(i);
                int[] old = oldCoordinates.get(v);
                if (old != null) {
                    /* If the cell was present in the ListView before the collapse and
                    * after the collapse then the bounds are reset to their old values.*/
                    v.setTop(old[0]);
                    v.setBottom(old[1]);
                    ViewCompat.setHasTransientState(v, false);
                } else {
                    /* If the cell is present in the ListView after the collapse but
                     * not before the collapse then the bounds are calculated using
                     * the bottom and top translation of the collapsing cell.*/
                    int delta = i > index ? yTranslateBottom : -yTranslateTop;
                    v.setTop(v.getTop() + delta);
                    v.setBottom(v.getBottom() + delta);
                }
            }

            final View expandingLayout = view.findViewById(R.id.expanding_layout);

            /* Animates all the cells present on the screen after the collapse. */
            ArrayList<Animator> animations = new ArrayList<Animator>();
            for (int i = 0; i < childCount; i++) {
                View v = getChildAt(i);
                if (v != view) {
                    float diff = i > index ? -yTranslateBottom : yTranslateTop;
                    animations.add(getAnimation(v, diff, diff));
                }
            }

            /* Adds animation for collapsing the cell that was clicked. */
            animations.add(getAnimation(view, yTranslateTop, -yTranslateBottom));

            /* Adds an animation for fading out the extra content. */
            animations.add(ObjectAnimator.ofFloat(expandingLayout, View.ALPHA, 1, 0));

            /* Disabled the ListView for the duration of the animation.*/
            setEnabled(false);
            setClickable(false);

            /* Play all the animations created above together at the same time. */
            AnimatorSet s = new AnimatorSet();
            s.playTogether(animations);
            s.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    expandingLayout.setVisibility(View.GONE);
                    view.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT,
                            AbsListView.LayoutParams.WRAP_CONTENT));
                    viewObject.setExpanded(false);
                    setEnabled(true);
                    setClickable(true);
                    /* Note that alpha must be set back to 1 in case this view is reused
                    * by a cell that was expanded, but not yet collapsed, so its state
                    * should persist in an expanded state with the extra content visible.*/
                    expandingLayout.setAlpha(1);
                }
            });
            s.start();

            return true;
        }
    });
}

From source file:com.shoshin.paidpay.ExpandingListView.java

/**
 * This method collapses the view that was clicked and animates all the views
 * around it to close around the collapsing view. There are several steps required
 * to do this which are outlined below.//from  w w w . j ava 2  s.c o  m
 *
 * 1. Update the layout parameters of the view clicked so as to minimize its height
 *    to the original collapsed (default) state.
 * 2. After invoking a layout, the listview will shift all the cells so as to display
 *    them most efficiently. Therefore, during the first predraw pass, the listview
 *    must be offset by some amount such that given the custom bound change upon
 *    collapse, all the cells that need to be on the screen after the layout
 *    are rendered by the listview.
 * 3. On the second predraw pass, all the items are first returned to their original
 *    location (before the first layout).
 * 4. The collapsing view's bounds are animated to what the final values should be.
 * 5. The bounds above the collapsing view are animated downwards while the bounds
 *    below the collapsing view are animated upwards.
 * 6. The extra text is faded out as its contents become visible throughout the
 *    animation process.
 */

private void collapseView(final View view) {
    final ExpandableListItem viewObject = (ExpandableListItem) getItemAtPosition(getPositionForView(view));

    /* Store the original top and bottom bounds of all the cells.*/
    final int oldTop = view.getTop();
    final int oldBottom = view.getBottom();

    final HashMap<View, int[]> oldCoordinates = new HashMap<View, int[]>();

    int childCount = getChildCount();
    for (int i = 0; i < childCount; i++) {
        View v = getChildAt(i);
        ViewCompat.setHasTransientState(v, true);
        oldCoordinates.put(v, new int[] { v.getTop(), v.getBottom() });
    }

    /* Update the layout so the extra content becomes invisible.*/
    view.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT,
            viewObject.getCollapsedHeight()));

    /* Add an onPreDraw listener. */
    final ViewTreeObserver observer = getViewTreeObserver();
    observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {

        @Override
        public boolean onPreDraw() {

            if (!mShouldRemoveObserver) {
                /*Same as for expandingView, the parameters for setSelectionFromTop must
                * be determined such that the necessary cells of the ListView are rendered
                * and added to it.*/
                mShouldRemoveObserver = true;

                int newTop = view.getTop();
                int newBottom = view.getBottom();

                int newHeight = newBottom - newTop;
                int oldHeight = oldBottom - oldTop;
                int deltaHeight = oldHeight - newHeight;

                mTranslate = getTopAndBottomTranslations(oldTop, oldBottom, deltaHeight, false);

                int currentTop = view.getTop();
                int futureTop = oldTop + mTranslate[0];

                int firstChildStartTop = getChildAt(0).getTop();
                int firstVisiblePosition = getFirstVisiblePosition();
                int deltaTop = currentTop - futureTop;

                int i;
                int childCount = getChildCount();
                for (i = 0; i < childCount; i++) {
                    View v = getChildAt(i);
                    int height = v.getBottom() - Math.max(0, v.getTop());
                    if (deltaTop - height > 0) {
                        firstVisiblePosition++;
                        deltaTop -= height;
                    } else {
                        break;
                    }
                }

                if (i > 0) {
                    firstChildStartTop = 0;
                }

                setSelectionFromTop(firstVisiblePosition, firstChildStartTop - deltaTop);

                requestLayout();

                return false;
            }

            mShouldRemoveObserver = false;
            observer.removeOnPreDrawListener(this);

            int yTranslateTop = mTranslate[0];
            int yTranslateBottom = mTranslate[1];

            int index = indexOfChild(view);
            int childCount = getChildCount();
            for (int i = 0; i < childCount; i++) {
                View v = getChildAt(i);
                int[] old = oldCoordinates.get(v);
                if (old != null) {
                    /* If the cell was present in the ListView before the collapse and
                    * after the collapse then the bounds are reset to their old values.*/
                    v.setTop(old[0]);
                    v.setBottom(old[1]);
                    ViewCompat.setHasTransientState(v, false);
                } else {
                    /* If the cell is present in the ListView after the collapse but
                     * not before the collapse then the bounds are calculated using
                     * the bottom and top translation of the collapsing cell.*/
                    int delta = i > index ? yTranslateBottom : -yTranslateTop;
                    v.setTop(v.getTop() + delta);
                    v.setBottom(v.getBottom() + delta);
                }
            }

            final View expandingLayout = view.findViewById(R.id.expanding_layout);

            /* Animates all the cells present on the screen after the collapse. */
            ArrayList<Animator> animations = new ArrayList<Animator>();
            for (int i = 0; i < childCount; i++) {
                View v = getChildAt(i);
                if (v != view) {
                    float diff = i > index ? -yTranslateBottom : yTranslateTop;
                    animations.add(getAnimation(v, diff, diff));
                }
            }

            /* Adds animation for collapsing the cell that was clicked. */
            animations.add(getAnimation(view, yTranslateTop, -yTranslateBottom));

            /* Adds an animation for fading out the extra content. */
            //TODO
            animations.add(ObjectAnimator.ofFloat(expandingLayout, View.ALPHA, 1, 0));

            /* Disabled the ListView for the duration of the animation.*/
            setEnabled(false);
            setClickable(false);

            /* Play all the animations created above together at the same time. */
            AnimatorSet s = new AnimatorSet();
            s.playTogether(animations);
            s.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    expandingLayout.setVisibility(View.GONE);
                    view.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT,
                            AbsListView.LayoutParams.WRAP_CONTENT));
                    viewObject.setExpanded(false);
                    setEnabled(true);
                    setClickable(true);
                    /* Note that alpha must be set back to 1 in case this view is reused
                    * by a cell that was expanded, but not yet collapsed, so its state
                    * should persist in an expanded state with the extra content visible.*/
                    expandingLayout.setAlpha(1);
                    invalidateViews();
                }
            });
            s.start();

            return true;
        }
    });
}

From source file:com.shoshin.paidpay.ExpandingListViewPayVia.java

/**
 * This method collapses the view that was clicked and animates all the views
 * around it to close around the collapsing view. There are several steps required
 * to do this which are outlined below.// ww w  . j av  a2  s.  c  o  m
 *
 * 1. Update the layout parameters of the view clicked so as to minimize its height
 *    to the original collapsed (default) state.
 * 2. After invoking a layout, the listview will shift all the cells so as to display
 *    them most efficiently. Therefore, during the first predraw pass, the listview
 *    must be offset by some amount such that given the custom bound change upon
 *    collapse, all the cells that need to be on the screen after the layout
 *    are rendered by the listview.
 * 3. On the second predraw pass, all the items are first returned to their original
 *    location (before the first layout).
 * 4. The collapsing view's bounds are animated to what the final values should be.
 * 5. The bounds above the collapsing view are animated downwards while the bounds
 *    below the collapsing view are animated upwards.
 * 6. The extra text is faded out as its contents become visible throughout the
 *    animation process.
 */

private void collapseView(final View view) {
    final ExpandableCardsWithOffers viewObject = (ExpandableCardsWithOffers) getItemAtPosition(
            getPositionForView(view));

    /* Store the original top and bottom bounds of all the cells.*/
    final int oldTop = view.getTop();
    final int oldBottom = view.getBottom();

    final HashMap<View, int[]> oldCoordinates = new HashMap<View, int[]>();

    int childCount = getChildCount();
    for (int i = 0; i < childCount; i++) {
        View v = getChildAt(i);
        ViewCompat.setHasTransientState(v, true);
        oldCoordinates.put(v, new int[] { v.getTop(), v.getBottom() });
    }

    /* Update the layout so the extra content becomes invisible.*/
    view.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT,
            viewObject.getCollapsedHeight()));

    /* Add an onPreDraw listener. */
    final ViewTreeObserver observer = getViewTreeObserver();
    observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {

        @Override
        public boolean onPreDraw() {

            if (!mShouldRemoveObserver) {
                /*Same as for expandingView, the parameters for setSelectionFromTop must
                * be determined such that the necessary cells of the ListView are rendered
                * and added to it.*/
                mShouldRemoveObserver = true;

                int newTop = view.getTop();
                int newBottom = view.getBottom();

                int newHeight = newBottom - newTop;
                int oldHeight = oldBottom - oldTop;
                int deltaHeight = oldHeight - newHeight;

                mTranslate = getTopAndBottomTranslations(oldTop, oldBottom, deltaHeight, false);

                int currentTop = view.getTop();
                int futureTop = oldTop + mTranslate[0];

                int firstChildStartTop = getChildAt(0).getTop();
                int firstVisiblePosition = getFirstVisiblePosition();
                int deltaTop = currentTop - futureTop;

                int i;
                int childCount = getChildCount();
                for (i = 0; i < childCount; i++) {
                    View v = getChildAt(i);
                    int height = v.getBottom() - Math.max(0, v.getTop());
                    if (deltaTop - height > 0) {
                        firstVisiblePosition++;
                        deltaTop -= height;
                    } else {
                        break;
                    }
                }

                if (i > 0) {
                    firstChildStartTop = 0;
                }

                setSelectionFromTop(firstVisiblePosition, firstChildStartTop - deltaTop);

                requestLayout();

                return false;
            }

            mShouldRemoveObserver = false;
            observer.removeOnPreDrawListener(this);

            int yTranslateTop = mTranslate[0];
            int yTranslateBottom = mTranslate[1];

            int index = indexOfChild(view);
            int childCount = getChildCount();
            for (int i = 0; i < childCount; i++) {
                View v = getChildAt(i);
                int[] old = oldCoordinates.get(v);
                if (old != null) {
                    /* If the cell was present in the ListView before the collapse and
                    * after the collapse then the bounds are reset to their old values.*/
                    v.setTop(old[0]);
                    v.setBottom(old[1]);
                    ViewCompat.setHasTransientState(v, false);
                } else {
                    /* If the cell is present in the ListView after the collapse but
                     * not before the collapse then the bounds are calculated using
                     * the bottom and top translation of the collapsing cell.*/
                    int delta = i > index ? yTranslateBottom : -yTranslateTop;
                    v.setTop(v.getTop() + delta);
                    v.setBottom(v.getBottom() + delta);
                }
            }

            final View expandingLayout = view.findViewById(R.id.expanding_layout);

            /* Animates all the cells present on the screen after the collapse. */
            ArrayList<Animator> animations = new ArrayList<Animator>();
            for (int i = 0; i < childCount; i++) {
                View v = getChildAt(i);
                if (v != view) {
                    float diff = i > index ? -yTranslateBottom : yTranslateTop;
                    animations.add(getAnimation(v, diff, diff));
                }
            }

            /* Adds animation for collapsing the cell that was clicked. */
            animations.add(getAnimation(view, yTranslateTop, -yTranslateBottom));

            /* Adds an animation for fading out the extra content. */
            //TODO
            animations.add(ObjectAnimator.ofFloat(expandingLayout, View.ALPHA, 1, 0));

            /* Disabled the ListView for the duration of the animation.*/
            setEnabled(false);
            setClickable(false);

            /* Play all the animations created above together at the same time. */
            AnimatorSet s = new AnimatorSet();
            s.playTogether(animations);
            s.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    expandingLayout.setVisibility(View.GONE);
                    view.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT,
                            AbsListView.LayoutParams.WRAP_CONTENT));
                    viewObject.setExpanded(false);
                    setEnabled(true);
                    setClickable(true);
                    /* Note that alpha must be set back to 1 in case this view is reused
                    * by a cell that was expanded, but not yet collapsed, so its state
                    * should persist in an expanded state with the extra content visible.*/
                    expandingLayout.setAlpha(1);
                    invalidateViews();
                }
            });
            s.start();

            return true;
        }
    });
}