Example usage for android.view View getLayoutParams

List of usage examples for android.view View getLayoutParams

Introduction

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

Prototype

@ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
public ViewGroup.LayoutParams getLayoutParams() 

Source Link

Document

Get the LayoutParams associated with this view.

Usage

From source file:VerticalViewPager.java

/**
 * This method will be invoked when the current page is scrolled, either as part
 * of a programmatically initiated smooth scroll or a user initiated touch scroll.
 * If you override this method you must call through to the superclass implementation
 * (e.g. super.onPageScrolled(position, offset, offsetPixels)) before onPageScrolled
 * returns./*from www .j  a v a  2 s  .co  m*/
 *
 * @param position Position index of the first page currently being displayed.
 *                 Page position+1 will be visible if positionOffset is nonzero.
 * @param offset Value from [0, 1) indicating the offset from the page at position.
 * @param offsetPixels Value in pixels indicating the offset from position.
 */
protected void onPageScrolled(int position, float offset, int offsetPixels) {
    // Offset any decor views if needed - keep them on-screen at all times.
    if (mDecorChildCount > 0) {
        final int scrollY = getScrollY();
        int paddingTop = getPaddingTop();
        int paddingBottom = getPaddingBottom();
        final int height = getHeight();
        final int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View child = getChildAt(i);
            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
            if (!lp.isDecor)
                continue;

            final int vgrav = lp.gravity & Gravity.VERTICAL_GRAVITY_MASK;
            int childTop = 0;
            switch (vgrav) {
            default:
                childTop = paddingTop;
                break;
            case Gravity.TOP:
                childTop = paddingTop;
                paddingTop += child.getHeight();
                break;
            case Gravity.CENTER_VERTICAL:
                childTop = Math.max((height - child.getMeasuredHeight()) / 2, paddingTop);
                break;
            case Gravity.BOTTOM:
                childTop = height - paddingBottom - child.getMeasuredHeight();
                paddingBottom += child.getMeasuredHeight();
                break;
            }
            childTop += scrollY;

            final int childOffset = childTop - child.getTop();
            if (childOffset != 0) {
                child.offsetTopAndBottom(childOffset);
            }
        }
    }

    if (mOnPageChangeListener != null) {
        mOnPageChangeListener.onPageScrolled(position, offset, offsetPixels);
    }
    if (mInternalPageChangeListener != null) {
        mInternalPageChangeListener.onPageScrolled(position, offset, offsetPixels);
    }

    if (mPageTransformer != null) {
        final int scrollY = getScrollY();
        final int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View child = getChildAt(i);
            final LayoutParams lp = (LayoutParams) child.getLayoutParams();

            if (lp.isDecor)
                continue;

            final float transformPos = (float) (child.getTop() - scrollY) / getClientHeight();
            mPageTransformer.transformPage(child, transformPos);
        }
    }

    mCalledSuper = true;
}

From source file:VerticalViewPager.java

void dataSetChanged() {
    // This method only gets called if our observer is attached, so mAdapter is non-null.

    final int adapterCount = mAdapter.getCount();
    mExpectedAdapterCount = adapterCount;
    boolean needPopulate = mItems.size() < mOffscreenPageLimit * 2 + 1 && mItems.size() < adapterCount;
    int newCurrItem = mCurItem;

    boolean isUpdating = false;
    for (int i = 0; i < mItems.size(); i++) {
        final ItemInfo ii = mItems.get(i);
        final int newPos = mAdapter.getItemPosition(ii.object);

        if (newPos == PagerAdapter.POSITION_UNCHANGED) {
            continue;
        }/*from   w  w  w.  java  2s . c o  m*/

        if (newPos == PagerAdapter.POSITION_NONE) {
            mItems.remove(i);
            i--;

            if (!isUpdating) {
                mAdapter.startUpdate(this);
                isUpdating = true;
            }

            mAdapter.destroyItem(this, ii.position, ii.object);
            needPopulate = true;

            if (mCurItem == ii.position) {
                // Keep the current item in the valid range
                newCurrItem = Math.max(0, Math.min(mCurItem, adapterCount - 1));
                needPopulate = true;
            }
            continue;
        }

        if (ii.position != newPos) {
            if (ii.position == mCurItem) {
                // Our current item changed position. Follow it.
                newCurrItem = newPos;
            }

            ii.position = newPos;
            needPopulate = true;
        }
    }

    if (isUpdating) {
        mAdapter.finishUpdate(this);
    }

    Collections.sort(mItems, COMPARATOR);

    if (needPopulate) {
        // Reset our known page widths; populate will recompute them.
        final int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View child = getChildAt(i);
            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
            if (!lp.isDecor) {
                lp.heightFactor = 0.f;
            }
        }

        setCurrentItemInternal(newCurrItem, false, true);
        requestLayout();
    }
}

From source file:android.support.custom.view.VerticalViewPager.java

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    if (DEBUG)/* w  ww.  j a v a2  s  .  c  om*/
        Log.d(TAG, "onLayout");

    mInLayout = true;
    populate();
    mInLayout = false;

    final int count = getChildCount();
    int width = r - l;
    int height = b - t;
    int paddingLeft = getPaddingLeft();
    int paddingTop = getPaddingTop();
    int paddingRight = getPaddingRight();
    int paddingBottom = getPaddingBottom();
    final int scrollY = getScrollY();

    int decorCount = 0;

    // First pass - decor views. We need to do this in two passes so that
    // we have the proper offsets for non-decor views later.
    for (int i = 0; i < count; i++) {
        final View child = getChildAt(i);
        if (child.getVisibility() != GONE) {
            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
            int childLeft = 0;
            int childTop = 0;
            if (lp.isDecor) {
                final int hgrav = lp.gravity & Gravity.HORIZONTAL_GRAVITY_MASK;
                final int vgrav = lp.gravity & Gravity.VERTICAL_GRAVITY_MASK;
                switch (hgrav) {
                default:
                    childLeft = paddingLeft;
                    break;
                case Gravity.LEFT:
                    childLeft = paddingLeft;
                    paddingLeft += child.getMeasuredWidth();
                    break;
                case Gravity.CENTER_HORIZONTAL:
                    childLeft = Math.max((width - child.getMeasuredWidth()) / 2, paddingLeft);
                    break;
                case Gravity.RIGHT:
                    childLeft = width - paddingRight - child.getMeasuredWidth();
                    paddingRight += child.getMeasuredWidth();
                    break;
                }
                switch (vgrav) {
                default:
                    childTop = paddingTop;
                    break;
                case Gravity.TOP:
                    childTop = paddingTop;
                    paddingTop += child.getMeasuredHeight();
                    break;
                case Gravity.CENTER_VERTICAL:
                    childTop = Math.max((height - child.getMeasuredHeight()) / 2, paddingTop);
                    break;
                case Gravity.BOTTOM:
                    childTop = height - paddingBottom - child.getMeasuredHeight();
                    paddingBottom += child.getMeasuredHeight();
                    break;
                }
                childTop += scrollY;
                child.layout(childLeft, childTop, childLeft + child.getMeasuredWidth(),
                        childTop + child.getMeasuredHeight());
                decorCount++;
            }
        }
    }

    // Page views. Do this once we have the right padding offsets from above.
    for (int i = 0; i < count; i++) {
        final View child = getChildAt(i);
        if (child.getVisibility() != GONE) {
            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
            ItemInfo ii;
            if (!lp.isDecor && (ii = infoForChild(child)) != null) {
                int loff = (int) (height * ii.offset);

                int childLeft = paddingLeft;

                int childTop = paddingTop = loff;

                if (lp.needsMeasure) {
                    // This was added during layout and needs measurement.
                    // Do it now that we know what we're working with.
                    lp.needsMeasure = false;
                    final int widthSpec = MeasureSpec.makeMeasureSpec(
                            (int) ((width - paddingLeft - paddingRight) * lp.widthFactor), MeasureSpec.EXACTLY);
                    final int heightSpec = MeasureSpec
                            .makeMeasureSpec((int) (height - paddingTop - paddingBottom), MeasureSpec.EXACTLY);
                    child.measure(widthSpec, heightSpec);
                }
                if (DEBUG)
                    Log.v(TAG, "Positioning #" + i + " " + child + " f=" + ii.object + ":" + childLeft + ","
                            + childTop + " " + child.getMeasuredWidth() + "x" + child.getMeasuredHeight());
                child.layout(childLeft, childTop, childLeft + child.getMeasuredWidth(),
                        childTop + child.getMeasuredHeight());
            }
        }
    }
    mLeftPageBounds = paddingLeft;
    mRightPageBounds = width - paddingRight;
    mDecorChildCount = decorCount;
    mFirstLayout = false;
}

From source file:android.support.custom.view.VerticalViewPager.java

void populate(int newCurrentItem) {
    ItemInfo oldCurInfo = null;//from  w  ww.ja v a  2 s. c om
    if (mCurItem != newCurrentItem) {
        oldCurInfo = infoForPosition(mCurItem);
        mCurItem = newCurrentItem;
    }

    if (mAdapter == null) {
        return;
    }

    // Bail now if we are waiting to populate.  This is to hold off
    // on creating views from the time the user releases their finger to
    // fling to a new position until we have finished the scroll to
    // that position, avoiding glitches from happening at that point.
    if (mPopulatePending) {
        if (DEBUG)
            Log.i(TAG, "populate is pending, skipping for now...");
        return;
    }

    // Also, don't populate until we are attached to a window.  This is to
    // avoid trying to populate before we have restored our view hierarchy
    // state and conflicting with what is restored.
    if (getWindowToken() == null) {
        return;
    }

    mAdapter.startUpdate(this);

    final int pageLimit = mOffscreenPageLimit;
    final int startPos = Math.max(0, mCurItem - pageLimit);
    final int N = mAdapter.getCount();
    final int endPos = Math.min(N - 1, mCurItem + pageLimit);

    // Locate the currently focused item or add it if needed.
    int curIndex = -1;
    ItemInfo curItem = null;
    for (curIndex = 0; curIndex < mItems.size(); curIndex++) {
        final ItemInfo ii = mItems.get(curIndex);
        if (ii.position >= mCurItem) {
            if (ii.position == mCurItem)
                curItem = ii;
            break;
        }
    }

    if (curItem == null && N > 0) {
        curItem = addNewItem(mCurItem, curIndex);
    }

    // Fill 3x the available width or up to the number of offscreen
    // pages requested to either side, whichever is larger.
    // If we have no current item we have no work to do.
    if (curItem != null) {
        float extraHeightLeft = 0f;
        int itemIndex = curIndex - 1;
        ItemInfo ii = itemIndex >= 0 ? mItems.get(itemIndex) : null;
        final float topHeightNeeded = 2.f - curItem.heightFactor;

        for (int pos = mCurItem - 1; pos >= 0; pos--) {
            if (extraHeightLeft >= topHeightNeeded && pos < startPos) {
                if (ii == null) {
                    break;
                }
                if (pos == ii.position && !ii.scrolling) {
                    mItems.remove(itemIndex);
                    mAdapter.destroyItem(this, pos, ii.object);
                    if (DEBUG) {
                        Log.i(TAG,
                                "populate() - destroyItem() with pos: " + pos + " view: " + ((View) ii.object));
                    }
                    itemIndex--;
                    curIndex--;
                    ii = itemIndex >= 0 ? mItems.get(itemIndex) : null;
                }
            } else if (ii != null && pos == ii.position) {
                extraHeightLeft += ii.heightFactor;
                itemIndex--;
                ii = itemIndex >= 0 ? mItems.get(itemIndex) : null;
            } else {
                ii = addNewItem(pos, itemIndex + 1);
                extraHeightLeft += ii.heightFactor;
                curIndex++;
                ii = itemIndex >= 0 ? mItems.get(itemIndex) : null;
            }
        }

        float extraHeightBottom = curItem.heightFactor;
        itemIndex = curIndex + 1;
        if (extraHeightBottom < 2.f) {
            ii = itemIndex < mItems.size() ? mItems.get(itemIndex) : null;
            for (int pos = mCurItem + 1; pos < N; pos++) {
                if (extraHeightBottom >= 2.f && pos > endPos) {
                    if (ii == null) {
                        break;
                    }
                    if (pos == ii.position && !ii.scrolling) {
                        mItems.remove(itemIndex);
                        mAdapter.destroyItem(this, pos, ii.object);
                        if (DEBUG) {
                            Log.i(TAG, "populate() - destroyItem() with pos: " + pos + " view: "
                                    + ((View) ii.object));
                        }
                        ii = itemIndex < mItems.size() ? mItems.get(itemIndex) : null;
                    }
                } else if (ii != null && pos == ii.position) {
                    extraHeightBottom += ii.heightFactor;
                    itemIndex++;
                    ii = itemIndex < mItems.size() ? mItems.get(itemIndex) : null;
                } else {
                    ii = addNewItem(pos, itemIndex);
                    itemIndex++;
                    extraHeightBottom += ii.heightFactor;
                    ii = itemIndex < mItems.size() ? mItems.get(itemIndex) : null;
                }
            }
        }

        calculatePageOffsets(curItem, curIndex, oldCurInfo);
    }

    if (DEBUG) {
        Log.i(TAG, "Current page list:");
        for (int i = 0; i < mItems.size(); i++) {
            Log.i(TAG, "#" + i + ": page " + mItems.get(i).position);
        }
    }

    mAdapter.setPrimaryItem(this, mCurItem, curItem != null ? curItem.object : null);

    mAdapter.finishUpdate(this);

    // Check width measurement of current pages and drawing sort order.
    // Update LayoutParams as needed.
    final boolean sort = mDrawingOrder != DRAW_ORDER_DEFAULT;
    if (sort) {
        if (mDrawingOrderedChildren == null) {
            mDrawingOrderedChildren = new ArrayList<View>();
        } else {
            mDrawingOrderedChildren.clear();
        }
    }
    final int childCount = getChildCount();
    for (int i = 0; i < childCount; i++) {
        final View child = getChildAt(i);
        final LayoutParams lp = (LayoutParams) child.getLayoutParams();
        lp.childIndex = i;
        if (!lp.isDecor && lp.heightFactor == 0.f) {
            // 0 means requery the adapter for this, it doesn't have a valid width.
            final ItemInfo ii = infoForChild(child);
            if (ii != null) {
                lp.heightFactor = ii.heightFactor;
                lp.position = ii.position;
            }
        }
        if (sort)
            mDrawingOrderedChildren.add(child);
    }
    if (sort) {
        Collections.sort(mDrawingOrderedChildren, sPositionComparator);
    }

    if (hasFocus()) {
        View currentFocused = findFocus();
        ItemInfo ii = currentFocused != null ? infoForAnyChild(currentFocused) : null;
        if (ii == null || ii.position != mCurItem) {
            for (int i = 0; i < getChildCount(); i++) {
                View child = getChildAt(i);
                ii = infoForChild(child);
                if (ii != null && ii.position == mCurItem) {
                    if (child.requestFocus(FOCUS_FORWARD)) {
                        break;
                    }
                }
            }
        }
    }
}

From source file:cc.flydev.launcher.Page.java

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    if (!mIsDataReady || getChildCount() == 0) {
        return;/*www  .j  a  v  a 2 s .co m*/
    }

    if (DEBUG)
        Log.d(TAG, "PagedView.onLayout()");
    final int childCount = getChildCount();

    int screenWidth = getViewportWidth();

    int offsetX = getViewportOffsetX();
    int offsetY = getViewportOffsetY();

    // Update the viewport offsets
    mViewport.offset(offsetX, offsetY);

    final boolean isRtl = isLayoutRtl();

    final int startIndex = isRtl ? childCount - 1 : 0;
    final int endIndex = isRtl ? -1 : childCount;
    final int delta = isRtl ? -1 : 1;

    int verticalPadding = getPaddingTop() + getPaddingBottom();

    int childLeft = mFirstChildLeft = offsetX + (screenWidth - getChildWidth(startIndex)) / 2;
    if (mPageScrolls == null || getChildCount() != mChildCountOnLastLayout) {
        mPageScrolls = new int[getChildCount()];
    }

    for (int i = startIndex; i != endIndex; i += delta) {
        final View child = getPageAt(i);
        if (child.getVisibility() != View.GONE) {
            LayoutParams lp = (LayoutParams) child.getLayoutParams();
            int childTop;
            if (lp.isFullScreenPage) {
                childTop = offsetY;
            } else {
                childTop = offsetY + getPaddingTop() + mInsets.top;
                if (mCenterPagesVertically) {
                    childTop += (getViewportHeight() - mInsets.top - mInsets.bottom - verticalPadding
                            - child.getMeasuredHeight()) / 2;
                }
            }

            final int childWidth = child.getMeasuredWidth();
            final int childHeight = child.getMeasuredHeight();

            if (DEBUG)
                Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
            child.layout(childLeft, childTop, childLeft + child.getMeasuredWidth(), childTop + childHeight);

            // We assume the left and right padding are equal, and hence center the pages
            // horizontally
            int scrollOffset = (getViewportWidth() - childWidth) / 2;
            mPageScrolls[i] = childLeft - scrollOffset - offsetX;

            if (i != endIndex - delta) {
                childLeft += childWidth + scrollOffset;
                int nextScrollOffset = (getViewportWidth() - getChildWidth(i + delta)) / 2;
                childLeft += nextScrollOffset;
            }
        }
    }

    if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
        setHorizontalScrollBarEnabled(false);
        updateCurrentPageScroll();
        setHorizontalScrollBarEnabled(true);
        mFirstLayout = false;
    }

    if (childCount > 0) {
        final int index = isLayoutRtl() ? 0 : childCount - 1;
        mMaxScrollX = getScrollForPage(index);
    } else {
        mMaxScrollX = 0;
    }

    if (mScroller.isFinished() && mChildCountOnLastLayout != getChildCount() && !mDeferringForDelete) {
        if (mRestorePage != INVALID_RESTORE_PAGE) {
            setCurrentPage(mRestorePage);
            mRestorePage = INVALID_RESTORE_PAGE;
        } else {
            setCurrentPage(getNextPage());
        }
    }
    mChildCountOnLastLayout = getChildCount();

    if (isReordering(true)) {
        updateDragViewTranslationDuringDrag();
    }
}

From source file:android.support.v7.widget.GridLayout.java

private int computeLayoutParamsHashCode() {
    int result = 1;
    for (int i = 0, N = getChildCount(); i < N; i++) {
        View c = getChildAt(i);
        if (c.getVisibility() == View.GONE)
            continue;
        LayoutParams lp = (LayoutParams) c.getLayoutParams();
        result = 31 * result + lp.hashCode();
    }// w  w  w .j a  va2 s  .c o m
    return result;
}

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

void dispatchDependentViewRemoved(View view) {
    final int childCount = mDependencySortedChildren.size();
    boolean viewSeen = false;
    for (int i = 0; i < childCount; i++) {
        final View child = mDependencySortedChildren.get(i);
        if (child == view) {
            // We've seen our view, which means that any Views after this could be dependent
            viewSeen = true;/*  ww  w . j a va 2 s . c om*/
            continue;
        }
        if (viewSeen) {
            CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams) child.getLayoutParams();
            CoordinatorLayout.Behavior b = lp.getBehavior();
            if (b != null && lp.dependsOn(this, child, view)) {
                b.onDependentViewRemoved(this, child, view);
            }
        }
    }
}

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

/**
 * Allows the caller to manually dispatch
 * {@link Behavior#onDependentViewChanged(CoordinatorLayout, View, View)} to the associated
 * {@link Behavior} instances of views which depend on the provided {@link View}.
 *
 * <p>You should not normally need to call this method as the it will be automatically done
 * when the view has changed./*from w w w .j  a  va2s  .  c o m*/
 *
 * @param view the View to find dependents of to dispatch the call.
 */
public void dispatchDependentViewsChanged(View view) {
    final int childCount = mDependencySortedChildren.size();
    boolean viewSeen = false;
    for (int i = 0; i < childCount; i++) {
        final View child = mDependencySortedChildren.get(i);
        if (child == view) {
            // We've seen our view, which means that any Views after this could be dependent
            viewSeen = true;
            continue;
        }
        if (viewSeen) {
            CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams) child.getLayoutParams();
            CoordinatorLayout.Behavior b = lp.getBehavior();
            if (b != null && lp.dependsOn(this, child, view)) {
                b.onDependentViewChanged(this, child, view);
            }
        }
    }
}

From source file:cc.flydev.launcher.Page.java

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    if (!mIsDataReady || getChildCount() == 0) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        return;//w  w  w.  java  2  s  . com
    }

    // We measure the dimensions of the PagedView to be larger than the pages so that when we
    // zoom out (and scale down), the view is still contained in the parent
    int widthMode = MeasureSpec.getMode(widthMeasureSpec);
    int widthSize = MeasureSpec.getSize(widthMeasureSpec);
    int heightMode = MeasureSpec.getMode(heightMeasureSpec);
    int heightSize = MeasureSpec.getSize(heightMeasureSpec);
    // NOTE: We multiply by 1.5f to account for the fact that depending on the offset of the
    // viewport, we can be at most one and a half screens offset once we scale down
    DisplayMetrics dm = getResources().getDisplayMetrics();
    int maxSize = Math.max(dm.widthPixels, dm.heightPixels + mInsets.top + mInsets.bottom);

    int parentWidthSize, parentHeightSize;
    int scaledWidthSize, scaledHeightSize;
    if (mUseMinScale) {
        parentWidthSize = (int) (1.5f * maxSize);
        parentHeightSize = maxSize;
        scaledWidthSize = (int) (parentWidthSize / mMinScale);
        scaledHeightSize = (int) (parentHeightSize / mMinScale);
    } else {
        scaledWidthSize = widthSize;
        scaledHeightSize = heightSize;
    }
    mViewport.set(0, 0, widthSize, heightSize);

    if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        return;
    }

    // Return early if we aren't given a proper dimension
    if (widthSize <= 0 || heightSize <= 0) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        return;
    }

    /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
     * of the All apps view on XLarge displays to not take up more space then it needs. Width
     * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
     * each page to have the same width.
     */
    final int verticalPadding = getPaddingTop() + getPaddingBottom();
    final int horizontalPadding = getPaddingLeft() + getPaddingRight();

    // The children are given the same width and height as the workspace
    // unless they were set to WRAP_CONTENT
    if (DEBUG)
        Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
    if (DEBUG)
        Log.d(TAG, "PagedView.scaledSize: " + scaledWidthSize + ", " + scaledHeightSize);
    if (DEBUG)
        Log.d(TAG, "PagedView.parentSize: " + parentWidthSize + ", " + parentHeightSize);
    if (DEBUG)
        Log.d(TAG, "PagedView.horizontalPadding: " + horizontalPadding);
    if (DEBUG)
        Log.d(TAG, "PagedView.verticalPadding: " + verticalPadding);
    final int childCount = getChildCount();
    for (int i = 0; i < childCount; i++) {
        // disallowing padding in paged view (just pass 0)
        final View child = getPageAt(i);
        if (child.getVisibility() != GONE) {
            final LayoutParams lp = (LayoutParams) child.getLayoutParams();

            int childWidthMode;
            int childHeightMode;
            int childWidth;
            int childHeight;

            if (!lp.isFullScreenPage) {
                if (lp.width == LayoutParams.WRAP_CONTENT) {
                    childWidthMode = MeasureSpec.AT_MOST;
                } else {
                    childWidthMode = MeasureSpec.EXACTLY;
                }

                if (lp.height == LayoutParams.WRAP_CONTENT) {
                    childHeightMode = MeasureSpec.AT_MOST;
                } else {
                    childHeightMode = MeasureSpec.EXACTLY;
                }

                childWidth = widthSize - horizontalPadding;
                childHeight = heightSize - verticalPadding - mInsets.top - mInsets.bottom;
                mNormalChildHeight = childHeight;

            } else {
                childWidthMode = MeasureSpec.EXACTLY;
                childHeightMode = MeasureSpec.EXACTLY;

                if (mUseMinScale) {
                    childWidth = getViewportWidth();
                    childHeight = getViewportHeight();
                } else {
                    childWidth = widthSize - getPaddingLeft() - getPaddingRight();
                    childHeight = heightSize - getPaddingTop() - getPaddingBottom();
                }
            }

            final int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(childWidth, childWidthMode);
            final int childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(childHeight, childHeightMode);
            child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
        }
    }
    setMeasuredDimension(scaledWidthSize, scaledHeightSize);

    if (childCount > 0) {
        // Calculate the variable page spacing if necessary
        if (mAutoComputePageSpacing && mRecomputePageSpacing) {
            // The gap between pages in the PagedView should be equal to the gap from the page
            // to the edge of the screen (so it is not visible in the current screen).  To
            // account for unequal padding on each side of the paged view, we take the maximum
            // of the left/right gap and use that as the gap between each page.
            int offset = (getViewportWidth() - getChildWidth(0)) / 2;
            int spacing = Math.max(offset, widthSize - offset - getChildAt(0).getMeasuredWidth());
            setPageSpacing(spacing);
            mRecomputePageSpacing = false;
        }
    }
}

From source file:VerticalViewPager.java

@SuppressWarnings("deprecation")
@Override//w  w w  . j av  a2  s  .c o  m
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    // For simple implementation, our internal size is always 0.
    // We depend on the container to specify the layout size of
    // our view.  We can't really know what it is since we will be
    // adding and removing different arbitrary views and do not
    // want the layout to change as this happens.
    setMeasuredDimension(getDefaultSize(0, widthMeasureSpec), getDefaultSize(0, heightMeasureSpec));

    final int measuredWidth = getMeasuredWidth();
    final int measuredHeight = getMeasuredHeight();

    final int maxGutterSize = measuredHeight / 10;
    mGutterSize = Math.min(maxGutterSize, mDefaultGutterSize);

    // Children are just made to fill our space.
    int childWidthSize = measuredWidth - getPaddingLeft() - getPaddingRight();
    int childHeightSize = measuredHeight - getPaddingTop() - getPaddingBottom();

    /*
     * Make sure all children have been properly measured. Decor views first.
     * Right now we cheat and make this less complicated by assuming decor
     * views won't intersect. We will pin to edges based on gravity.
     */
    int size = getChildCount();
    for (int i = 0; i < size; ++i) {
        final View child = getChildAt(i);
        if (child.getVisibility() != GONE) {
            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
            if (lp != null && lp.isDecor) {
                final int hgrav = lp.gravity & Gravity.HORIZONTAL_GRAVITY_MASK;
                final int vgrav = lp.gravity & Gravity.VERTICAL_GRAVITY_MASK;
                int widthMode = MeasureSpec.AT_MOST;
                int heightMode = MeasureSpec.AT_MOST;
                boolean consumeVertical = vgrav == Gravity.TOP || vgrav == Gravity.BOTTOM;
                boolean consumeHorizontal = hgrav == Gravity.LEFT || hgrav == Gravity.RIGHT;

                if (consumeVertical) {
                    widthMode = MeasureSpec.EXACTLY;
                } else if (consumeHorizontal) {
                    heightMode = MeasureSpec.EXACTLY;
                }

                int widthSize = childWidthSize;
                int heightSize = childHeightSize;
                if (lp.width != LayoutParams.WRAP_CONTENT) {
                    widthMode = MeasureSpec.EXACTLY;
                    if (lp.width != LayoutParams.FILL_PARENT) {
                        widthSize = lp.width;
                    }
                }
                if (lp.height != LayoutParams.WRAP_CONTENT) {
                    heightMode = MeasureSpec.EXACTLY;
                    if (lp.height != LayoutParams.FILL_PARENT) {
                        heightSize = lp.height;
                    }
                }
                final int widthSpec = MeasureSpec.makeMeasureSpec(widthSize, widthMode);
                final int heightSpec = MeasureSpec.makeMeasureSpec(heightSize, heightMode);
                child.measure(widthSpec, heightSpec);

                if (consumeVertical) {
                    childHeightSize -= child.getMeasuredHeight();
                } else if (consumeHorizontal) {
                    childWidthSize -= child.getMeasuredWidth();
                }
            }
        }
    }

    mChildWidthMeasureSpec = MeasureSpec.makeMeasureSpec(childWidthSize, MeasureSpec.EXACTLY);
    mChildHeightMeasureSpec = MeasureSpec.makeMeasureSpec(childHeightSize, MeasureSpec.EXACTLY);

    // Make sure we have created all fragments that we need to have shown.
    mInLayout = true;
    populate();
    mInLayout = false;

    // Page views next.
    size = getChildCount();
    for (int i = 0; i < size; ++i) {
        final View child = getChildAt(i);
        if (child.getVisibility() != GONE) {
            if (DEBUG)
                Log.v(TAG, "Measuring #" + i + " " + child + ": " + mChildWidthMeasureSpec);

            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
            if (lp == null || !lp.isDecor) {
                final int heightSpec = MeasureSpec.makeMeasureSpec((int) (childHeightSize * lp.heightFactor),
                        MeasureSpec.EXACTLY);
                child.measure(mChildWidthMeasureSpec, heightSpec);
            }
        }
    }
}