Example usage for android.view ViewGroup getChildMeasureSpec

List of usage examples for android.view ViewGroup getChildMeasureSpec

Introduction

In this page you can find the example usage for android.view ViewGroup getChildMeasureSpec.

Prototype

public static int getChildMeasureSpec(int spec, int padding, int childDimension) 

Source Link

Document

Does the hard part of measureChildren: figuring out the MeasureSpec to pass to a particular child.

Usage

From source file:com.example.androidannotationtesttwo.widget.swiptlistview.SwipeListView.java

/**
 * measure header layout//from ww w  .j  a v  a  2 s.  c  o m
 * 
 * @param child
 */
private void measureHeaderLayout(View child) {
    ViewGroup.LayoutParams p = child.getLayoutParams();
    if (p == null) {
        p = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
    }

    int childWidthSpec = ViewGroup.getChildMeasureSpec(0, 0, p.width);
    int lpHeight = p.height;
    int childHeightSpec;
    if (lpHeight > 0) {
        childHeightSpec = MeasureSpec.makeMeasureSpec(lpHeight, MeasureSpec.EXACTLY);
    } else {
        childHeightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
    }
    child.measure(childWidthSpec, childHeightSpec);
}

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

private void measureScrapChild(View child, int position, int widthMeasureSpec) {
    LayoutParams p = (LayoutParams) child.getLayoutParams();
    if (p == null) {
        p = (AbsListView.LayoutParams) generateDefaultLayoutParams();
        child.setLayoutParams(p);/*from  www . j  av a2s  .  com*/
    }
    p.viewType = mAdapter.getItemViewType(position);
    p.forceAdd = true;

    int childWidthSpec = ViewGroup.getChildMeasureSpec(widthMeasureSpec, mListPadding.left + mListPadding.right,
            p.width);
    int lpHeight = p.height;
    int childHeightSpec;
    if (lpHeight > 0) {
        childHeightSpec = MeasureSpec.makeMeasureSpec(lpHeight, MeasureSpec.EXACTLY);
    } else {
        childHeightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
    }
    child.measure(childWidthSpec, childHeightSpec);
}

From source file:org.bangbang.support.v4.widget.HListView.java

private void measureScrapChild(View child, int position, int heightMeasureSpec) {
        LayoutParams p = (LayoutParams) child.getLayoutParams();
        if (p == null) {
            p = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.FILL_PARENT, 0);
            child.setLayoutParams(p);/* ww  w  .j  av  a2s .co  m*/
        }
        p.viewType = mAdapter.getItemViewType(position);

        int childheightSpec = ViewGroup.getChildMeasureSpec(heightMeasureSpec,
                mListPadding.top + mListPadding.bottom, p.height);
        int lpWidth = p.width;
        int childWidthSpec;
        if (lpWidth > 0) {
            childWidthSpec = MeasureSpec.makeMeasureSpec(lpWidth, MeasureSpec.EXACTLY);
        } else {
            childWidthSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
        }
        child.measure(childWidthSpec, childheightSpec);
    }

From source file:com.awrtechnologies.valor.valorfireplace.hlistview.widget.HListView.java

private void measureScrapChildWidth(View child, int position, int heightMeasureSpec) {
    LayoutParams p = (LayoutParams) child.getLayoutParams();
    if (p == null) {
        p = (AbsHListView.LayoutParams) generateDefaultLayoutParams();
        child.setLayoutParams(p);/*from   w  w  w.  ja  v  a 2s. c o m*/
    }
    p.viewType = mAdapter.getItemViewType(position);
    p.forceAdd = true;

    int childHeightSpec = ViewGroup.getChildMeasureSpec(heightMeasureSpec,
            mListPadding.top + mListPadding.bottom, p.height);
    int lpWidth = p.width;
    int childWidthSpec;
    if (lpWidth > 0) {
        childWidthSpec = View.MeasureSpec.makeMeasureSpec(lpWidth, View.MeasureSpec.EXACTLY);
    } else {
        childWidthSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
    }
    child.measure(childWidthSpec, childHeightSpec);
}

From source file:com.awrtechnologies.carbudgetsales.hlistview.widget.HListView.java

private void measureScrapChildWidth(View child, int position, int heightMeasureSpec) {
    LayoutParams p = (LayoutParams) child.getLayoutParams();
    if (p == null) {
        p = (AbsHListView.LayoutParams) generateDefaultLayoutParams();
        child.setLayoutParams(p);//  ww  w.  ja v  a  2  s . c  o m
    }
    p.viewType = mAdapter.getItemViewType(position);
    p.forceAdd = true;

    int childHeightSpec = ViewGroup.getChildMeasureSpec(heightMeasureSpec,
            mListPadding.top + mListPadding.bottom, p.height);
    int lpWidth = p.width;
    int childWidthSpec;
    if (lpWidth > 0) {
        childWidthSpec = MeasureSpec.makeMeasureSpec(lpWidth, MeasureSpec.EXACTLY);
    } else {
        childWidthSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
    }
    child.measure(childWidthSpec, childHeightSpec);
}

From source file:io.github.clendy.leanback.widget.GridLayoutManager.java

private void measureChild(View child) {
    if (TRACE)/*  ww  w  .j a v  a2  s  .  co  m*/
        TraceHelper.beginSection("measureChild");
    final ViewGroup.LayoutParams lp = child.getLayoutParams();
    final int secondarySpec = (mRowSizeSecondaryRequested == ViewGroup.LayoutParams.WRAP_CONTENT)
            ? MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)
            : MeasureSpec.makeMeasureSpec(mFixedRowSizeSecondary, MeasureSpec.EXACTLY);
    int widthSpec, heightSpec;

    if (mOrientation == HORIZONTAL) {
        widthSpec = ViewGroup.getChildMeasureSpec(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), 0,
                lp.width);
        heightSpec = ViewGroup.getChildMeasureSpec(secondarySpec, 0, lp.height);
    } else {
        heightSpec = ViewGroup.getChildMeasureSpec(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), 0,
                lp.height);
        widthSpec = ViewGroup.getChildMeasureSpec(secondarySpec, 0, lp.width);
    }
    child.measure(widthSpec, heightSpec);
    if (DEBUG)
        Log.v(getTag(),
                "measureChild secondarySpec " + Integer.toHexString(secondarySpec) + " widthSpec "
                        + Integer.toHexString(widthSpec) + " heightSpec " + Integer.toHexString(heightSpec)
                        + " measuredWidth " + child.getMeasuredWidth() + " measuredHeight "
                        + child.getMeasuredHeight());
    if (DEBUG)
        Log.v(getTag(), "child lp width " + lp.width + " height " + lp.height);
    if (TRACE)
        TraceHelper.endSection();
}

From source file:com.rbware.github.androidcouchpotato.widget.GridLayoutManager.java

void measureChild(View child) {
    if (TRACE)// w  ww .j a va2s. co m
        TraceHelper.beginSection("measureChild");
    final LayoutParams lp = (LayoutParams) child.getLayoutParams();
    calculateItemDecorationsForChild(child, sTempRect);
    int widthUsed = lp.leftMargin + lp.rightMargin + sTempRect.left + sTempRect.right;
    int heightUsed = lp.topMargin + lp.bottomMargin + sTempRect.top + sTempRect.bottom;

    final int secondarySpec = (mRowSizeSecondaryRequested == ViewGroup.LayoutParams.WRAP_CONTENT)
            ? MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)
            : MeasureSpec.makeMeasureSpec(mFixedRowSizeSecondary, MeasureSpec.EXACTLY);
    int widthSpec, heightSpec;

    if (mOrientation == HORIZONTAL) {
        widthSpec = ViewGroup.getChildMeasureSpec(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
                widthUsed, lp.width);
        heightSpec = ViewGroup.getChildMeasureSpec(secondarySpec, heightUsed, lp.height);
    } else {
        heightSpec = ViewGroup.getChildMeasureSpec(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
                heightUsed, lp.height);
        widthSpec = ViewGroup.getChildMeasureSpec(secondarySpec, widthUsed, lp.width);
    }
    child.measure(widthSpec, heightSpec);
    if (DEBUG)
        Log.v(getTag(),
                "measureChild secondarySpec " + Integer.toHexString(secondarySpec) + " widthSpec "
                        + Integer.toHexString(widthSpec) + " heightSpec " + Integer.toHexString(heightSpec)
                        + " measuredWidth " + child.getMeasuredWidth() + " measuredHeight "
                        + child.getMeasuredHeight());
    if (DEBUG)
        Log.v(getTag(), "child lp width " + lp.width + " height " + lp.height);
    if (TRACE)
        TraceHelper.endSection();
}

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./*from  w  w  w.ja v  a2s. c  o  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:android.support.v17.leanback.widget.GridLayoutManager.java

private void measureChild(View child) {
    if (TRACE)/*ww w  .ja v  a 2s .  c  o  m*/
        TraceHelper.beginSection("measureChild");
    final LayoutParams lp = (LayoutParams) child.getLayoutParams();
    calculateItemDecorationsForChild(child, sTempRect);
    int widthUsed = lp.leftMargin + lp.rightMargin + sTempRect.left + sTempRect.right;
    int heightUsed = lp.topMargin + lp.bottomMargin + sTempRect.top + sTempRect.bottom;

    final int secondarySpec = (mRowSizeSecondaryRequested == ViewGroup.LayoutParams.WRAP_CONTENT)
            ? MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)
            : MeasureSpec.makeMeasureSpec(mFixedRowSizeSecondary, MeasureSpec.EXACTLY);
    int widthSpec, heightSpec;

    if (mOrientation == HORIZONTAL) {
        widthSpec = ViewGroup.getChildMeasureSpec(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
                widthUsed, lp.width);
        heightSpec = ViewGroup.getChildMeasureSpec(secondarySpec, heightUsed, lp.height);
    } else {
        heightSpec = ViewGroup.getChildMeasureSpec(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
                heightUsed, lp.height);
        widthSpec = ViewGroup.getChildMeasureSpec(secondarySpec, widthUsed, lp.width);
    }
    child.measure(widthSpec, heightSpec);
    if (DEBUG)
        Log.v(getTag(),
                "measureChild secondarySpec " + Integer.toHexString(secondarySpec) + " widthSpec "
                        + Integer.toHexString(widthSpec) + " heightSpec " + Integer.toHexString(heightSpec)
                        + " measuredWidth " + child.getMeasuredWidth() + " measuredHeight "
                        + child.getMeasuredHeight());
    if (DEBUG)
        Log.v(getTag(), "child lp width " + lp.width + " height " + lp.height);
    if (TRACE)
        TraceHelper.endSection();
}

From source file:com.huewu.pla.lib.internal.PLA_ListView.java

/**
 * Add a view as a child and make sure it is measured (if necessary) and
 * positioned properly.//from w  ww.  j  ava2  s. co m
 * 
 * @param child
 *            The view to add
 * @param position
 *            The position of this child
 * @param y
 *            The y position relative to which this view will be positioned
 * @param flowDown
 *            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.
 */
@TargetApi(11)
private void setupChild(View child, int position, int y, boolean flowDown, int childrenLeft, boolean selected,
        boolean recycled) {

    final 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();
    final boolean needToMeasure = !recycled || updateChildSelected || child.isLayoutRequested();

    // Respect layout params that are already in the view. Otherwise make
    // some up...
    // noinspection unchecked
    PLA_AbsListView.LayoutParams p = (PLA_AbsListView.LayoutParams) child.getLayoutParams();
    if (p == null) {
        p = new PLA_AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT, 0);
    }
    p.viewType = mAdapter.getItemViewType(position);

    if ((recycled && !p.forceAdd)
            || (p.recycledHeaderFooter && p.viewType == PLA_AdapterView.ITEM_VIEW_TYPE_HEADER_OR_FOOTER)) {
        attachViewToParent(child, flowDown ? -1 : 0, p);
    } else {
        p.forceAdd = false;
        if (p.viewType == PLA_AdapterView.ITEM_VIEW_TYPE_HEADER_OR_FOOTER) {
            p.recycledHeaderFooter = true;
        }
        addViewInLayout(child, flowDown ? -1 : 0, p, true);
    }

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

    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
                && Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            child.setActivated(mCheckStates.get(position));
        }
    }

    if (needToMeasure) {
        int childWidthSpec = ViewGroup.getChildMeasureSpec(mWidthMeasureSpec,
                mListPadding.left + mListPadding.right, p.width);
        int lpHeight = p.height;
        int childHeightSpec;
        if (lpHeight > 0) {
            childHeightSpec = MeasureSpec.makeMeasureSpec(lpHeight, MeasureSpec.EXACTLY);
        } else {
            childHeightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
        }

        onMeasureChild(child, position, childWidthSpec, childHeightSpec);
        // child.measure(childWidthSpec, childHeightSpec);
    } else {
        cleanupLayoutState(child);
    }

    final int w = child.getMeasuredWidth();
    final int h = child.getMeasuredHeight();
    final int childTop = flowDown ? y : y - h;

    if (needToMeasure) {
        final int childRight = childrenLeft + w;
        final int childBottom = childTop + h;
        // child.layout(childrenLeft, childTop, childRight, childBottom);
        onLayoutChild(child, position, childrenLeft, childTop, childRight, childBottom);
    } else {
        final int offsetLeft = childrenLeft - child.getLeft();
        final int offsetTop = childTop - child.getTop();
        onOffsetChild(child, position, offsetLeft, offsetTop);
    }

    if (mCachingStarted && !child.isDrawingCacheEnabled()) {
        child.setDrawingCacheEnabled(true);
    }
}