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.awrtechnologies.carbudgetsales.hlistview.widget.HListView.java

/**
 * Add a view as a child and make sure it is measured (if necessary) and positioned properly.
 * /*  w  w  w  .jav  a2s.  c  om*/
 * @param child
 *           The view to add
 * @param position
 *           The position of this child
 * @param x
 *           The x position relative to which this view will be positioned
 * @param flowDown
 *           If true, align left edge to x. If false, align right edge to x.
 * @param childrenTop
 *           Top 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 x, boolean flowDown, int childrenTop, 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
    AbsHListView.LayoutParams p = (AbsHListView.LayoutParams) child.getLayoutParams();
    if (p == null) {
        p = (AbsHListView.LayoutParams) generateDefaultLayoutParams();
    }
    p.viewType = mAdapter.getItemViewType(position);

    if ((recycled && !p.forceAdd)
            || (p.recycledHeaderFooter && p.viewType == AdapterView.ITEM_VIEW_TYPE_HEADER_OR_FOOTER)) {
        attachViewToParent(child, flowDown ? -1 : 0, p);
    } else {
        p.forceAdd = false;
        if (p.viewType == 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 != AbsListView.CHOICE_MODE_NONE && mCheckStates != null) {
        if (child instanceof Checkable) {
            ((Checkable) child).setChecked(mCheckStates.get(position, false));
        } else if (android.os.Build.VERSION.SDK_INT >= 11) {
            child.setActivated(mCheckStates.get(position, false));
        }
    }

    if (needToMeasure) {
        int childHeightSpec = ViewGroup.getChildMeasureSpec(mHeightMeasureSpec,
                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);
    } else {
        cleanupLayoutState(child);
    }

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

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

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

    if (android.os.Build.VERSION.SDK_INT >= 11) {
        if (recycled
                && (((AbsHListView.LayoutParams) child.getLayoutParams()).scrappedFromPosition) != position) {
            child.jumpDrawablesToCurrentState();
        }
    }
}

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

/**
 * Add a view as a child and make sure it is measured (if necessary) and
 * positioned properly./*from  w ww .  java  2  s  .c  o 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.
 */
private void setupChild(final View child, final int position, final int y, final boolean flowDown,
        final int childrenLeft, final boolean selected, final 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
    PLAAbsListView.LayoutParams p = (PLAAbsListView.LayoutParams) child.getLayoutParams();
    if (p == null) {
        p = new PLAAbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT, 0);
    }
    p.viewType = mAdapter.getItemViewType(position);

    if (recycled && !p.forceAdd
            || p.recycledHeaderFooter && p.viewType == PLAAdapterView.ITEM_VIEW_TYPE_HEADER_OR_FOOTER) {
        attachViewToParent(child, flowDown ? -1 : 0, p);
    } else {
        p.forceAdd = false;
        if (p.viewType == PLAAdapterView.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);
    }

    final boolean useActivated = getContext()
            .getApplicationInfo().targetSdkVersion >= android.os.Build.VERSION_CODES.HONEYCOMB;

    if (mChoiceMode == CHOICE_MODE_NONE) {
        if (child instanceof Checkable) {
            ((Checkable) child).setChecked(false);
        } else if (useActivated) {
            child.setActivated(false);
        }
    }
    if (mChoiceMode != CHOICE_MODE_NONE && mCheckStates != null) {
        if (child instanceof Checkable) {
            ((Checkable) child).setChecked(mCheckStates.get(position));
        } else if (useActivated) {
            child.setActivated(mCheckStates.get(position));
        }
    }

    if (needToMeasure) {
        final int childWidthSpec = ViewGroup.getChildMeasureSpec(mWidthMeasureSpec,
                mListPadding.left + mListPadding.right, p.width);
        final 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);
    }
}

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

/**
     * Measure a particular list child.//from   w  w  w.  ja  v a  2 s .  c  om
     * TODO: unify with setUpChild.
     * @param child The child.
     */
    private void measureItem(View child) {
        ViewGroup.LayoutParams p = child.getLayoutParams();
        if (p == null) {
            p = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.FILL_PARENT);
        }

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

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

/**
 * Measure a particular list child./*from  w ww.ja  v a 2s . co  m*/
 * TODO: unify with setUpChild.
 * @param child The child.
 */
private void measureItem(View child) {
    ViewGroup.LayoutParams p = child.getLayoutParams();
    if (p == null) {
        p = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
    }

    int childHeightSpec = ViewGroup.getChildMeasureSpec(mHeightMeasureSpec,
            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.appunite.list.ListView.java

/**
 * Measure a particular list child./* w w  w  . j av a 2  s  .c o  m*/
 * TODO: unify with setUpChild.
 * @param child The child.
 */
private void measureItem(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(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);
    }
    child.measure(childWidthSpec, childHeightSpec);
}

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

/**
 * Measure a particular list child. TODO: unify with setUpChild.
 *
 * @param child//from w w  w  .  ja  v a 2s  . co  m
 *           The child.
 */
private void measureItem(View child) {
    ViewGroup.LayoutParams p = child.getLayoutParams();
    if (p == null) {
        p = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
    }

    int childHeightSpec = ViewGroup.getChildMeasureSpec(mHeightMeasureSpec,
            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

/**
 * Measure a particular list child. TODO: unify with setUpChild.
 * /*from   www .j a v a  2  s .com*/
 * @param child
 *           The child.
 */
private void measureItem(View child) {
    ViewGroup.LayoutParams p = child.getLayoutParams();
    if (p == null) {
        p = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
    }

    int childHeightSpec = ViewGroup.getChildMeasureSpec(mHeightMeasureSpec,
            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);
}