Example usage for android.view ViewTreeObserver removeOnPreDrawListener

List of usage examples for android.view ViewTreeObserver removeOnPreDrawListener

Introduction

In this page you can find the example usage for android.view ViewTreeObserver removeOnPreDrawListener.

Prototype

public void removeOnPreDrawListener(OnPreDrawListener victim) 

Source Link

Document

Remove a previously installed pre-draw callback

Usage

From source file:com.example.android.expandingcells.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  2  s. co  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 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);
    if (expandingLayout == null) {
        throw new IllegalStateException("Layout must have a ExpandingLayout called 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 = getExpandingTopAndBottomTranslations(getPositionForView(view), oldTop, oldBottom,
                        delta);

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

                handleSetTopAndBottom(v, old[0], old[1]);

                if (v == view) {
                    continue;
                } else if (v.getParent() == null) {
                    mViewsToDraw.add(v);
                    int delta = old[0] < oldTop ? -yTranslateTop : yTranslateBottom;
                    animations.add(getAnimation(v, old[0], old[1], delta, delta));

                } else {
                    int i = indexOfChild(v);
                    if (v != view) {
                        int delta = i > index ? yTranslateBottom : -yTranslateTop;
                        animations.add(getAnimation(v, old[0], old[1], delta, delta));
                    }
                    ViewCompat.setHasTransientState(v, false);
                }
            }

            /* Adds animation for expanding the cell that was clicked. */
            int[] old = oldCoordinates.get(view);
            animations.add(getAnimation(view, old[0], old[1], -yTranslateTop, yTranslateBottom));

            /* Adds an animation for fading in the extra content. */
            animations.add(ObjectAnimator.ofFloat(view.findViewById(R.id.expanding_layout), "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();

                    if (mOnExpandingListener != null) {
                        mOnExpandingListener.onExpanded(view);
                    }
                }
            });
            s.start();
            return true;
        }
    });
}

From source file:com.example.mike.birdalarm.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.
 * <p/>/*  w ww .j  a  v a2 s . co  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.
 * <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/>
 * 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.
 * <p/>
 * 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.
 */

public void expandView(final View view) {

    final Alarm viewObject = (Alarm) getItemAtPosition(getPositionForView(view));

    //        final CardListItem viewObject = (CardListItem) 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<>();

    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.options_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<>();

            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.options_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.setExpandedState(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.jforce.chapelhillnextbus.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.
 * <p/>// w w  w .  j a  v a2  s. co  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.
 * <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/>
 * 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.
 * <p/>
 * 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. */
            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:graphic.expand_graphic.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.
 * <p/>//w  w  w  .  j  av  a2 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.
 * <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/>
 * 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.
 * <p/>
 * 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) {

    /* 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.android.launcher2.Launcher.java

public void onWindowVisibilityChanged(int visibility) {
    mVisible = visibility == View.VISIBLE;
    updateRunning();//www.j av  a2  s  .com
    // The following code used to be in onResume, but it turns out onResume is called when
    // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
    // is a more appropriate event to handle
    if (mVisible) {
        mAppsCustomizeTabHost.onWindowVisible();
        if (!mWorkspaceLoading) {
            final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
            // We want to let Launcher draw itself at least once before we force it to build
            // layers on all the workspace pages, so that transitioning to Launcher from other
            // apps is nice and speedy. Usually the first call to preDraw doesn't correspond to
            // a true draw so we wait until the second preDraw call to be safe
            observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
                public boolean onPreDraw() {
                    // We delay the layer building a bit in order to give
                    // other message processing a time to run.  In particular
                    // this avoids a delay in hiding the IME if it was
                    // currently shown, because doing that may involve
                    // some communication back with the app.
                    mWorkspace.postDelayed(mBuildLayersRunnable, 500);

                    observer.removeOnPreDrawListener(this);
                    return true;
                }
            });
        }
        // When Launcher comes back to foreground, a different Activity might be responsible for
        // the app market intent, so refresh the icon
        updateAppMarketIcon();
        clearTypedText();
    }
}