Example usage for android.view View isLayoutRequested

List of usage examples for android.view View isLayoutRequested

Introduction

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

Prototype

public boolean isLayoutRequested() 

Source Link

Document

Indicates whether or not this view's layout will be requested during the next hierarchy layout pass.

Usage

From source file:com.facebook.litho.LithoView.java

private static void performLayoutOnChildrenIfNecessary(ComponentHost host) {
    for (int i = 0, count = host.getChildCount(); i < count; i++) {
        final View child = host.getChildAt(i);

        if (child.isLayoutRequested()) {
            // The hosting view doesn't allow children to change sizes dynamically as
            // this would conflict with the component's own layout calculations.
            child.measure(MeasureSpec.makeMeasureSpec(child.getWidth(), MeasureSpec.EXACTLY),
                    MeasureSpec.makeMeasureSpec(child.getHeight(), MeasureSpec.EXACTLY));
            child.layout(child.getLeft(), child.getTop(), child.getRight(), child.getBottom());
        }// w w w.j a v a2s .co m

        if (child instanceof ComponentHost) {
            performLayoutOnChildrenIfNecessary((ComponentHost) child);
        }
    }
}

From source file:android.widget.PinnedHeaderListView.java

private void ensurePinnedHeaderLayout(final int viewIndex) {
    final View view = mHeaders[viewIndex].view;
    if (view.isLayoutRequested()) {
        final ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
        int widthSpec;
        int heightSpec;

        if (layoutParams != null && layoutParams.width > 0)
            widthSpec = View.MeasureSpec.makeMeasureSpec(layoutParams.width, View.MeasureSpec.EXACTLY);
        else// w  w w .  j  a v  a2 s  .  com
            widthSpec = View.MeasureSpec.makeMeasureSpec(mHeaderWidth, View.MeasureSpec.EXACTLY);

        if (layoutParams != null && layoutParams.height > 0)
            heightSpec = View.MeasureSpec.makeMeasureSpec(layoutParams.height, View.MeasureSpec.EXACTLY);
        else
            heightSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        view.measure(widthSpec, heightSpec);
        final int height = view.getMeasuredHeight();
        mHeaders[viewIndex].height = height;
        view.layout(0, 0, view.getMeasuredWidth(), height);
    }
}

From source file:com.aizou.core.widget.pagerIndicator.indicator.FixedIndicatorView.java

private void measureScrollBar(boolean needChange) {
    if (scrollBar == null)
        return;//from www .  j a v a2  s  .  c o  m
    View view = scrollBar.getSlideView();
    if (view.isLayoutRequested() || needChange) {
        if (mAdapter != null && mAdapter.getCount() > 0 && mSelectedTabIndex >= 0
                && mSelectedTabIndex < mAdapter.getCount()) {
            int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), mWidthMode);
            int heightSpec;
            ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
            if (layoutParams != null && layoutParams.height > 0) {
                heightSpec = MeasureSpec.makeMeasureSpec(layoutParams.height, MeasureSpec.EXACTLY);
            } else {
                heightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
            }
            view.measure(widthSpec, heightSpec);
            View curr = getChildAt(mSelectedTabIndex);
            view.layout(0, 0, scrollBar.getWidth(curr.getMeasuredWidth()), scrollBar.getHeight(getHeight()));
        }
    }
}

From source file:cn.ieclipse.af.view.StaggeredGridView.java

/**
 * Measure and layout all currently visible children.
 *
 * @param queryAdapter true to requery the adapter for view data
 *//*from  ww  w .  ja va 2  s . co m*/
final void layoutChildren(boolean queryAdapter) {
    final int paddingLeft = getPaddingLeft();
    final int paddingRight = getPaddingRight();
    final int itemMargin = mItemMargin;
    final int colWidth = (getWidth() - paddingLeft - paddingRight - itemMargin * (mColCount - 1)) / mColCount;
    int rebuildLayoutRecordsBefore = -1;
    int rebuildLayoutRecordsAfter = -1;

    Arrays.fill(mItemBottoms, Integer.MIN_VALUE);

    final int childCount = getChildCount();
    for (int i = 0; i < childCount; i++) {
        View child = getChildAt(i);
        LayoutParams lp = (LayoutParams) child.getLayoutParams();
        final int col = lp.column;
        final int position = mFirstPosition + i;
        final boolean needsLayout = queryAdapter || child.isLayoutRequested();

        if (queryAdapter) {
            View newView = obtainView(position, child);
            if (newView != child) {
                removeViewAt(i);
                addView(newView, i);
                child = newView;
            }
            lp = (LayoutParams) child.getLayoutParams(); // Might have changed
        }

        final int span = Math.min(mColCount, lp.span);
        final int widthSize = colWidth * span + itemMargin * (span - 1);

        if (needsLayout) {
            final int widthSpec = MeasureSpec.makeMeasureSpec(widthSize, MeasureSpec.EXACTLY);

            final int heightSpec;
            if (lp.height == LayoutParams.WRAP_CONTENT) {
                heightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
            } else {
                heightSpec = MeasureSpec.makeMeasureSpec(lp.height, MeasureSpec.EXACTLY);
            }

            child.measure(widthSpec, heightSpec);
        }

        int childTop = mItemBottoms[col] > Integer.MIN_VALUE ? mItemBottoms[col] + mItemMargin : child.getTop();
        if (span > 1) {
            int lowest = childTop;
            for (int j = col + 1; j < col + span; j++) {
                final int bottom = mItemBottoms[j] + mItemMargin;
                if (bottom > lowest) {
                    lowest = bottom;
                }
            }
            childTop = lowest;
        }
        final int childHeight = child.getMeasuredHeight();
        final int childBottom = childTop + childHeight;
        final int childLeft = paddingLeft + col * (colWidth + itemMargin);
        final int childRight = childLeft + child.getMeasuredWidth();
        child.layout(childLeft, childTop, childRight, childBottom);

        for (int j = col; j < col + span; j++) {
            mItemBottoms[j] = childBottom;
        }

        final LayoutRecord rec = mLayoutRecords.get(position);
        if (rec != null && rec.height != childHeight) {
            // Invalidate our layout records for everything before this.
            rec.height = childHeight;
            rebuildLayoutRecordsBefore = position;
        }

        if (rec != null && rec.span != span) {
            // Invalidate our layout records for everything after this.
            rec.span = span;
            rebuildLayoutRecordsAfter = position;
        }
    }

    // Update mItemBottoms for any empty columns
    for (int i = 0; i < mColCount; i++) {
        if (mItemBottoms[i] == Integer.MIN_VALUE) {
            mItemBottoms[i] = mItemTops[i];
        }
    }

    if (rebuildLayoutRecordsBefore >= 0 || rebuildLayoutRecordsAfter >= 0) {
        if (rebuildLayoutRecordsBefore >= 0) {
            invalidateLayoutRecordsBeforePosition(rebuildLayoutRecordsBefore);
        }
        if (rebuildLayoutRecordsAfter >= 0) {
            invalidateLayoutRecordsAfterPosition(rebuildLayoutRecordsAfter);
        }
        for (int i = 0; i < childCount; i++) {
            final int position = mFirstPosition + i;
            final View child = getChildAt(i);
            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
            LayoutRecord rec = mLayoutRecords.get(position);
            if (rec == null) {
                rec = new LayoutRecord();
                mLayoutRecords.put(position, rec);
            }
            rec.column = lp.column;
            rec.height = child.getHeight();
            rec.id = lp.id;
            rec.span = Math.min(mColCount, lp.span);
        }
    }
}

From source file:com.irontec.jaigiro.widgets.StaggeredGridView.java

/**
 * Measure and layout all currently visible children.
 * /*from  w w  w  .j a  v  a 2 s .c  o  m*/
 * @param queryAdapter
 *            true to requery the adapter for view data
 */
final void layoutChildren(boolean queryAdapter) {
    final int paddingLeft = getPaddingLeft();
    final int paddingRight = getPaddingRight();
    final int itemMargin = mItemMargin;
    final int colWidth = (getWidth() - paddingLeft - paddingRight - itemMargin * (mColCount - 1)) / mColCount;
    int rebuildLayoutRecordsBefore = -1;
    int rebuildLayoutRecordsAfter = -1;

    Arrays.fill(mItemBottoms, Integer.MIN_VALUE);

    final int childCount = getChildCount();
    for (int i = 0; i < childCount; i++) {
        View child = getChildAt(i);
        LayoutParams lp = (LayoutParams) child.getLayoutParams();
        final int col = lp.column;
        final int position = mFirstPosition + i;
        final boolean needsLayout = queryAdapter || child.isLayoutRequested();

        if (queryAdapter) {
            View newView = obtainView(position, child);
            if (newView != child) {
                removeViewAt(i);
                addView(newView, i);
                child = newView;
            }
            lp = (LayoutParams) child.getLayoutParams(); // Might have
                                                         // changed
        }

        final int span = Math.min(mColCount, lp.span);
        final int widthSize = colWidth * span + itemMargin * (span - 1);

        if (needsLayout) {
            final int widthSpec = MeasureSpec.makeMeasureSpec(widthSize, MeasureSpec.EXACTLY);

            final int heightSpec;
            if (lp.height == LayoutParams.WRAP_CONTENT) {
                heightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
            } else {
                heightSpec = MeasureSpec.makeMeasureSpec(lp.height, MeasureSpec.EXACTLY);
            }

            child.measure(widthSpec, heightSpec);
        }

        int childTop = mItemBottoms[col] > Integer.MIN_VALUE ? mItemBottoms[col] + mItemMargin : child.getTop();
        if (span > 1) {
            int lowest = childTop;
            for (int j = col + 1; j < col + span; j++) {
                final int bottom = mItemBottoms[j] + mItemMargin;
                if (bottom > lowest) {
                    lowest = bottom;
                }
            }
            childTop = lowest;
        }
        final int childHeight = child.getMeasuredHeight();
        final int childBottom = childTop + childHeight;
        final int childLeft = paddingLeft + col * (colWidth + itemMargin);
        final int childRight = childLeft + child.getMeasuredWidth();
        child.layout(childLeft, childTop, childRight, childBottom);

        for (int j = col; j < col + span; j++) {
            mItemBottoms[j] = childBottom;
        }

        final LayoutRecord rec = mLayoutRecords.get(position);
        if (rec != null && rec.height != childHeight) {
            // Invalidate our layout records for everything before this.
            rec.height = childHeight;
            rebuildLayoutRecordsBefore = position;
        }

        if (rec != null && rec.span != span) {
            // Invalidate our layout records for everything after this.
            rec.span = span;
            rebuildLayoutRecordsAfter = position;
        }
    }

    // Update mItemBottoms for any empty columns
    for (int i = 0; i < mColCount; i++) {
        if (mItemBottoms[i] == Integer.MIN_VALUE) {
            mItemBottoms[i] = mItemTops[i];
        }
    }

    if (rebuildLayoutRecordsBefore >= 0 || rebuildLayoutRecordsAfter >= 0) {
        if (rebuildLayoutRecordsBefore >= 0) {
            invalidateLayoutRecordsBeforePosition(rebuildLayoutRecordsBefore);
        }
        if (rebuildLayoutRecordsAfter >= 0) {
            invalidateLayoutRecordsAfterPosition(rebuildLayoutRecordsAfter);
        }
        for (int i = 0; i < childCount; i++) {
            final int position = mFirstPosition + i;
            final View child = getChildAt(i);
            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
            LayoutRecord rec = mLayoutRecords.get(position);
            if (rec == null) {
                rec = new LayoutRecord();
                mLayoutRecords.put(position, rec);
            }
            rec.column = lp.column;
            rec.height = child.getHeight();
            rec.id = lp.id;
            rec.span = Math.min(mColCount, lp.span);
        }
    }
}

From source file:com.tandong.sa.view.StaggeredGridView.java

/**
 * Measure and layout all currently visible children.
 * //from ww w  . j av a  2  s.c om
 * @param queryAdapter
 *            true to requery the adapter for view data
 */
final void layoutChildren(boolean queryAdapter) {
    final int paddingLeft = getPaddingLeft();
    final int paddingRight = getPaddingRight();
    final int itemMargin = mItemMargin;
    final int colWidth = (getWidth() - paddingLeft - paddingRight - itemMargin * (mColCount - 1)) / mColCount;
    int rebuildLayoutRecordsBefore = -1;
    int rebuildLayoutRecordsAfter = -1;

    Arrays.fill(mItemBottoms, Integer.MIN_VALUE);

    final int childCount = getChildCount();
    for (int i = 0; i < childCount; i++) {
        View child = getChildAt(i);
        LayoutParams lp = (LayoutParams) child.getLayoutParams();
        final int col = lp.column;
        final int position = mFirstPosition + i;
        final boolean needsLayout = queryAdapter || child.isLayoutRequested();

        if (queryAdapter) {
            View newView = obtainView(position, child);
            if (newView != child) {
                removeViewAt(i);
                addView(newView, i);
                child = newView;
            }
            lp = (LayoutParams) child.getLayoutParams(); // Might have
            // changed
        }

        final int span = Math.min(mColCount, lp.span);
        final int widthSize = colWidth * span + itemMargin * (span - 1);

        if (needsLayout) {
            final int widthSpec = MeasureSpec.makeMeasureSpec(widthSize, MeasureSpec.EXACTLY);

            final int heightSpec;
            if (lp.height == LayoutParams.WRAP_CONTENT) {
                heightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
            } else {
                heightSpec = MeasureSpec.makeMeasureSpec(lp.height, MeasureSpec.EXACTLY);
            }

            child.measure(widthSpec, heightSpec);
        }

        int childTop = mItemBottoms[col] > Integer.MIN_VALUE ? mItemBottoms[col] + mItemMargin : child.getTop();
        if (span > 1) {
            int lowest = childTop;
            for (int j = col + 1; j < col + span; j++) {
                final int bottom = mItemBottoms[j] + mItemMargin;
                if (bottom > lowest) {
                    lowest = bottom;
                }
            }
            childTop = lowest;
        }
        final int childHeight = child.getMeasuredHeight();
        final int childBottom = childTop + childHeight;
        final int childLeft = paddingLeft + col * (colWidth + itemMargin);
        final int childRight = childLeft + child.getMeasuredWidth();
        child.layout(childLeft, childTop, childRight, childBottom);

        for (int j = col; j < col + span; j++) {
            mItemBottoms[j] = childBottom;
        }

        final LayoutRecord rec = mLayoutRecords.get(position);
        if (rec != null && rec.height != childHeight) {
            // Invalidate our layout records for everything before this.
            rec.height = childHeight;
            rebuildLayoutRecordsBefore = position;
        }

        if (rec != null && rec.span != span) {
            // Invalidate our layout records for everything after this.
            rec.span = span;
            rebuildLayoutRecordsAfter = position;
        }
    }

    // Update mItemBottoms for any empty columns
    for (int i = 0; i < mColCount; i++) {
        if (mItemBottoms[i] == Integer.MIN_VALUE) {
            mItemBottoms[i] = mItemTops[i];
        }
    }

    if (rebuildLayoutRecordsBefore >= 0 || rebuildLayoutRecordsAfter >= 0) {
        if (rebuildLayoutRecordsBefore >= 0) {
            invalidateLayoutRecordsBeforePosition(rebuildLayoutRecordsBefore);
        }
        if (rebuildLayoutRecordsAfter >= 0) {
            invalidateLayoutRecordsAfterPosition(rebuildLayoutRecordsAfter);
        }
        for (int i = 0; i < childCount; i++) {
            final int position = mFirstPosition + i;
            final View child = getChildAt(i);
            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
            LayoutRecord rec = mLayoutRecords.get(position);
            if (rec == null) {
                rec = new LayoutRecord();
                mLayoutRecords.put(position, rec);
            }
            rec.column = lp.column;
            rec.height = child.getHeight();
            rec.id = lp.id;
            rec.span = Math.min(mColCount, lp.span);
        }
    }
}

From source file:android.support.designox.widget.CoordinatorLayout.java

/**
 * Get the position rect for the given child. If the child has currently requested layout
 * or has a visibility of GONE./*from ww  w .j av a  2 s.  c  om*/
 *
 * @param child child view to check
 * @param transform true to include transformation in the output rect, false to
 *                        only account for the base position
 * @param out rect to set to the output values
 */
void getChildRect(View child, boolean transform, Rect out) {
    if (child.isLayoutRequested() || child.getVisibility() == View.GONE) {
        out.set(0, 0, 0, 0);
        return;
    }
    if (transform) {
        getDescendantRect(child, out);
    } else {
        out.set(child.getLeft(), child.getTop(), child.getRight(), child.getBottom());
    }
}

From source file:com.appunite.list.GridView.java

/**
 * Add a view as a child and make sure it is measured (if necessary) and
 * positioned properly.// w ww.java 2 s  . co  m
 *
 * @param child The view to add
 * @param position The position of the view
 * @param y The y position relative to which this view will be positioned
 * @param flow if true, align top edge to y. If false, align bottom edge
 *        to y.
 * @param childrenLeft Left edge where children should be positioned
 * @param selected Is this position selected?
 * @param recycled Has this view been pulled from the recycle bin? If so it
 *        does not need to be remeasured.
 * @param where Where to add the item in the list
 *
 */
private void setupChild(View child, int position, int y, boolean flow, int childrenLeft, boolean selected,
        boolean recycled, int where) {
    boolean isSelected = selected && shouldShowSelector();
    final boolean updateChildSelected = isSelected != child.isSelected();
    final int mode = mTouchMode;
    final boolean isPressed = mode > TOUCH_MODE_DOWN && mode < TOUCH_MODE_SCROLL && mMotionPosition == position;
    final boolean updateChildPressed = isPressed != child.isPressed();

    boolean needToMeasure = !recycled || updateChildSelected || child.isLayoutRequested();

    // Respect layout params that are already in the view. Otherwise make
    // some up...
    LayoutParams p = (LayoutParams) child.getLayoutParams();
    if (p == null) {
        p = (LayoutParams) generateDefaultLayoutParams();
    }
    p.viewType = mAdapter.getItemViewType(position);

    if (recycled && !p.forceAdd) {
        attachViewToParent(child, where, p);
    } else {
        p.forceAdd = false;
        addViewInLayout(child, where, p, true);
    }

    if (updateChildSelected) {
        child.setSelected(isSelected);
        if (isSelected) {
            requestFocus();
        }
    }

    if (updateChildPressed) {
        child.setPressed(isPressed);
    }

    if (mChoiceMode != CHOICE_MODE_NONE && mCheckStates != null) {
        if (child instanceof Checkable) {
            ((Checkable) child).setChecked(mCheckStates.get(position));
        } else if (getContext()
                .getApplicationInfo().targetSdkVersion >= android.os.Build.VERSION_CODES.HONEYCOMB) {
            Compat.setActivated(child, mCheckStates.get(position));
        }
    }

    if (needToMeasure) {
        int childHeightSpec = ViewGroup
                .getChildMeasureSpec(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), 0, p.height);

        int childWidthSpec = ViewGroup.getChildMeasureSpec(
                MeasureSpec.makeMeasureSpec(mColumnWidth, MeasureSpec.EXACTLY), 0, p.width);
        child.measure(childWidthSpec, childHeightSpec);
    } else {
        cleanupLayoutState(child);
    }

    final int w = child.getMeasuredWidth();
    final int h = child.getMeasuredHeight();

    int childLeft;
    final int childTop = flow ? y : y - h;

    final int layoutDirection = ViewCompat.getLayoutDirection(this);
    final int absoluteGravity = GravityCompat.getAbsoluteGravity(mGravity, layoutDirection);
    switch (absoluteGravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
    case Gravity.LEFT:
        childLeft = childrenLeft;
        break;
    case Gravity.CENTER_HORIZONTAL:
        childLeft = childrenLeft + ((mColumnWidth - w) / 2);
        break;
    case Gravity.RIGHT:
        childLeft = childrenLeft + mColumnWidth - w;
        break;
    default:
        childLeft = childrenLeft;
        break;
    }

    if (needToMeasure) {
        final int childRight = childLeft + w;
        final int childBottom = childTop + h;
        child.layout(childLeft, childTop, childRight, childBottom);
    } else {
        child.offsetLeftAndRight(childLeft - child.getLeft());
        child.offsetTopAndBottom(childTop - child.getTop());
    }

    if (mCachingStarted) {
        child.setDrawingCacheEnabled(true);
    }

    if (recycled && (((LayoutParams) child.getLayoutParams()).scrappedFromPosition) != position) {
        Compat.jumpDrawablesToCurrentState(child);
    }
}

From source file:com.example.uidemo.widget.waterfall.StaggeredGridView.java

/**
 * Measure and layout all currently visible children.
 *
 * @param queryAdapter true to requery the adapter for view data
 *///  w ww  .  j a  v  a2s . co m
final void layoutChildren(boolean queryAdapter) {
    final int paddingLeft = getPaddingLeft();
    final int paddingRight = getPaddingRight();
    final int itemMargin = mItemMargin;
    final int colWidth = (getWidth() - paddingLeft - paddingRight - itemMargin * (mColCount - 1)) / mColCount;
    mColWidth = colWidth;
    int rebuildLayoutRecordsBefore = -1;
    int rebuildLayoutRecordsAfter = -1;

    Arrays.fill(mItemBottoms, Integer.MIN_VALUE);

    final int childCount = getChildCount();
    int amountRemoved = 0;

    for (int i = 0; i < childCount; i++) {
        View child = getChildAt(i);
        LayoutParams lp = (LayoutParams) child.getLayoutParams();
        final int col = lp.column;
        final int position = mFirstPosition + i;
        final boolean needsLayout = queryAdapter || child.isLayoutRequested();

        if (queryAdapter) {

            View newView = obtainView(position, child);
            if (newView == null) {
                // child has been removed 
                removeViewAt(i);
                if (i - 1 >= 0)
                    invalidateLayoutRecordsAfterPosition(i - 1);
                amountRemoved++;
                continue;
            } else if (newView != child) {
                removeViewAt(i);
                addView(newView, i);
                child = newView;
            }
            lp = (LayoutParams) child.getLayoutParams(); // Might have changed
        }

        final int span = Math.min(mColCount, lp.span);
        final int widthSize = colWidth * span + itemMargin * (span - 1);

        if (needsLayout) {
            final int widthSpec = MeasureSpec.makeMeasureSpec(widthSize, MeasureSpec.EXACTLY);

            final int heightSpec;
            if (lp.height == LayoutParams.WRAP_CONTENT) {
                heightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
            } else {
                heightSpec = MeasureSpec.makeMeasureSpec(lp.height, MeasureSpec.EXACTLY);
            }

            child.measure(widthSpec, heightSpec);
        }

        int childTop = mItemBottoms[col] > Integer.MIN_VALUE ? mItemBottoms[col] + mItemMargin : child.getTop();

        if (span > 1) {
            int lowest = childTop;
            for (int j = col + 1; j < col + span; j++) {
                final int bottom = mItemBottoms[j] + mItemMargin;
                if (bottom > lowest) {
                    lowest = bottom;
                }
            }
            childTop = lowest;
        }
        final int childHeight = child.getMeasuredHeight();
        final int childBottom = childTop + childHeight;
        final int childLeft = paddingLeft + col * (colWidth + itemMargin);
        final int childRight = childLeft + child.getMeasuredWidth();
        child.layout(childLeft, childTop, childRight, childBottom);

        for (int j = col; j < col + span; j++) {
            mItemBottoms[j] = childBottom;
        }

        final LayoutRecord rec = mLayoutRecords.get(position);
        if (rec != null && rec.height != childHeight) {
            // Invalidate our layout records for everything before this.
            rec.height = childHeight;
            rebuildLayoutRecordsBefore = position;
        }

        if (rec != null && rec.span != span) {
            // Invalidate our layout records for everything after this.
            rec.span = span;
            rebuildLayoutRecordsAfter = position;
        }
    }

    // Update mItemBottoms for any empty columns
    for (int i = 0; i < mColCount; i++) {
        if (mItemBottoms[i] == Integer.MIN_VALUE) {
            mItemBottoms[i] = mItemTops[i];
        }
    }

    if (rebuildLayoutRecordsBefore >= 0 || rebuildLayoutRecordsAfter >= 0) {
        if (rebuildLayoutRecordsBefore >= 0) {
            invalidateLayoutRecordsBeforePosition(rebuildLayoutRecordsBefore);
        }
        if (rebuildLayoutRecordsAfter >= 0) {
            invalidateLayoutRecordsAfterPosition(rebuildLayoutRecordsAfter);
        }
        for (int i = 0; i < (childCount - amountRemoved); i++) {
            final int position = mFirstPosition + i;
            final View child = getChildAt(i);
            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
            LayoutRecord rec = mLayoutRecords.get(position);
            if (rec == null) {
                rec = new LayoutRecord();
                mLayoutRecords.put(position, rec);
            }
            rec.column = lp.column;
            rec.height = child.getHeight();
            rec.id = lp.id;
            rec.span = Math.min(mColCount, lp.span);
        }
    }

    if (this.mSelectorPosition != INVALID_POSITION) {
        View child = getChildAt(mMotionPosition - mFirstPosition);
        if (child != null)
            positionSelector(mMotionPosition, child);
    } else if (mTouchMode > TOUCH_MODE_DOWN) {
        View child = getChildAt(mMotionPosition - mFirstPosition);
        if (child != null)
            positionSelector(mMotionPosition, child);
    } else {
        mSelectorRect.setEmpty();
    }
}

From source file:cn.iterlog.myapplication.widget.overscroll.StaggeredGridView.java

/**
 * Measure and layout all currently visible children.
 *
 * @param queryAdapter true to requery the adapter for view data
 *///from  ww w . j ava  2 s.co m
final void layoutChildren(boolean queryAdapter) {
    final int paddingLeft = getPaddingLeft();
    final int paddingRight = getPaddingRight();
    final int itemMargin = mItemMargin;
    final int colWidth = (getWidth() - paddingLeft - paddingRight - itemMargin * (mColCount - 1)) / mColCount;
    mColWidth = colWidth;
    int rebuildLayoutRecordsBefore = -1;
    int rebuildLayoutRecordsAfter = -1;

    Arrays.fill(mItemBottoms, Integer.MIN_VALUE);

    final int childCount = getChildCount();
    int amountRemoved = 0;

    for (int i = 0; i < childCount; i++) {
        View child = getChildAt(i);
        LayoutParams lp = (LayoutParams) child.getLayoutParams();
        final int col = lp.column;
        final int position = mFirstPosition + i;
        final boolean needsLayout = queryAdapter || child.isLayoutRequested();

        if (queryAdapter) {

            View newView = obtainView(position, child);
            if (newView == null) {
                // child has been removed
                removeViewAt(i);
                if (i - 1 >= 0)
                    invalidateLayoutRecordsAfterPosition(i - 1);
                amountRemoved++;
                continue;
            } else if (newView != child) {
                removeViewAt(i);
                addView(newView, i);
                child = newView;
            }
            lp = (LayoutParams) child.getLayoutParams(); // Might have changed
        }

        final int span = Math.min(mColCount, lp.span);
        final int widthSize = colWidth * span + itemMargin * (span - 1);

        if (needsLayout) {
            final int widthSpec = MeasureSpec.makeMeasureSpec(widthSize, MeasureSpec.EXACTLY);

            final int heightSpec;
            if (lp.height == LayoutParams.WRAP_CONTENT) {
                heightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
            } else {
                heightSpec = MeasureSpec.makeMeasureSpec(lp.height, MeasureSpec.EXACTLY);
            }

            child.measure(widthSpec, heightSpec);
        }

        int childTop = mItemBottoms[col] > Integer.MIN_VALUE ? mItemBottoms[col] + mItemMargin : child.getTop();

        if (span > 1) {
            int lowest = childTop;
            for (int j = col + 1; j < col + span; j++) {
                final int bottom = mItemBottoms[j] + mItemMargin;
                if (bottom > lowest) {
                    lowest = bottom;
                }
            }
            childTop = lowest;
        }
        final int childHeight = child.getMeasuredHeight();
        final int childBottom = childTop + childHeight;
        final int childLeft = paddingLeft + col * (colWidth + itemMargin);
        final int childRight = childLeft + child.getMeasuredWidth();
        child.layout(childLeft, childTop, childRight, childBottom);

        for (int j = col; j < col + span; j++) {
            mItemBottoms[j] = childBottom;
        }

        final LayoutRecord rec = mLayoutRecords.get(position);
        if (rec != null && rec.height != childHeight) {
            // Invalidate our layout records for everything before this.
            rec.height = childHeight;
            rebuildLayoutRecordsBefore = position;
        }

        if (rec != null && rec.span != span) {
            // Invalidate our layout records for everything after this.
            rec.span = span;
            rebuildLayoutRecordsAfter = position;
        }
    }

    // Update mItemBottoms for any empty columns
    for (int i = 0; i < mColCount; i++) {
        if (mItemBottoms[i] == Integer.MIN_VALUE) {
            mItemBottoms[i] = mItemTops[i];
        }
    }

    if (rebuildLayoutRecordsBefore >= 0 || rebuildLayoutRecordsAfter >= 0) {
        if (rebuildLayoutRecordsBefore >= 0) {
            invalidateLayoutRecordsBeforePosition(rebuildLayoutRecordsBefore);
        }
        if (rebuildLayoutRecordsAfter >= 0) {
            invalidateLayoutRecordsAfterPosition(rebuildLayoutRecordsAfter);
        }
        for (int i = 0; i < (childCount - amountRemoved); i++) {
            final int position = mFirstPosition + i;
            final View child = getChildAt(i);
            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
            LayoutRecord rec = mLayoutRecords.get(position);
            if (rec == null) {
                rec = new LayoutRecord();
                mLayoutRecords.put(position, rec);
            }
            rec.column = lp.column;
            rec.height = child.getHeight();
            rec.id = lp.id;
            rec.span = Math.min(mColCount, lp.span);
        }
    }

    if (this.mSelectorPosition != INVALID_POSITION) {
        View child = getChildAt(mMotionPosition - mFirstPosition);
        if (child != null)
            positionSelector(mMotionPosition, child);
    } else if (mTouchMode > TOUCH_MODE_DOWN) {
        View child = getChildAt(mMotionPosition - mFirstPosition);
        if (child != null)
            positionSelector(mMotionPosition, child);
    } else {
        mSelectorRect.setEmpty();
    }
}