Example usage for android.view View getMeasuredHeight

List of usage examples for android.view View getMeasuredHeight

Introduction

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

Prototype

public final int getMeasuredHeight() 

Source Link

Document

Like #getMeasuredHeightAndState() , but only returns the raw height component (that is the result is masked by #MEASURED_SIZE_MASK ).

Usage

From source file:com.example.CustomSlidingPaneLayout.java

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {

    final int width = r - l;
    final int paddingLeft = getPaddingLeft();
    final int paddingRight = getPaddingRight();
    final int paddingTop = getPaddingTop();

    final int childCount = getChildCount();
    int xStart = paddingLeft;
    int nextXStart = xStart;

    if (mFirstLayout) {
        mSlideOffset = mCanSlide && mPreservedOpenState ? 1.f : 0.f;
    }//w  ww .j a  v a 2  s.c o m

    for (int i = 0; i < childCount; i++) {
        final View child = getChildAt(i);

        if (child.getVisibility() == GONE) {
            continue;
        }

        final LayoutParams lp = (LayoutParams) child.getLayoutParams();

        final int childWidth = child.getMeasuredWidth();
        int offset = 0;

        if (lp.slideable) {
            final int margin = lp.leftMargin + lp.rightMargin;
            final int range = Math.min(nextXStart, width - paddingRight - mOverhangSize) - xStart - margin;
            mSlideRange = range;
            lp.dimWhenOffset = xStart + lp.leftMargin + range + childWidth / 2 > width - paddingRight;
            final int pos = (int) (range * mSlideOffset);
            xStart += pos + lp.leftMargin;
            mSlideOffset = (float) pos / mSlideRange;
        } else if (mCanSlide && mParallaxBy != 0) {
            offset = (int) ((1 - mSlideOffset) * mParallaxBy);
            xStart = nextXStart;
        } else {
            xStart = nextXStart;
        }

        final int childLeft = xStart - offset;
        final int childRight = childLeft + childWidth;
        final int childTop = paddingTop;
        final int childBottom = childTop + child.getMeasuredHeight();
        child.layout(childLeft, paddingTop, childRight, childBottom);

        nextXStart += child.getWidth();
    }

    if (mFirstLayout) {
        if (mCanSlide) {
            if (mParallaxBy != 0) {
                parallaxOtherViews(mSlideOffset);
            }
            if (((LayoutParams) mSlideableView.getLayoutParams()).dimWhenOffset) {
                dimChildView(mSlideableView, mSlideOffset, mSliderFadeColor);
            }
        } else {
            // Reset the dim level of all children; it's irrelevant when nothing moves.
            for (int i = 0; i < childCount; i++) {
                dimChildView(getChildAt(i), 0, mSliderFadeColor);
            }
        }
        updateObscuredViewsVisibility(mSlideableView);
    }

    mFirstLayout = false;
}

From source file:com.devabit.takestock.ui.widget.FlexboxLayout.java

/**
 * Sub method for {@link #onLayout(boolean, int, int, int, int)} when the
 * {@link #mFlexDirection} is either {@link #FLEX_DIRECTION_COLUMN} or
 * {@link #FLEX_DIRECTION_COLUMN_REVERSE}.
 *
 * @param isRtl           {@code true} if the horizontal layout direction is right to left,
 *                        {@code false}// ww  w. j a va2s.  c om
 *                        otherwise
 * @param fromBottomToTop {@code true} if the layout direction is bottom to top, {@code false}
 *                        otherwise
 * @param left            the left position of this View
 * @param top             the top position of this View
 * @param right           the right position of this View
 * @param bottom          the bottom position of this View
 * @see #getFlexWrap()
 * @see #setFlexWrap(int)
 * @see #getJustifyContent()
 * @see #setJustifyContent(int)
 * @see #getAlignItems()
 * @see #setAlignItems(int)
 * @see LayoutParams#alignSelf
 */
private void layoutVertical(boolean isRtl, boolean fromBottomToTop, int left, int top, int right, int bottom) {
    int paddingTop = getPaddingTop();
    int paddingBottom = getPaddingBottom();

    int paddingRight = getPaddingRight();
    int childLeft = getPaddingLeft();
    int currentViewIndex = 0;

    int width = right - left;
    int height = bottom - top;
    // childRight is used if the mFlexWrap is FLEX_WRAP_WRAP_REVERSE otherwise
    // childLeft is used to align the horizontal position of the children views.
    int childRight = width - paddingRight;

    // Use float to reduce the round error that may happen in when justifyContent ==
    // SPACE_BETWEEN or SPACE_AROUND
    float childTop;

    // Used only for if the direction is from bottom to top
    float childBottom;
    for (FlexLine flexLine : mFlexLines) {
        float spaceBetweenItem = 0f;
        switch (mJustifyContent) {
        case JUSTIFY_CONTENT_FLEX_START:
            childTop = paddingTop;
            childBottom = height - paddingBottom;
            break;
        case JUSTIFY_CONTENT_FLEX_END:
            childTop = height - flexLine.mainSize + paddingBottom;
            childBottom = flexLine.mainSize - paddingTop;
            break;
        case JUSTIFY_CONTENT_CENTER:
            childTop = paddingTop + (height - flexLine.mainSize) / 2f;
            childBottom = height - paddingBottom - (height - flexLine.mainSize) / 2f;
            break;
        case JUSTIFY_CONTENT_SPACE_AROUND:
            if (flexLine.itemCount != 0) {
                spaceBetweenItem = (height - flexLine.mainSize) / (float) flexLine.itemCount;
            }
            childTop = paddingTop + spaceBetweenItem / 2f;
            childBottom = height - paddingBottom - spaceBetweenItem / 2f;
            break;
        case JUSTIFY_CONTENT_SPACE_BETWEEN:
            childTop = paddingTop;
            float denominator = flexLine.itemCount != 1 ? flexLine.itemCount - 1 : 1f;
            spaceBetweenItem = (height - flexLine.mainSize) / denominator;
            childBottom = height - paddingBottom;
            break;
        default:
            throw new IllegalStateException("Invalid justifyContent is set: " + mJustifyContent);
        }
        spaceBetweenItem = Math.max(spaceBetweenItem, 0);

        for (int i = 0; i < flexLine.itemCount; i++) {
            View child = getReorderedChildAt(currentViewIndex);
            if (child == null) {
                continue;
            } else if (child.getVisibility() == View.GONE) {
                currentViewIndex++;
                continue;
            }
            LayoutParams lp = ((LayoutParams) child.getLayoutParams());
            childTop += lp.topMargin;
            childBottom -= lp.bottomMargin;
            if (isRtl) {
                if (fromBottomToTop) {
                    layoutSingleChildVertical(child, flexLine, true, mAlignItems,
                            childRight - child.getMeasuredWidth(),
                            Math.round(childBottom) - child.getMeasuredHeight(), childRight,
                            Math.round(childBottom));
                } else {
                    layoutSingleChildVertical(child, flexLine, true, mAlignItems,
                            childRight - child.getMeasuredWidth(), Math.round(childTop), childRight,
                            Math.round(childTop) + child.getMeasuredHeight());
                }
            } else {
                if (fromBottomToTop) {
                    layoutSingleChildVertical(child, flexLine, false, mAlignItems, childLeft,
                            Math.round(childBottom) - child.getMeasuredHeight(),
                            childLeft + child.getMeasuredWidth(), Math.round(childBottom));
                } else {
                    layoutSingleChildVertical(child, flexLine, false, mAlignItems, childLeft,
                            Math.round(childTop), childLeft + child.getMeasuredWidth(),
                            Math.round(childTop) + child.getMeasuredHeight());
                }
            }
            childTop += child.getMeasuredHeight() + spaceBetweenItem + lp.bottomMargin;
            childBottom -= child.getMeasuredHeight() + spaceBetweenItem + lp.topMargin;
            currentViewIndex++;
        }
        childLeft += flexLine.crossSize;
        childRight -= flexLine.crossSize;
    }
}

From source file:com.devabit.takestock.ui.widget.FlexboxLayout.java

/**
 * Sub method for {@link #onLayout(boolean, int, int, int, int)} when the
 * {@link #mFlexDirection} is either {@link #FLEX_DIRECTION_ROW} or
 * {@link #FLEX_DIRECTION_ROW_REVERSE}./* w ww . ja  v a 2 s  .com*/
 *
 * @param isRtl  {@code true} if the horizontal layout direction is right to left, {@code
 *               false} otherwise.
 * @param left   the left position of this View
 * @param top    the top position of this View
 * @param right  the right position of this View
 * @param bottom the bottom position of this View
 * @see #getFlexWrap()
 * @see #setFlexWrap(int)
 * @see #getJustifyContent()
 * @see #setJustifyContent(int)
 * @see #getAlignItems()
 * @see #setAlignItems(int)
 * @see LayoutParams#alignSelf
 */
private void layoutHorizontal(boolean isRtl, int left, int top, int right, int bottom) {
    int paddingLeft = getPaddingLeft();
    int paddingRight = getPaddingRight();
    // Use float to reduce the round error that may happen in when justifyContent ==
    // SPACE_BETWEEN or SPACE_AROUND
    float childLeft;
    int currentViewIndex = 0;

    int height = bottom - top;
    int width = right - left;
    // childBottom is used if the mFlexWrap is FLEX_WRAP_WRAP_REVERSE otherwise
    // childTop is used to align the vertical position of the children views.
    int childBottom = height - getPaddingBottom();
    int childTop = getPaddingTop();

    // Used only for RTL layout
    // Use float to reduce the round error that may happen in when justifyContent ==
    // SPACE_BETWEEN or SPACE_AROUND
    float childRight;
    for (FlexLine flexLine : mFlexLines) {
        float spaceBetweenItem = 0f;
        switch (mJustifyContent) {
        case JUSTIFY_CONTENT_FLEX_START:
            childLeft = paddingLeft;
            childRight = width - paddingRight;
            break;
        case JUSTIFY_CONTENT_FLEX_END:
            childLeft = width - flexLine.mainSize + paddingRight;
            childRight = flexLine.mainSize - paddingLeft;
            break;
        case JUSTIFY_CONTENT_CENTER:
            childLeft = paddingLeft + (width - flexLine.mainSize) / 2f;
            childRight = width - paddingRight - (width - flexLine.mainSize) / 2f;
            break;
        case JUSTIFY_CONTENT_SPACE_AROUND:
            if (flexLine.itemCount != 0) {
                spaceBetweenItem = (width - flexLine.mainSize) / (float) flexLine.itemCount;
            }
            childLeft = paddingLeft + spaceBetweenItem / 2f;
            childRight = width - paddingRight - spaceBetweenItem / 2f;
            break;
        case JUSTIFY_CONTENT_SPACE_BETWEEN:
            childLeft = paddingLeft;
            float denominator = flexLine.itemCount != 1 ? flexLine.itemCount - 1 : 1f;
            spaceBetweenItem = (width - flexLine.mainSize) / denominator;
            childRight = width - paddingRight;
            break;
        default:
            throw new IllegalStateException("Invalid justifyContent is set: " + mJustifyContent);
        }
        spaceBetweenItem = Math.max(spaceBetweenItem, 0);

        for (int i = 0; i < flexLine.itemCount; i++) {
            View child = getReorderedChildAt(currentViewIndex);
            if (child == null) {
                continue;
            } else if (child.getVisibility() == View.GONE) {
                currentViewIndex++;
                continue;
            }
            LayoutParams lp = ((LayoutParams) child.getLayoutParams());
            childLeft += lp.leftMargin;
            childRight -= lp.rightMargin;
            if (mFlexWrap == FLEX_WRAP_WRAP_REVERSE) {
                if (isRtl) {
                    layoutSingleChildHorizontal(child, flexLine, mFlexWrap, mAlignItems,
                            Math.round(childRight) - child.getMeasuredWidth(),
                            childBottom - child.getMeasuredHeight(), Math.round(childRight), childBottom);
                } else {
                    layoutSingleChildHorizontal(child, flexLine, mFlexWrap, mAlignItems, Math.round(childLeft),
                            childBottom - child.getMeasuredHeight(),
                            Math.round(childLeft) + child.getMeasuredWidth(), childBottom);
                }
            } else {
                if (isRtl) {
                    layoutSingleChildHorizontal(child, flexLine, mFlexWrap, mAlignItems,
                            Math.round(childRight) - child.getMeasuredWidth(), childTop, Math.round(childRight),
                            childTop + child.getMeasuredHeight());
                } else {
                    layoutSingleChildHorizontal(child, flexLine, mFlexWrap, mAlignItems, Math.round(childLeft),
                            childTop, Math.round(childLeft) + child.getMeasuredWidth(),
                            childTop + child.getMeasuredHeight());
                }
            }
            childLeft += child.getMeasuredWidth() + spaceBetweenItem + lp.rightMargin;
            childRight -= child.getMeasuredWidth() + spaceBetweenItem + lp.leftMargin;
            currentViewIndex++;
        }
        childTop += flexLine.crossSize;
        childBottom -= flexLine.crossSize;
    }
}

From source file:com.cxsplay.wallyskim.widget.flexbox.FlexboxLayout.java

/**
 * Checks if the view's width/height don't violate the minimum/maximum size constraints imposed
 * by the {@link LayoutParams#minWidth}, {@link LayoutParams#minHeight},
 * {@link LayoutParams#maxWidth} and {@link LayoutParams#maxHeight} attributes.
 *
 * @param view the view to be checked/*from www  .  j a  v a  2 s . co  m*/
 */
private void checkSizeConstraints(View view) {
    boolean needsMeasure = false;
    LayoutParams lp = (LayoutParams) view.getLayoutParams();
    int childWidth = view.getMeasuredWidth();
    int childHeight = view.getMeasuredHeight();

    if (view.getMeasuredWidth() < lp.minWidth) {
        needsMeasure = true;
        childWidth = lp.minWidth;
    } else if (view.getMeasuredWidth() > lp.maxWidth) {
        needsMeasure = true;
        childWidth = lp.maxWidth;
    }

    if (childHeight < lp.minHeight) {
        needsMeasure = true;
        childHeight = lp.minHeight;
    } else if (childHeight > lp.maxHeight) {
        needsMeasure = true;
        childHeight = lp.maxHeight;
    }
    if (needsMeasure) {
        view.measure(MeasureSpec.makeMeasureSpec(childWidth, MeasureSpec.EXACTLY),
                MeasureSpec.makeMeasureSpec(childHeight, MeasureSpec.EXACTLY));
    }
}

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

private int getMeasurement(View c, boolean horizontal) {
    return horizontal ? c.getMeasuredWidth() : c.getMeasuredHeight();
}

From source file:com.android.launcher2.AsyncTaskCallback.java

@Override
protected void screenScrolled(int screenCenter) {
    super.screenScrolled(screenCenter);

    for (int i = 0; i < getChildCount(); i++) {
        View v = getPageAt(i);
        if (v != null) {
            float scrollProgress = getScrollProgress(screenCenter, v, i);

            float interpolatedProgress = mZInterpolator.getInterpolation(Math.abs(Math.min(scrollProgress, 0)));
            float scale = (1 - interpolatedProgress) + interpolatedProgress * TRANSITION_SCALE_FACTOR;
            float translationX = Math.min(0, scrollProgress) * v.getMeasuredWidth();

            float alpha;

            if (scrollProgress < 0) {
                alpha = scrollProgress < 0 ? mAlphaInterpolator.getInterpolation(1 - Math.abs(scrollProgress))
                        : 1.0f;/*from w ww  .j  ava 2  s  .c o  m*/
            } else {
                // On large screens we need to fade the page as it nears its leftmost position
                alpha = mLeftScreenAlphaInterpolator.getInterpolation(1 - scrollProgress);
            }

            v.setCameraDistance(mDensity * CAMERA_DISTANCE);
            int pageWidth = v.getMeasuredWidth();
            int pageHeight = v.getMeasuredHeight();

            if (PERFORM_OVERSCROLL_ROTATION) {
                if (i == 0 && scrollProgress < 0) {
                    // Overscroll to the left
                    v.setPivotX(TRANSITION_PIVOT * pageWidth);
                    v.setRotationY(-TRANSITION_MAX_ROTATION * scrollProgress);
                    scale = 1.0f;
                    alpha = 1.0f;
                    // On the first page, we don't want the page to have any lateral motion
                    translationX = 0;
                } else if (i == getChildCount() - 1 && scrollProgress > 0) {
                    // Overscroll to the right
                    v.setPivotX((1 - TRANSITION_PIVOT) * pageWidth);
                    v.setRotationY(-TRANSITION_MAX_ROTATION * scrollProgress);
                    scale = 1.0f;
                    alpha = 1.0f;
                    // On the last page, we don't want the page to have any lateral motion.
                    translationX = 0;
                } else {
                    v.setPivotY(pageHeight / 2.0f);
                    v.setPivotX(pageWidth / 2.0f);
                    v.setRotationY(0f);
                }
            }

            v.setTranslationX(translationX);
            v.setScaleX(scale);
            v.setScaleY(scale);
            v.setAlpha(alpha);

            // If the view has 0 alpha, we set it to be invisible so as to prevent
            // it from accepting touches
            if (alpha == 0) {
                v.setVisibility(INVISIBLE);
            } else if (v.getVisibility() != VISIBLE) {
                v.setVisibility(VISIBLE);
            }
        }
    }
}

From source file:com.android.launcher2.PagedView.java

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    if (!mIsDataReady) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        return;//from ww w .j a v a2  s  .c  o  m
    }

    final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
    final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
    final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
    int heightSize = MeasureSpec.getSize(heightMeasureSpec);
    if (widthMode != MeasureSpec.EXACTLY) {
        throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
    }

    // 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.
     */
    int maxChildHeight = 0;

    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);
    final int childCount = getChildCount();
    for (int i = 0; i < childCount; i++) {
        // disallowing padding in paged view (just pass 0)
        final View child = getPageAt(i);
        final LayoutParams lp = (LayoutParams) child.getLayoutParams();

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

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

        final int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(widthSize - horizontalPadding,
                childWidthMode);
        final int childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(heightSize - verticalPadding,
                childHeightMode);

        child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
        maxChildHeight = Math.max(maxChildHeight, child.getMeasuredHeight());
        if (DEBUG)
            Log.d(TAG,
                    "\tmeasure-child" + i + ": " + child.getMeasuredWidth() + ", " + child.getMeasuredHeight());
    }

    if (heightMode == MeasureSpec.AT_MOST) {
        heightSize = maxChildHeight + verticalPadding;
    }

    setMeasuredDimension(widthSize, heightSize);

    // We can't call getChildOffset/getRelativeChildOffset until we set the measured dimensions.
    // We also wait until we set the measured dimensions before flushing the cache as well, to
    // ensure that the cache is filled with good values.
    invalidateCachedOffsets();

    if (childCount > 0) {
        if (DEBUG)
            Log.d(TAG, "getRelativeChildOffset(): " + getMeasuredWidth() + ", " + getChildWidth(0));

        // Calculate the variable page spacing if necessary
        if (mPageSpacing == AUTOMATIC_PAGE_SPACING) {
            // 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 = getRelativeChildOffset(0);
            int spacing = Math.max(offset, widthSize - offset - getChildAt(0).getMeasuredWidth());
            setPageSpacing(spacing);
        }
    }

    updateScrollingIndicatorPosition();

    if (childCount > 0) {
        mMaxScrollX = getChildOffset(childCount - 1) - getRelativeChildOffset(childCount - 1);
    } else {
        mMaxScrollX = 0;
    }
}

From source file:com.app.afteryou.ui.staggered.StaggeredGridView.java

private void performAnimation() {
    if (!mViewCacheForAnim.isEmpty()) {
        isAnimating = true;//  w w w  . jav a 2s.c  om
        int count = mViewCacheForAnim.size();
        final View view = mViewCacheForAnim.remove(0);
        Rect rect = new Rect();
        this.getGlobalVisibleRect(rect);
        int fly_distance = (int) (rect.height());
        float top = view.getY();
        float bottom = view.getY() + view.getMeasuredHeight();
        float startTop = top + fly_distance;
        float startBottom = bottom + fly_distance;
        int bufferDis = rect.height() / 4;

        if (startBottom < 0) {
            Log.d(TAG, "No animation as the view is passed!!");
            view.setVisibility(View.VISIBLE);
            performAnimation();
            return;
        } else if (startTop < rect.height()) {
            fly_distance += (rect.height() - startTop + bufferDis);
        }
        if (fly_distance > rect.height() * 2) {
            Log.d(TAG, "No animation as the fly distance is too long!");
            view.setVisibility(View.VISIBLE);
            performAnimation();
            return;
        } else if (fly_distance < rect.height()) {
            fly_distance = rect.height();
        }

        view.setTranslationY(fly_distance);
        long delay = FLY_IN_DELAY / count;
        long duration = delay > FLY_IN_DURATION ? FLY_IN_DURATION : delay;
        view.animate().setDuration(duration).setInterpolator(new DecelerateInterpolator())
                .setListener(new AnimatorListenerAdapter() {

                    public void onAnimationStart(Animator anim) {
                        view.setVisibility(View.VISIBLE);
                    }

                    @Override
                    public void onAnimationEnd(Animator anim) {
                        view.clearAnimation();
                        view.setTranslationY(0f);
                        performAnimation();
                    }
                }).translationY(0);
    } else {
        isAnimating = false;
    }
}

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

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    // Sets up mListPadding
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    int widthMode = MeasureSpec.getMode(widthMeasureSpec);
    int heightMode = MeasureSpec.getMode(heightMeasureSpec);
    int widthSize = MeasureSpec.getSize(widthMeasureSpec);
    int heightSize = MeasureSpec.getSize(heightMeasureSpec);

    if (widthMode == MeasureSpec.UNSPECIFIED) {
        if (mColumnWidth > 0) {
            widthSize = mColumnWidth + mListPadding.left + mListPadding.right;
        } else {/*w ww.  j av a 2  s . co  m*/
            widthSize = mListPadding.left + mListPadding.right;
        }
        widthSize += getVerticalScrollbarWidth();
    }

    int childWidth = widthSize - mListPadding.left - mListPadding.right;
    boolean didNotInitiallyFit = determineColumns(childWidth);

    int childHeight = 0;
    int childState = 0;

    mItemCount = mAdapter == null ? 0 : mAdapter.getCount();
    final int count = mItemCount;
    if (count > 0) {
        final View child = obtainView(0, mIsScrap);

        LayoutParams p = (LayoutParams) child.getLayoutParams();
        if (p == null) {
            p = (LayoutParams) generateDefaultLayoutParams();
            child.setLayoutParams(p);
        }
        p.viewType = mAdapter.getItemViewType(0);
        p.forceAdd = true;

        int childHeightSpec = getChildMeasureSpec(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), 0,
                p.height);
        int childWidthSpec = getChildMeasureSpec(MeasureSpec.makeMeasureSpec(mColumnWidth, MeasureSpec.EXACTLY),
                0, p.width);
        child.measure(childWidthSpec, childHeightSpec);

        childHeight = child.getMeasuredHeight();
        childState = Compat.combineMeasuredStates(childState, Compat.getMeasuredState(child));

        if (mRecycler.shouldRecycleViewType(p.viewType)) {
            mRecycler.addScrapView(child, -1);
        }
    }

    if (heightMode == MeasureSpec.UNSPECIFIED) {
        heightSize = mListPadding.top + mListPadding.bottom + childHeight + getVerticalFadingEdgeLength() * 2;
    }

    if (heightMode == MeasureSpec.AT_MOST) {
        int ourSize = mListPadding.top + mListPadding.bottom;

        final int numColumns = mNumColumns;
        for (int i = 0; i < count; i += numColumns) {
            ourSize += childHeight;
            if (i + numColumns < count) {
                ourSize += mVerticalSpacing;
            }
            if (ourSize >= heightSize) {
                ourSize = heightSize;
                break;
            }
        }
        heightSize = ourSize;
    }

    if (widthMode == MeasureSpec.AT_MOST && mRequestedNumColumns != AUTO_FIT) {
        int ourSize = (mRequestedNumColumns * mColumnWidth) + ((mRequestedNumColumns - 1) * mHorizontalSpacing)
                + mListPadding.left + mListPadding.right;
        if (ourSize > widthSize || didNotInitiallyFit) {
            widthSize |= MEASURED_STATE_TOO_SMALL;
        }
    }

    setMeasuredDimension(widthSize, heightSize);
    mWidthMeasureSpec = widthMeasureSpec;
}

From source file:administrator.example.com.myscrollview.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 w ww  .j  a  v a  2 s  .c  om*/
 *
 * @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_HORIZONTAL:
                childTop = Math.max((height - child.getMeasuredHeight()) / 2, paddingTop);
                break;
            case Gravity.BOTTOM:
                childTop = height - paddingBottom - child.getMeasuredHeight();
                paddingBottom += child.getMeasuredHeight();
                break;
            } /* end of switch */
            childTop += scrollY;

            final int childOffset = childTop - child.getTop();
            if (childOffset != 0) {
                child.offsetTopAndBottom(childOffset);
            } /* end of if */
        } /* end of for */
    } /* end of for */

    if (mOnPageChangeListener != null) {
        mOnPageChangeListener.onPageScrolled(position, offset, offsetPixels);
    } /* end of if */
    if (mInternalPageChangeListener != null) {
        mInternalPageChangeListener.onPageScrolled(position, offset, offsetPixels);
    } /* end of if */
    mCalledSuper = true;
}