Example usage for android.animation AnimatorSet addListener

List of usage examples for android.animation AnimatorSet addListener

Introduction

In this page you can find the example usage for android.animation AnimatorSet addListener.

Prototype

public void addListener(AnimatorListener listener) 

Source Link

Document

Adds a listener to the set of listeners that are sent events through the life of an animation, such as start, repeat, and end.

Usage

From source file:com.grepsound.activities.MainActivity.java

/**
 * This method animates the image fragment into the foreground by both
 * scaling and rotating the fragment's view, while also removing the
 * previously added translucent dark hover view. Upon the completion of
 * this animation, the image fragment regains focus since this method is
 * called from the onBackStackChanged method.
 *//*from  w w  w .  ja v a  2 s  .c  o  m*/
public void slideForward(Animator.AnimatorListener listener) {
    View movingFragmentView = mMainFrag.getView();

    PropertyValuesHolder rotateX = PropertyValuesHolder.ofFloat("rotationX", 40f);
    PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
    PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
    ObjectAnimator movingFragmentAnimator = ObjectAnimator.ofPropertyValuesHolder(movingFragmentView, rotateX,
            scaleX, scaleY);

    ObjectAnimator darkHoverViewAnimator = ObjectAnimator.ofFloat(mDarkHoverView, "alpha", 0.5f, 0.0f);

    ObjectAnimator movingFragmentRotator = ObjectAnimator.ofFloat(movingFragmentView, "rotationX", 0);
    movingFragmentRotator.setStartDelay(getResources().getInteger(R.integer.half_slide_up_down_duration));

    AnimatorSet s = new AnimatorSet();
    s.playTogether(movingFragmentAnimator, movingFragmentRotator, darkHoverViewAnimator);
    s.setStartDelay(getResources().getInteger(R.integer.slide_up_down_duration));
    s.addListener(new AnimatorListenerAdapter() {
        @Override
        public void onAnimationEnd(Animator animation) {
            mIsAnimating = false;
        }
    });
    s.start();
}

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  w  w w.j a  v a 2  s.c om*/
 *
 * 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.modprobe.profit.ExpandingListView.java

/**
 * This method expands the view that was clicked and animates all the views
 * around it to make room for the expanding view. There are several steps required
 * to do this which are outlined below.//w  ww  .j av a 2s .  c om
 *
 * 1. Store the current top and bottom bounds of each visible item in the listview.
 * 2. Update the layout parameters of the selected view. In the context of this
 *    method, the view should be originally collapsed and set to some custom height.
 *    The layout parameters are updated so as to wrap the content of the additional
 *    text that is to be displayed.
 *
 * After invoking a layout to take place, the listview will order all the items
 * such that there is space for each view. This layout will be independent of what
 * the bounds of the items were prior to the layout so two pre-draw passes will
 * be made. This is necessary because after the layout takes place, some views that
 * were visible before the layout may now be off bounds but a reference to these
 * views is required so the animation completes as intended.
 *
 * 3. The first predraw pass will set the bounds of all the visible items to
 *    their original location before the layout took place and then force another
 *    layout. Since the bounds of the cells cannot be set directly, the method
 *    setSelectionFromTop can be used to achieve a very similar effect.
 * 4. The expanding view's bounds are animated to what the final values should be
 *    from the original bounds.
 * 5. The bounds above the expanding view are animated upwards while the bounds
 *    below the expanding view are animated downwards.
 * 6. The extra text is faded in as its contents become visible throughout the
 *    animation process.
 *
 * It is important to note that the listview is disabled during the animation
 * because the scrolling behaviour is unpredictable if the bounds of the items
 * within the listview are not constant during the scroll.
 */

private void expandView(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 visible.*/
    final View expandingLayout = view.findViewById(R.id.expanding_layout);
    expandingLayout.setVisibility(View.VISIBLE);

    /* Add an onPreDraw Listener to the listview. onPreDraw will get invoked after onLayout
    * and onMeasure have run but before anything has been drawn. This
    * means that the final post layout properties for all the items have already been
    * determined, but still have not been rendered onto the screen.*/
    final ViewTreeObserver observer = getViewTreeObserver();
    observer.addOnPreDrawListener(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 = view.getTop();
                int newBottom = view.getBottom();

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

                mTranslate = getTopAndBottomTranslations(oldTop, oldBottom, delta, true);

                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);

                /* 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>();

            int index = indexOfChild(view);

            /* 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 != view) {
                        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(view, -yTranslateTop, yTranslateBottom));

            /* Adds an animation for fading in the extra content. */
            animations.add(ObjectAnimator.ofFloat(view.findViewById(R.id.expanding_layout), 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();
            s.playTogether(animations);
            s.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    viewObject.setExpanded(true);
                    setEnabled(true);
                    setClickable(true);
                    if (mViewsToDraw.size() > 0) {
                        for (View v : mViewsToDraw) {
                            ViewCompat.setHasTransientState(v, false);
                        }
                    }
                    mViewsToDraw.clear();
                }
            });
            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   www  . j a  va  2  s  .  c om*/
 *
 * 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.ExpandingListView.java

/**
 * This method expands the view that was clicked and animates all the views
 * around it to make room for the expanding view. There are several steps required
 * to do this which are outlined below./*from w w w.  j a v a 2  s .  com*/
 *
 * 1. Store the current top and bottom bounds of each visible item in the listview.
 * 2. Update the layout parameters of the selected view. In the context of this
 *    method, the view should be originally collapsed and set to some custom height.
 *    The layout parameters are updated so as to wrap the content of the additional
 *    text that is to be displayed.
 *
 * After invoking a layout to take place, the listview will order all the items
 * such that there is space for each view. This layout will be independent of what
 * the bounds of the items were prior to the layout so two pre-draw passes will
 * be made. This is necessary because after the layout takes place, some views that
 * were visible before the layout may now be off bounds but a reference to these
 * views is required so the animation completes as intended.
 *
 * 3. The first predraw pass will set the bounds of all the visible items to
 *    their original location before the layout took place and then force another
 *    layout. Since the bounds of the cells cannot be set directly, the method
 *    setSelectionFromTop can be used to achieve a very similar effect.
 * 4. The expanding view's bounds are animated to what the final values should be
 *    from the original bounds.
 * 5. The bounds above the expanding view are animated upwards while the bounds
 *    below the expanding view are animated downwards.
 * 6. The extra text is faded in as its contents become visible throughout the
 *    animation process.
 *
 * It is important to note that the listview is disabled during the animation
 * because the scrolling behaviour is unpredictable if the bounds of the items
 * within the listview are not constant during the scroll.
 */

private void expandView(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 visible.*/
    final View expandingLayout = view.findViewById(R.id.expanding_layout);
    expandingLayout.setVisibility(View.VISIBLE);

    /* Add an onPreDraw Listener to the listview. onPreDraw will get invoked after onLayout
    * and onMeasure have run but before anything has been drawn. This
    * means that the final post layout properties for all the items have already been
    * determined, but still have not been rendered onto the screen.*/
    final ViewTreeObserver observer = getViewTreeObserver();
    observer.addOnPreDrawListener(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 = view.getTop();
                int newBottom = view.getBottom();

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

                mTranslate = getTopAndBottomTranslations(oldTop, oldBottom, delta, true);

                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);

                /* 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>();

            int index = indexOfChild(view);

            /* 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 != view) {
                        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(view, -yTranslateTop, yTranslateBottom));

            /* Adds an animation for fading in the extra content. */
            //TODO
            animations.add(ObjectAnimator.ofFloat(view.findViewById(R.id.expanding_layout), 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();
            s.playTogether(animations);
            s.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    viewObject.setExpanded(true);
                    setEnabled(true);
                    setClickable(true);
                    if (mViewsToDraw.size() > 0) {
                        for (View v : mViewsToDraw) {
                            ViewCompat.setHasTransientState(v, false);
                        }
                    }
                    mViewsToDraw.clear();
                }
            });
            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.//from  w  w  w . j  av  a2  s .c  om
 *
 * 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;
        }
    });
}

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

/**
 * This method expands the view that was clicked and animates all the views
 * around it to make room for the expanding view. There are several steps required
 * to do this which are outlined below./*from w  ww .  j  av a 2  s  . c o  m*/
 *
 * 1. Store the current top and bottom bounds of each visible item in the listview.
 * 2. Update the layout parameters of the selected view. In the context of this
 *    method, the view should be originally collapsed and set to some custom height.
 *    The layout parameters are updated so as to wrap the content of the additional
 *    text that is to be displayed.
 *
 * After invoking a layout to take place, the listview will order all the items
 * such that there is space for each view. This layout will be independent of what
 * the bounds of the items were prior to the layout so two pre-draw passes will
 * be made. This is necessary because after the layout takes place, some views that
 * were visible before the layout may now be off bounds but a reference to these
 * views is required so the animation completes as intended.
 *
 * 3. The first predraw pass will set the bounds of all the visible items to
 *    their original location before the layout took place and then force another
 *    layout. Since the bounds of the cells cannot be set directly, the method
 *    setSelectionFromTop can be used to achieve a very similar effect.
 * 4. The expanding view's bounds are animated to what the final values should be
 *    from the original bounds.
 * 5. The bounds above the expanding view are animated upwards while the bounds
 *    below the expanding view are animated downwards.
 * 6. The extra text is faded in as its contents become visible throughout the
 *    animation process.
 *
 * It is important to note that the listview is disabled during the animation
 * because the scrolling behaviour is unpredictable if the bounds of the items
 * within the listview are not constant during the scroll.
 */

private void expandView(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 visible.*/
    final View expandingLayout = view.findViewById(R.id.expanding_layout);
    expandingLayout.setVisibility(View.VISIBLE);

    /* Add an onPreDraw Listener to the listview. onPreDraw will get invoked after onLayout
    * and onMeasure have run but before anything has been drawn. This
    * means that the final post layout properties for all the items have already been
    * determined, but still have not been rendered onto the screen.*/
    final ViewTreeObserver observer = getViewTreeObserver();
    observer.addOnPreDrawListener(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 = view.getTop();
                int newBottom = view.getBottom();

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

                mTranslate = getTopAndBottomTranslations(oldTop, oldBottom, delta, true);

                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);

                /* 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>();

            int index = indexOfChild(view);

            /* 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 != view) {
                        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(view, -yTranslateTop, yTranslateBottom));

            /* Adds an animation for fading in the extra content. */
            //TODO
            animations.add(ObjectAnimator.ofFloat(view.findViewById(R.id.expanding_layout), 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();
            s.playTogether(animations);
            s.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    viewObject.setExpanded(true);
                    setEnabled(true);
                    setClickable(true);
                    if (mViewsToDraw.size() > 0) {
                        for (View v : mViewsToDraw) {
                            ViewCompat.setHasTransientState(v, false);
                        }
                    }
                    mViewsToDraw.clear();
                }
            });
            s.start();
            return true;
        }
    });
}

From source file:com.fada21.android.hydralist.expandable.ExpandingListViewDelegate.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  ww w .  j  av  a 2 s . c o  m*/
 * <li>Update the layout parameters of the view clicked so as to minimize its height to the original collapsed (default) state.</li> <li>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.</li> <li>On the second predraw pass, all the items are first returned to their original location (before the first layout).</li> <li>The
 * collapsing view's bounds are animated to what the final values should be.</li> <li>The bounds above the collapsing view are animated downwards while the
 * bounds below the collapsing view are animated upwards.</li> <li>The extra text is faded out as its contents become visible throughout the animation
 * process.</li>
 * 
 * @param view
 *            collapsing view
 * @param position
 *            item position in list
 * @param id
 *            item id
 */
private void collapseView(final View view, int position, long id) {
    final ExpandableListItem item = (ExpandableListItem) nlv.getItemAtPosition(nlv.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 = nlv.getChildCount();
    for (int i = 0; i < childCount; i++) {
        View v = nlv.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, item.getCollapsedHeight()));

    /* Add an onPreDraw listener. */
    final ViewTreeObserver observer = nlv.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 = nlv.getChildAt(0).getTop();
                int firstVisiblePosition = nlv.getFirstVisiblePosition();
                int deltaTop = currentTop - futureTop;

                int i;
                int childCount = nlv.getChildCount();
                for (i = 0; i < childCount; i++) {
                    View v = nlv.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;
                }

                nlv.setSelectionFromTop(firstVisiblePosition, firstChildStartTop - deltaTop);

                nlv.requestLayout();

                return false;
            }

            mShouldRemoveObserver = false;
            observer.removeOnPreDrawListener(this);

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

            int index = nlv.indexOfChild(view);
            int childCount = nlv.getChildCount();
            for (int i = 0; i < childCount; i++) {
                View v = nlv.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(getExpandingAdapter().getExpandingHelper().getExpandingLayout());

            /* 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 = nlv.getChildAt(i);
                if (v != view) {
                    float diff = i > index ? -yTranslateBottom : yTranslateTop;
                    if (diff != 0f)
                        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. */
            nlv.setEnabled(false);
            nlv.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));
                    item.setExpanded(false);
                    nlv.setEnabled(true);
                    nlv.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.fada21.android.hydralist.expandable.ExpandingListViewDelegate.java

/**
 * <p>// ww  w  .  j  av a  2 s . c  o m
 * This method expands the view that was clicked and animates all the views around it to make room for the expanding view. There are several steps required
 * to do this which are outlined below.
 * </p>
 * 
 * <li>Store the current top and bottom bounds of each visible item in the listview.</li> <li>Update the layout parameters of the selected view. In the
 * context of this method, the view should be originally collapsed and set to some custom height. The layout parameters are updated so as to wrap the
 * content of the additional text that is to be displayed.</li>
 * 
 * <br/>
 * <br/>
 * <p>
 * After invoking a layout to take place, the listview will order all the items such that there is space for each view. This layout will be independent of
 * what the bounds of the items were prior to the layout so two pre-draw passes will be made. This is necessary because after the layout takes place, some
 * views that were visible before the layout may now be off bounds but a reference to these views is required so the animation completes as intended.
 * </p>
 * <br/>
 * 
 * <li>The first predraw pass will set the bounds of all the visible items to their original location before the layout took place and then force another
 * layout. Since the bounds of the cells cannot be set directly, the method setSelectionFromTop can be used to achieve a very similar effect.</li> <li>The
 * expanding view's bounds are animated to what the final values should be from the original bounds.</li> <li>The bounds above the expanding view are
 * animated upwards while the bounds below the expanding view are animated downwards.</li> <li>The extra text is faded in as its contents become visible
 * throughout the animation process.</li>
 * 
 * <br/>
 * <br/>
 * <p>
 * It is important to note that the listview is disabled during the animation because the scrolling behavior is unpredictable if the bounds of the items
 * within the listview are not constant during the scroll.
 * </p>
 * <br/>
 * 
 * @param view
 *            expanding view
 * @param position
 *            item position in list
 * @param id
 *            item id
 */
private void expandView(final View view, int position, long id) {
    // final ExpandableListItem item = (ExpandableListItem) nlv.getItemAtPosition(nlv.getPositionForView(view));
    HydraListAdapter<ExpandableListItem> expandingAdapter = getExpandingAdapter();
    final ExpandableListItem item = expandingAdapter.getDataProvider().get(position);
    ExpandingLayout expandingLayout = expandingAdapter.getExpandingHelper().getExpandedView(view, item);

    /* 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 = nlv.getChildCount();
    for (int i = 0; i < childCount; i++) {
        View v = nlv.getChildAt(i);
        ViewCompat.setHasTransientState(v, true);
        oldCoordinates.put(v, new int[] { v.getTop(), v.getBottom() });
    }

    /* Update the layout so the extra content becomes visible. */
    expandingLayout.setVisibility(View.VISIBLE);

    /*
     * Add an onPreDraw Listener to the listview. onPreDraw will get invoked after onLayout
     * and onMeasure have run but before anything has been drawn. This
     * means that the final post layout properties for all the items have already been
     * determined, but still have not been rendered onto the screen.
     */
    final ViewTreeObserver observer = nlv.getViewTreeObserver();
    observer.addOnPreDrawListener(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 = view.getTop();
                int newBottom = view.getBottom();

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

                mTranslate = getTopAndBottomTranslations(oldTop, oldBottom, delta, true);

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

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

                int i;
                int childCount = nlv.getChildCount();
                for (i = 0; i < childCount; i++) {
                    View v = nlv.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;
                }

                nlv.setSelectionFromTop(firstVisiblePosition, firstChildStartTop - deltaTop);

                /* Request another layout to update the layout parameters of the cells. */
                nlv.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>();

            int index = nlv.indexOfChild(view);

            /*
             * 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 = nlv.indexOfChild(v);
                    if (v != view) {
                        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(view, -yTranslateTop, yTranslateBottom));

            /* Adds an animation for fading in the extra content. */
            animations.add(ObjectAnimator.ofFloat(
                    view.findViewById(getExpandingAdapter().getExpandingHelper().getExpandingLayout()),
                    View.ALPHA, 0, 1));

            /* Disabled the ListView for the duration of the animation. */
            nlv.setEnabled(false);
            nlv.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) {
                    item.setExpanded(true);
                    nlv.setEnabled(true);
                    nlv.setClickable(true);
                    if (mViewsToDraw.size() > 0) {
                        for (View v : mViewsToDraw) {
                            ViewCompat.setHasTransientState(v, false);
                        }
                    }
                    mViewsToDraw.clear();
                }
            });
            s.start();
            return true;
        }
    });
}

From source file:com.hannesdorfmann.FeedAdapter.java

private void bindDesignerNewsStory(final Story story, final DesignerNewsStoryHolder holder) {
    holder.title.setText(story.title);/*from   ww  w  .j a va  2s  . c o m*/
    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            CustomTabActivityHelper.openCustomTab(host,
                    DesignerNewsStory.getCustomTabIntent(host, story, null).build(), Uri.parse(story.url));
        }
    });
    holder.comments.setText(String.valueOf(story.comment_count));
    holder.comments.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View commentsView) {
            final Intent intent = new Intent();
            intent.setClass(host, DesignerNewsStory.class);
            intent.putExtra(DesignerNewsStory.EXTRA_STORY, story);
            final ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(host,
                    Pair.create(holder.itemView, host.getString(R.string.transition_story_title_background)),
                    Pair.create(holder.itemView, host.getString(R.string.transition_story_background)));
            host.startActivity(intent, options.toBundle());
        }
    });
    if (pocketIsInstalled) {
        holder.pocket.setImageAlpha(178); // grumble... no xml setter, grumble...
        holder.pocket.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View view) {
                final ImageButton pocketButton = (ImageButton) view;
                // actually add to pocket
                PocketUtils.addToPocket(host, story.url);

                // setup for anim
                holder.itemView.setHasTransientState(true);
                ((ViewGroup) pocketButton.getParent().getParent()).setClipChildren(false);
                final int initialLeft = pocketButton.getLeft();
                final int initialTop = pocketButton.getTop();
                final int translatedLeft = (holder.itemView.getWidth() - pocketButton.getWidth()) / 2;
                final int translatedTop = initialTop
                        - ((holder.itemView.getHeight() - pocketButton.getHeight()) / 2);
                final ArcMotion arc = new ArcMotion();

                // animate the title & pocket icon up, scale the pocket icon up
                PropertyValuesHolder pvhTitleUp = PropertyValuesHolder.ofFloat(View.TRANSLATION_Y,
                        -(holder.itemView.getHeight() / 5));
                PropertyValuesHolder pvhTitleFade = PropertyValuesHolder.ofFloat(View.ALPHA, 0.54f);
                Animator titleMoveFadeOut = ObjectAnimator.ofPropertyValuesHolder(holder.title, pvhTitleUp,
                        pvhTitleFade);

                Animator pocketMoveUp = ObjectAnimator.ofFloat(pocketButton, View.TRANSLATION_X,
                        View.TRANSLATION_Y,
                        arc.getPath(initialLeft, initialTop, translatedLeft, translatedTop));
                PropertyValuesHolder pvhPocketScaleUpX = PropertyValuesHolder.ofFloat(View.SCALE_X, 3f);
                PropertyValuesHolder pvhPocketScaleUpY = PropertyValuesHolder.ofFloat(View.SCALE_Y, 3f);
                Animator pocketScaleUp = ObjectAnimator.ofPropertyValuesHolder(pocketButton, pvhPocketScaleUpX,
                        pvhPocketScaleUpY);
                ObjectAnimator pocketFadeUp = ObjectAnimator.ofInt(pocketButton, ViewUtils.IMAGE_ALPHA, 255);

                AnimatorSet up = new AnimatorSet();
                up.playTogether(titleMoveFadeOut, pocketMoveUp, pocketScaleUp, pocketFadeUp);
                up.setDuration(300);
                up.setInterpolator(
                        AnimationUtils.loadInterpolator(host, android.R.interpolator.fast_out_slow_in));

                // animate everything back into place
                PropertyValuesHolder pvhTitleMoveUp = PropertyValuesHolder.ofFloat(View.TRANSLATION_Y, 0f);
                PropertyValuesHolder pvhTitleFadeUp = PropertyValuesHolder.ofFloat(View.ALPHA, 1f);
                Animator titleMoveFadeIn = ObjectAnimator.ofPropertyValuesHolder(holder.title, pvhTitleMoveUp,
                        pvhTitleFadeUp);
                Animator pocketMoveDown = ObjectAnimator.ofFloat(pocketButton, View.TRANSLATION_X,
                        View.TRANSLATION_Y, arc.getPath(translatedLeft, translatedTop, 0, 0));
                PropertyValuesHolder pvhPocketScaleDownX = PropertyValuesHolder.ofFloat(View.SCALE_X, 1f);
                PropertyValuesHolder pvhPocketScaleDownY = PropertyValuesHolder.ofFloat(View.SCALE_Y, 1f);
                Animator pvhPocketScaleDown = ObjectAnimator.ofPropertyValuesHolder(pocketButton,
                        pvhPocketScaleDownX, pvhPocketScaleDownY);
                ObjectAnimator pocketFadeDown = ObjectAnimator.ofInt(pocketButton, ViewUtils.IMAGE_ALPHA, 138);

                AnimatorSet down = new AnimatorSet();
                down.playTogether(titleMoveFadeIn, pocketMoveDown, pvhPocketScaleDown, pocketFadeDown);
                down.setDuration(300);
                down.setInterpolator(
                        AnimationUtils.loadInterpolator(host, android.R.interpolator.fast_out_slow_in));
                down.setStartDelay(500);

                // play it
                AnimatorSet upDown = new AnimatorSet();
                upDown.playSequentially(up, down);

                // clean up
                upDown.addListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        ((ViewGroup) pocketButton.getParent().getParent()).setClipChildren(true);
                        holder.itemView.setHasTransientState(false);
                    }
                });
                upDown.start();
            }
        });
    }
}