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:org.kymjs.aframe.ui.widget.HorizontalListView.java

private void measureChild(View child) {
    ViewGroup.LayoutParams childParams = getLayoutParams(child);
    int childHeightSpec = ViewGroup.getChildMeasureSpec(mHeightMeasureSpec,
            getPaddingTop() + getPaddingBottom(), childParams.height);
    int childWidthSpec;
    if (childParams.width > 0) {
        childWidthSpec = MeasureSpec.makeMeasureSpec(childParams.width, MeasureSpec.EXACTLY);
    } else {/*w w w  . j a va2  s.  c  o m*/
        childWidthSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
    }
    child.measure(childWidthSpec, childHeightSpec);
}

From source file:com.tr.ui.widgets.HorizontalListView.java

private void addAndMeasureChild(final View child, int viewPos) {
    LayoutParams params = (LayoutParams) child.getLayoutParams();
    if (params == null) {
        params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
    }/* ww  w.j  av  a 2 s  .  com*/

    addViewInLayout(child, viewPos, params, true);

    int heightMeasureSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY);
    int childHeightSpec = ViewGroup.getChildMeasureSpec(heightMeasureSpec, getPaddingTop() + getPaddingBottom(),
            params.height);
    int childWidthSpec = 0;
    if (params.width == LayoutParams.MATCH_PARENT) {
        childWidthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY);
    } else if (params.width == LayoutParams.WRAP_CONTENT) {
        childWidthSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
    } else {
        childWidthSpec = MeasureSpec.makeMeasureSpec(params.width, MeasureSpec.EXACTLY);
    }
    child.measure(childWidthSpec, childHeightSpec);

}

From source file:com.flexible.flexibleadapter.helpers.StickyHeaderHelper.java

/**
 * Gets the header view for the associated header position. If it doesn't exist yet, it will
 * be created, measured, and laid out./*from  w  ww.j a  va 2 s  .  c om*/
 *
 * @param position the adapter position to get the header view
 * @return ViewHolder of type FlexibleViewHolder of the associated header position
 */
@SuppressWarnings("unchecked")
private FlexibleViewHolder getHeaderViewHolder(int position) {
    // Find existing ViewHolder
    FlexibleViewHolder holder = (FlexibleViewHolder) mRecyclerView.findViewHolderForAdapterPosition(position);
    if (holder == null) {
        // Create and binds a new ViewHolder
        holder = (FlexibleViewHolder) mAdapter.createViewHolder(mRecyclerView,
                mAdapter.getItemViewType(position));
        mAdapter.bindViewHolder(holder, position);

        // Restore the Adapter position
        holder.setBackupPosition(position);

        // Calculate width and height
        int widthSpec;
        int heightSpec;
        if (Utils.getOrientation(mRecyclerView.getLayoutManager()) == OrientationHelper.VERTICAL) {
            widthSpec = View.MeasureSpec.makeMeasureSpec(mRecyclerView.getWidth(), View.MeasureSpec.EXACTLY);
            heightSpec = View.MeasureSpec.makeMeasureSpec(mRecyclerView.getHeight(),
                    View.MeasureSpec.UNSPECIFIED);
        } else {
            widthSpec = View.MeasureSpec.makeMeasureSpec(mRecyclerView.getWidth(),
                    View.MeasureSpec.UNSPECIFIED);
            heightSpec = View.MeasureSpec.makeMeasureSpec(mRecyclerView.getHeight(), View.MeasureSpec.EXACTLY);
        }

        // Measure and Layout the stickyView
        final View headerView = holder.getContentView();
        int childWidth = ViewGroup.getChildMeasureSpec(widthSpec,
                mRecyclerView.getPaddingLeft() + mRecyclerView.getPaddingRight(),
                headerView.getLayoutParams().width);
        int childHeight = ViewGroup.getChildMeasureSpec(heightSpec,
                mRecyclerView.getPaddingTop() + mRecyclerView.getPaddingBottom(),
                headerView.getLayoutParams().height);

        headerView.measure(childWidth, childHeight);
        headerView.layout(0, 0, headerView.getMeasuredWidth(), headerView.getMeasuredHeight());
    }
    return holder;
}

From source file:edu.ptu.recyclerviewdemo.stickheader.StickyHeaderHelper.java

/**
 * Gets the header view for the associated header position. If it doesn't exist yet, it will
 * be created, measured, and laid out.//w w  w  .  ja  va2 s .c o m
 *
 * @param position the adapter position to get the header view
 * @return ViewHolder of type ViewHolder of the associated header position
 */
@SuppressWarnings("unchecked")
private HeaderViewHolder getHeaderViewHolder(int position) {
    // Find existing ViewHolder
    HeaderViewHolder holder = (HeaderViewHolder) mRecyclerView.findViewHolderForAdapterPosition(position);
    if (holder == null) {
        // Create and binds a new ViewHolder
        holder = (HeaderViewHolder) mRecyclerView.getAdapter().createViewHolder(mRecyclerView,
                mAdapter.getItemViewType(position - getHeaderCount()));
        mRecyclerView.getAdapter().bindViewHolder(holder, position);
        // Restore the Adapter position
        holder.setBackupPosition(position);

        // Calculate width and height
        int widthSpec;
        int heightSpec;
        if (Utils.getOrientation(mRecyclerView.getLayoutManager()) == OrientationHelper.VERTICAL) {
            widthSpec = View.MeasureSpec.makeMeasureSpec(mRecyclerView.getWidth(), View.MeasureSpec.EXACTLY);
            heightSpec = View.MeasureSpec.makeMeasureSpec(mRecyclerView.getHeight(),
                    View.MeasureSpec.UNSPECIFIED);
        } else {
            widthSpec = View.MeasureSpec.makeMeasureSpec(mRecyclerView.getWidth(),
                    View.MeasureSpec.UNSPECIFIED);
            heightSpec = View.MeasureSpec.makeMeasureSpec(mRecyclerView.getHeight(), View.MeasureSpec.EXACTLY);
        }

        // Measure and Layout the stickyView
        final View headerView = holder.getContentView();
        int childWidth = ViewGroup.getChildMeasureSpec(widthSpec,
                mRecyclerView.getPaddingLeft() + mRecyclerView.getPaddingRight(),
                headerView.getLayoutParams().width);
        int childHeight = ViewGroup.getChildMeasureSpec(heightSpec,
                mRecyclerView.getPaddingTop() + mRecyclerView.getPaddingBottom(),
                headerView.getLayoutParams().height);

        headerView.measure(childWidth, childHeight);
        headerView.layout(0, 0, headerView.getMeasuredWidth(), headerView.getMeasuredHeight());
    }
    System.out.println("===> position " + position);
    mRecyclerView.getAdapter().bindViewHolder(holder, position);
    return holder;
}

From source file:com.example.stickablelistview.StickListView.java

/**
 * Measure the provided child.//from  w  w  w .  j a v a  2s. c o  m
 *
 * @param child The child.
 */
private void measureChild(View child) {
    ViewGroup.LayoutParams childLayoutParams = getLayoutParams(child);
    int childWidthSpec = ViewGroup.getChildMeasureSpec(mWidthMeasureSpec, getPaddingLeft() + getPaddingRight(),
            childLayoutParams.width);

    int childHeightSpec;
    if (childLayoutParams.height > 0) {
        childHeightSpec = MeasureSpec.makeMeasureSpec(childLayoutParams.height, MeasureSpec.EXACTLY);
    } else {
        childHeightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
    }

    child.measure(childWidthSpec, childHeightSpec);
}

From source file:aksha.upcomingdemo.HorizontalListView.java

/**
 * Measure the provided child./*from   ww w .j a v a  2 s.c o m*/
 *
 * @param child The child.
 */
private void measureChild(View child) {
    ViewGroup.LayoutParams childLayoutParams = getLayoutParams(child);
    int childHeightSpec = ViewGroup.getChildMeasureSpec(mHeightMeasureSpec,
            getPaddingTop() + getPaddingBottom(), childLayoutParams.height);

    int childWidthSpec;
    if (childLayoutParams.width > 0) {
        childWidthSpec = MeasureSpec.makeMeasureSpec(childLayoutParams.width, MeasureSpec.EXACTLY);
    } else {
        childWidthSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
    }

    child.measure(childWidthSpec, childHeightSpec);
}

From source file:beautician.beauty.android.views.HorizontalListView.java

/**
 * Measure the provided child.//from w  w w .  j av a 2  s  .c  om
 *
 * @param child The child.
 */
private void measureChild(View child) {
    LayoutParams childLayoutParams = getLayoutParams(child);
    int childHeightSpec = ViewGroup.getChildMeasureSpec(mHeightMeasureSpec,
            getPaddingTop() + getPaddingBottom(), childLayoutParams.height);

    int childWidthSpec;
    if (childLayoutParams.width > 0) {
        childWidthSpec = MeasureSpec.makeMeasureSpec(childLayoutParams.width, MeasureSpec.EXACTLY);
    } else {
        childWidthSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
    }

    child.measure(childWidthSpec, childHeightSpec);
}

From source file:com.xunlei.shortvideo.view.HorizontalListView.java

/**
 * Measure the provided child./* w  ww .j av a  2 s.  c  o m*/
 *
 * @param child The child.
 */
protected void measureChild(View child) {
    LayoutParams childLayoutParams = getLayoutParams(child);
    int childHeightSpec = ViewGroup.getChildMeasureSpec(mHeightMeasureSpec,
            getPaddingTop() + getPaddingBottom(), childLayoutParams.height);

    int childWidthSpec;
    if (childLayoutParams.width > 0) {
        childWidthSpec = MeasureSpec.makeMeasureSpec(childLayoutParams.width, MeasureSpec.EXACTLY);
    } else {
        childWidthSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
    }

    child.measure(childWidthSpec, childHeightSpec);
}

From source file:com.actionbarsherlock.internal.widget.IcsListPopupWindow.java

private void measureScrapChild(View child, int position, int widthMeasureSpec) {
    ListView.LayoutParams p = (ListView.LayoutParams) child.getLayoutParams();
    if (p == null) {
        p = new ListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT,
                0);/*from  w w  w  .j a v a  2s.c om*/
        child.setLayoutParams(p);
    }
    //XXX p.viewType = mAdapter.getItemViewType(position);
    //XXX p.forceAdd = true;

    int childWidthSpec = ViewGroup.getChildMeasureSpec(widthMeasureSpec,
            mDropDownList.getPaddingLeft() + mDropDownList.getPaddingRight(), 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:android.support.v7.internal.widget.SpinnerCompat.java

/**
 * Helper for makeAndAddView to set the position of a view and fill out its layout paramters.
 *
 * @param child    The view to position/*from  www.j  av a2  s.  c o  m*/
 * @param addChild true if the child should be added to the Spinner during setup
 */
private void setUpChild(View child, boolean addChild) {

    // Respect layout params that are already in the view. Otherwise
    // make some up...
    ViewGroup.LayoutParams lp = child.getLayoutParams();
    if (lp == null) {
        lp = generateDefaultLayoutParams();
    }

    if (addChild) {
        addViewInLayout(child, 0, lp);
    }

    child.setSelected(hasFocus());
    if (mDisableChildrenWhenDisabled) {
        child.setEnabled(isEnabled());
    }

    // Get measure specs
    int childHeightSpec = ViewGroup.getChildMeasureSpec(mHeightMeasureSpec,
            mSpinnerPadding.top + mSpinnerPadding.bottom, lp.height);
    int childWidthSpec = ViewGroup.getChildMeasureSpec(mWidthMeasureSpec,
            mSpinnerPadding.left + mSpinnerPadding.right, lp.width);

    // Measure child
    child.measure(childWidthSpec, childHeightSpec);

    int childLeft;
    int childRight;

    // Position vertically based on gravity setting
    int childTop = mSpinnerPadding.top
            + ((getMeasuredHeight() - mSpinnerPadding.bottom - mSpinnerPadding.top - child.getMeasuredHeight())
                    / 2);
    int childBottom = childTop + child.getMeasuredHeight();

    int width = child.getMeasuredWidth();
    childLeft = 0;
    childRight = childLeft + width;

    child.layout(childLeft, childTop, childRight, childBottom);
}