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

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    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;
                }//ww w  .  j ava  2 s  . com
                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++;
            }
        }
    }

    final int childWidth = width - paddingLeft - paddingRight;
    final int childHeight = height - paddingTop - paddingBottom;
    // 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 toff = (int) (childHeight * ii.offset);
                int childLeft = paddingLeft;
                int childTop = paddingTop + toff;

                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), MeasureSpec.EXACTLY);
                    final int heightSpec = MeasureSpec.makeMeasureSpec((int) (childHeight * lp.heightFactor),
                            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;

    if (mFirstLayout) {
        scrollToItem(mCurItem, false, 0, false);
    }
    mFirstLayout = false;
}

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

/**
 * Mark the last known child position rect for the given child view.
 * This will be used when checking if a child view's position has changed between frames.
 * The rect used here should be one returned by
 * {@link #getChildRect(View, boolean, Rect)}, with translation
 * disabled.// w  ww .  j  av a2 s  . co m
 *
 * @param child child view to set for
 * @param r rect to set
 */
void recordLastChildRect(View child, Rect r) {
    final LayoutParams lp = (LayoutParams) child.getLayoutParams();
    lp.setLastChildRect(r);
}

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

/**
 * Get the last known child rect recorded by
 * {@link #recordLastChildRect(View, Rect)}.
 *
 * @param child child view to retrieve from
 * @param out rect to set to the outpur values
 *//*from w  w  w  .java2  s .  c  o m*/
void getLastChildRect(View child, Rect out) {
    final LayoutParams lp = (LayoutParams) child.getLayoutParams();
    out.set(lp.getLastChildRect());
}

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

private void setInsetOffsetX(View child, int offsetX) {
    final LayoutParams lp = (LayoutParams) child.getLayoutParams();
    if (lp.mInsetOffsetX != offsetX) {
        final int dx = offsetX - lp.mInsetOffsetX;
        ViewCompat.offsetLeftAndRight(child, dx);
        lp.mInsetOffsetX = offsetX;//from  w  ww  .j  a va 2s . c o  m
    }
}

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

private void setInsetOffsetY(View child, int offsetY) {
    final LayoutParams lp = (LayoutParams) child.getLayoutParams();
    if (lp.mInsetOffsetY != offsetY) {
        final int dy = offsetY - lp.mInsetOffsetY;
        ViewCompat.offsetTopAndBottom(child, dy);
        lp.mInsetOffsetY = offsetY;/*from www  . j  a  v a 2 s  . c  o m*/
    }
}

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

LayoutParams getResolvedLayoutParams(View child) {
    final LayoutParams result = (LayoutParams) child.getLayoutParams();
    if (!result.mBehaviorResolved) {
        Class<?> childClass = child.getClass();
        DefaultBehavior defaultBehavior = null;
        while (childClass != null
                && (defaultBehavior = childClass.getAnnotation(DefaultBehavior.class)) == null) {
            childClass = childClass.getSuperclass();
        }//from w w  w  .  j a  v  a 2 s .com
        if (defaultBehavior != null) {
            try {
                result.setBehavior(defaultBehavior.value().newInstance());
            } catch (Exception e) {
                Log.e(TAG, "Default behavior class " + defaultBehavior.value().getName()
                        + " could not be instantiated. Did you forget a default constructor?", e);
            }
        }
        result.mBehaviorResolved = true;
    }
    return result;
}

From source file:android.support.design.widget.RaeTabLayout.java

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    // If we have a MeasureSpec which allows us to decide our height, try and use the default
    // height/*from  w ww . ja  va2  s.  c o  m*/
    final int idealHeight = dpToPx(getDefaultHeight()) + getPaddingTop() + getPaddingBottom();
    switch (MeasureSpec.getMode(heightMeasureSpec)) {
    case MeasureSpec.AT_MOST:
        heightMeasureSpec = MeasureSpec.makeMeasureSpec(
                Math.min(idealHeight, MeasureSpec.getSize(heightMeasureSpec)), MeasureSpec.EXACTLY);
        break;
    case MeasureSpec.UNSPECIFIED:
        heightMeasureSpec = MeasureSpec.makeMeasureSpec(idealHeight, MeasureSpec.EXACTLY);
        break;
    }

    final int specWidth = MeasureSpec.getSize(widthMeasureSpec);
    if (MeasureSpec.getMode(widthMeasureSpec) != MeasureSpec.UNSPECIFIED) {
        // If we don't have an unspecified width spec, use the given size to calculate
        // the max tab width
        mTabMaxWidth = mRequestedTabMaxWidth > 0 ? mRequestedTabMaxWidth
                : specWidth - dpToPx(TAB_MIN_WIDTH_MARGIN);
    }

    // Now super measure itself using the (possibly) modified height spec
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    if (getChildCount() == 1) {
        // If we're in fixed mode then we need to make the tab strip is the same width as us
        // so we don't scroll
        final View child = getChildAt(0);
        boolean remeasure = false;

        switch (mMode) {
        case MODE_SCROLLABLE:
            // We only need to resize the child if it's smaller than us. This is similar
            // to fillViewport
            remeasure = child.getMeasuredWidth() < getMeasuredWidth();
            break;
        case MODE_FIXED:
            // Resize the child so that it doesn't scroll
            remeasure = child.getMeasuredWidth() != getMeasuredWidth();
            break;
        }

        if (remeasure) {
            // Re-measure the child with a widthSpec set to be exactly our measure width
            int childHeightMeasureSpec = getChildMeasureSpec(heightMeasureSpec,
                    getPaddingTop() + getPaddingBottom(), child.getLayoutParams().height);
            int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY);
            child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
        }
    }
}

From source file:VerticalViewPager.java

void populate(int newCurrentItem) {
    ItemInfo oldCurInfo = null;/*  w w  w  .j  a  v a2s . co  m*/
    int focusDirection = View.FOCUS_FORWARD;
    if (mCurItem != newCurrentItem) {
        focusDirection = mCurItem < newCurrentItem ? View.FOCUS_DOWN : View.FOCUS_UP;
        oldCurInfo = infoForPosition(mCurItem);
        mCurItem = newCurrentItem;
    }

    if (mAdapter == null) {
        sortChildDrawingOrder();
        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...");
        sortChildDrawingOrder();
        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);

    if (N != mExpectedAdapterCount) {
        String resName;
        try {
            resName = getResources().getResourceName(getId());
        } catch (Resources.NotFoundException e) {
            resName = Integer.toHexString(getId());
        }
        throw new IllegalStateException("The application's PagerAdapter changed the adapter's"
                + " contents without calling PagerAdapter#notifyDataSetChanged!"
                + " Expected adapter item count: " + mExpectedAdapterCount + ", found: " + N + " Pager id: "
                + resName + " Pager class: " + getClass() + " Problematic adapter: " + mAdapter.getClass());
    }

    // 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 int clientHeight = getClientHeight();
        final float topHeightNeeded = clientHeight <= 0 ? 0
                : 2.f - curItem.heightFactor + (float) getPaddingTop() / (float) clientHeight;

        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;
            final float bottomHeightNeeded = clientHeight <= 0 ? 0
                    : (float) getPaddingBottom() / (float) clientHeight + 2.f;
            for (int pos = mCurItem + 1; pos < N; pos++) {
                if ((extraHeightBottom >= bottomHeightNeeded) && 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 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;
            }
        }
    }
    sortChildDrawingOrder();

    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(focusDirection)) {
                        break;
                    }
                }
            }
        }
    }
}

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

/**
 * Called to lay out each individual child view unless a
 * {@link Behavior Behavior} is present. The Behavior may choose to
 * delegate child measurement to this method.
 *
 * @param child child view to lay out/*w w  w . j  a  v  a 2  s.  com*/
 * @param layoutDirection the resolved layout direction for the CoordinatorLayout, such as
 *                        {@link ViewCompat#LAYOUT_DIRECTION_LTR} or
 *                        {@link ViewCompat#LAYOUT_DIRECTION_RTL}.
 */
public void onLayoutChild(View child, int layoutDirection) {
    final LayoutParams lp = (LayoutParams) child.getLayoutParams();
    if (lp.checkAnchorChanged()) {
        throw new IllegalStateException("An anchor may not be changed after CoordinatorLayout"
                + " measurement begins before layout is complete.");
    }
    if (lp.mAnchorView != null) {
        layoutChildWithAnchor(child, lp.mAnchorView, layoutDirection);
    } else if (lp.keyline >= 0) {
        layoutChildWithKeyline(child, lp.keyline, layoutDirection);
    } else {
        layoutChild(child, layoutDirection);
    }
}

From source file:android.support.design.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.//  w w  w  .  j a  va2  s.  c  om
 *
 * @param view the View to find dependents of to dispatch the call.
 */
public void dispatchDependentViewsChanged(View view) {
    final List<View> dependents = mChildDag.getIncomingEdges(view);
    if (dependents != null && !dependents.isEmpty()) {
        for (int i = 0; i < dependents.size(); i++) {
            final View child = dependents.get(i);
            LayoutParams lp = (LayoutParams) child.getLayoutParams();
            Behavior b = lp.getBehavior();
            if (b != null) {
                b.onDependentViewChanged(this, child, view);
            }
        }
    }
}