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:android.support.v7.widget.StaggeredGridLayoutManager.java

private void recycleFromEnd(RecyclerView.Recycler recycler, int line) {
    final int childCount = getChildCount();
    int i;/*from w w  w  . j  av a  2  s.c o m*/
    for (i = childCount - 1; i >= 0; i--) {
        View child = getChildAt(i);
        if (mPrimaryOrientation.getDecoratedStart(child) >= line) {
            LayoutParams lp = (LayoutParams) child.getLayoutParams();
            // Don't recycle the last View in a span not to lose span's start/end lines
            if (lp.mFullSpan) {
                for (int j = 0; j < mSpanCount; j++) {
                    if (mSpans[j].mViews.size() == 1) {
                        return;
                    }
                }
                for (int j = 0; j < mSpanCount; j++) {
                    mSpans[j].popEnd();
                }
            } else {
                if (lp.mSpan.mViews.size() == 1) {
                    return;
                }
                lp.mSpan.popEnd();
            }
            removeAndRecycleView(child, recycler);
        } else {
            return;// done
        }
    }
}

From source file:caesar.feng.framework.widget.StaggeredGrid.ExtendableListView.java

private void clearRecycledState(ArrayList<FixedViewInfo> infos) {
    if (infos == null)
        return;/*from  ww w .j  a  v  a2  s .  co m*/
    for (FixedViewInfo info : infos) {
        final View child = info.view;
        final ViewGroup.LayoutParams p = child.getLayoutParams();

        if (p instanceof LayoutParams) {
            ((LayoutParams) p).recycledHeaderFooter = false;
        }
    }
}

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

final LayoutParams getLayoutParams(View c) {
    return (LayoutParams) c.getLayoutParams();
}

From source file:android.support.custom.view.VerticalViewPager.java

void dataSetChanged() {
    // This method only gets called if our observer is attached, so mAdapter is non-null.

    boolean needPopulate = mItems.size() < mOffscreenPageLimit * 2 + 1 && mItems.size() < mAdapter.getCount();
    int newCurrItem = mCurItem;

    boolean isUpdating = false;
    for (int i = 0; i < mItems.size(); i++) {
        final ItemInfo ii = mItems.get(i);
        final int newPos = mAdapter.getItemPosition(ii.object);

        if (newPos == PagerAdapter.POSITION_UNCHANGED) {
            continue;
        }// ww w . jav  a  2 s  . com

        if (newPos == PagerAdapter.POSITION_NONE) {
            mItems.remove(i);
            i--;

            if (!isUpdating) {
                mAdapter.startUpdate(this);
                isUpdating = true;
            }

            mAdapter.destroyItem(this, ii.position, ii.object);
            needPopulate = true;

            if (mCurItem == ii.position) {
                // Keep the current item in the valid range
                newCurrItem = Math.max(0, Math.min(mCurItem, mAdapter.getCount() - 1));
                needPopulate = true;
            }
            continue;
        }

        if (ii.position != newPos) {
            if (ii.position == mCurItem) {
                // Our current item changed position. Follow it.
                newCurrItem = newPos;
            }

            ii.position = newPos;
            needPopulate = true;
        }
    }

    if (isUpdating) {
        mAdapter.finishUpdate(this);
    }

    Collections.sort(mItems, COMPARATOR);

    if (needPopulate) {
        // Reset our known page widths; populate will recompute them.
        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) {
                lp.widthFactor = 0.f;
                lp.heightFactor = 0.f;
            }
        }

        setCurrentItemInternal(newCurrItem, false, true);
        requestLayout();
    }
}

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

private int fill(RecyclerView.Recycler recycler, LayoutState layoutState, RecyclerView.State state) {
    mRemainingSpans.set(0, mSpanCount, true);
    // The target position we are trying to reach.
    final int targetLine;

    // Line of the furthest row.
    if (layoutState.mLayoutDirection == LAYOUT_END) {
        targetLine = layoutState.mEndLine + layoutState.mAvailable;
    } else { // LAYOUT_START
        targetLine = layoutState.mStartLine - layoutState.mAvailable;
    }/*from w  w  w .  j  a va  2s . com*/

    updateAllRemainingSpans(layoutState.mLayoutDirection, targetLine);
    if (DEBUG) {
        Log.d(TAG, "FILLING targetLine: " + targetLine + "," + "remaining spans:" + mRemainingSpans
                + ", state: " + layoutState);
    }

    // the default coordinate to add new view.
    final int defaultNewViewLine = mShouldReverseLayout ? mPrimaryOrientation.getEndAfterPadding()
            : mPrimaryOrientation.getStartAfterPadding();
    boolean added = false;
    while (layoutState.hasMore(state) && !mRemainingSpans.isEmpty()) {
        View view = layoutState.next(recycler);
        LayoutParams lp = ((LayoutParams) view.getLayoutParams());
        final int position = lp.getViewLayoutPosition();
        final int spanIndex = mLazySpanLookup.getSpan(position);
        Span currentSpan;
        final boolean assignSpan = spanIndex == LayoutParams.INVALID_SPAN_ID;
        if (assignSpan) {
            currentSpan = lp.mFullSpan ? mSpans[0] : getNextSpan(layoutState);
            mLazySpanLookup.setSpan(position, currentSpan);
            if (DEBUG) {
                Log.d(TAG, "assigned " + currentSpan.mIndex + " for " + position);
            }
        } else {
            if (DEBUG) {
                Log.d(TAG, "using " + spanIndex + " for pos " + position);
            }
            currentSpan = mSpans[spanIndex];
        }
        // assign span before measuring so that item decorators can get updated span index
        lp.mSpan = currentSpan;
        if (layoutState.mLayoutDirection == LAYOUT_END) {
            addView(view);
        } else {
            addView(view, 0);
        }
        measureChildWithDecorationsAndMargin(view, lp);

        final int start;
        final int end;
        if (layoutState.mLayoutDirection == LAYOUT_END) {
            start = lp.mFullSpan ? getMaxEnd(defaultNewViewLine) : currentSpan.getEndLine(defaultNewViewLine);
            end = start + mPrimaryOrientation.getDecoratedMeasurement(view);
            if (assignSpan && lp.mFullSpan) {
                LazySpanLookup.FullSpanItem fullSpanItem;
                fullSpanItem = createFullSpanItemFromEnd(start);
                fullSpanItem.mGapDir = LAYOUT_START;
                fullSpanItem.mPosition = position;
                mLazySpanLookup.addFullSpanItem(fullSpanItem);
            }
        } else {
            end = lp.mFullSpan ? getMinStart(defaultNewViewLine) : currentSpan.getStartLine(defaultNewViewLine);
            start = end - mPrimaryOrientation.getDecoratedMeasurement(view);
            if (assignSpan && lp.mFullSpan) {
                LazySpanLookup.FullSpanItem fullSpanItem;
                fullSpanItem = createFullSpanItemFromStart(end);
                fullSpanItem.mGapDir = LAYOUT_END;
                fullSpanItem.mPosition = position;
                mLazySpanLookup.addFullSpanItem(fullSpanItem);
            }
        }

        // check if this item may create gaps in the future
        if (lp.mFullSpan && layoutState.mItemDirection == ITEM_DIRECTION_HEAD) {
            if (assignSpan) {
                mLaidOutInvalidFullSpan = true;
            } else {
                final boolean hasInvalidGap;
                if (layoutState.mLayoutDirection == LAYOUT_END) {
                    hasInvalidGap = !areAllEndsEqual();
                } else { // layoutState.mLayoutDirection == LAYOUT_START
                    hasInvalidGap = !areAllStartsEqual();
                }
                if (hasInvalidGap) {
                    final LazySpanLookup.FullSpanItem fullSpanItem = mLazySpanLookup.getFullSpanItem(position);
                    if (fullSpanItem != null) {
                        fullSpanItem.mHasUnwantedGapAfter = true;
                    }
                    mLaidOutInvalidFullSpan = true;
                }
            }

        }
        attachViewToSpans(view, lp, layoutState);
        final int otherStart = lp.mFullSpan ? mSecondaryOrientation.getStartAfterPadding()
                : currentSpan.mIndex * mSizePerSpan + mSecondaryOrientation.getStartAfterPadding();
        final int otherEnd = otherStart + mSecondaryOrientation.getDecoratedMeasurement(view);
        if (mOrientation == VERTICAL) {
            layoutDecoratedWithMargins(view, otherStart, start, otherEnd, end);
        } else {
            layoutDecoratedWithMargins(view, start, otherStart, end, otherEnd);
        }

        if (lp.mFullSpan) {
            updateAllRemainingSpans(mLayoutState.mLayoutDirection, targetLine);
        } else {
            updateRemainingSpans(currentSpan, mLayoutState.mLayoutDirection, targetLine);
        }
        recycle(recycler, mLayoutState);
        added = true;
    }
    if (!added) {
        recycle(recycler, mLayoutState);
    }
    final int diff;
    if (mLayoutState.mLayoutDirection == LAYOUT_START) {
        final int minStart = getMinStart(mPrimaryOrientation.getStartAfterPadding());
        diff = mPrimaryOrientation.getStartAfterPadding() - minStart;
    } else {
        final int maxEnd = getMaxEnd(mPrimaryOrientation.getEndAfterPadding());
        diff = maxEnd - mPrimaryOrientation.getEndAfterPadding();
    }
    return diff > 0 ? Math.min(layoutState.mAvailable, diff) : 0;
}

From source file:android.support.v71.widget.StaggeredGridLayoutManager.java

private int fill(RecyclerView.Recycler recycler, LayoutState layoutState, RecyclerView.State state) {
    mRemainingSpans.set(0, mSpanCount, true);
    // The target position we are trying to reach.
    final int targetLine;

    // Line of the furthest row.
    if (layoutState.mLayoutDirection == LayoutState.LAYOUT_END) {
        targetLine = layoutState.mEndLine + layoutState.mAvailable;
    } else { // LAYOUT_START
        targetLine = layoutState.mStartLine - layoutState.mAvailable;
    }/*from w ww  .j  av  a2 s  .co m*/

    updateAllRemainingSpans(layoutState.mLayoutDirection, targetLine);
    if (DEBUG) {
        Log.d(TAG, "FILLING targetLine: " + targetLine + "," + "remaining spans:" + mRemainingSpans
                + ", state: " + layoutState);
    }

    // the default coordinate to add new view.
    final int defaultNewViewLine = mShouldReverseLayout ? mPrimaryOrientation.getEndAfterPadding()
            : mPrimaryOrientation.getStartAfterPadding();
    boolean added = false;
    while (layoutState.hasMore(state) && !mRemainingSpans.isEmpty()) {
        View view = layoutState.next(recycler);
        LayoutParams lp = ((LayoutParams) view.getLayoutParams());
        final int position = lp.getViewLayoutPosition();
        final int spanIndex = mLazySpanLookup.getSpan(position);
        Span currentSpan;
        final boolean assignSpan = spanIndex == LayoutParams.INVALID_SPAN_ID;
        if (assignSpan) {
            currentSpan = lp.mFullSpan ? mSpans[0] : getNextSpan(layoutState);
            mLazySpanLookup.setSpan(position, currentSpan);
            if (DEBUG) {
                Log.d(TAG, "assigned " + currentSpan.mIndex + " for " + position);
            }
        } else {
            if (DEBUG) {
                Log.d(TAG, "using " + spanIndex + " for pos " + position);
            }
            currentSpan = mSpans[spanIndex];
        }
        // assign span before measuring so that item decorators can get updated span index
        lp.mSpan = currentSpan;
        if (layoutState.mLayoutDirection == LayoutState.LAYOUT_END) {
            addView(view);
        } else {
            addView(view, 0);
        }
        measureChildWithDecorationsAndMargin(view, lp);

        final int start;
        final int end;
        if (layoutState.mLayoutDirection == LayoutState.LAYOUT_END) {
            start = lp.mFullSpan ? getMaxEnd(defaultNewViewLine) : currentSpan.getEndLine(defaultNewViewLine);
            end = start + mPrimaryOrientation.getDecoratedMeasurement(view);
            if (assignSpan && lp.mFullSpan) {
                LazySpanLookup.FullSpanItem fullSpanItem;
                fullSpanItem = createFullSpanItemFromEnd(start);
                fullSpanItem.mGapDir = LayoutState.LAYOUT_START;
                fullSpanItem.mPosition = position;
                mLazySpanLookup.addFullSpanItem(fullSpanItem);
            }
        } else {
            end = lp.mFullSpan ? getMinStart(defaultNewViewLine) : currentSpan.getStartLine(defaultNewViewLine);
            start = end - mPrimaryOrientation.getDecoratedMeasurement(view);
            if (assignSpan && lp.mFullSpan) {
                LazySpanLookup.FullSpanItem fullSpanItem;
                fullSpanItem = createFullSpanItemFromStart(end);
                fullSpanItem.mGapDir = LayoutState.LAYOUT_END;
                fullSpanItem.mPosition = position;
                mLazySpanLookup.addFullSpanItem(fullSpanItem);
            }
        }

        // check if this item may create gaps in the future
        if (lp.mFullSpan && layoutState.mItemDirection == LayoutState.ITEM_DIRECTION_HEAD) {
            if (assignSpan) {
                mLaidOutInvalidFullSpan = true;
            } else {
                final boolean hasInvalidGap;
                if (layoutState.mLayoutDirection == LayoutState.LAYOUT_END) {
                    hasInvalidGap = !areAllEndsEqual();
                } else { // layoutState.mLayoutDirection == LAYOUT_START
                    hasInvalidGap = !areAllStartsEqual();
                }
                if (hasInvalidGap) {
                    final LazySpanLookup.FullSpanItem fullSpanItem = mLazySpanLookup.getFullSpanItem(position);
                    if (fullSpanItem != null) {
                        fullSpanItem.mHasUnwantedGapAfter = true;
                    }
                    mLaidOutInvalidFullSpan = true;
                }
            }

        }
        attachViewToSpans(view, lp, layoutState);
        final int otherStart = lp.mFullSpan ? mSecondaryOrientation.getStartAfterPadding()
                : currentSpan.mIndex * mSizePerSpan + mSecondaryOrientation.getStartAfterPadding();
        final int otherEnd = otherStart + mSecondaryOrientation.getDecoratedMeasurement(view);
        if (mOrientation == VERTICAL) {
            layoutDecoratedWithMargins(view, otherStart, start, otherEnd, end);
        } else {
            layoutDecoratedWithMargins(view, start, otherStart, end, otherEnd);
        }

        if (lp.mFullSpan) {
            updateAllRemainingSpans(mLayoutState.mLayoutDirection, targetLine);
        } else {
            updateRemainingSpans(currentSpan, mLayoutState.mLayoutDirection, targetLine);
        }
        recycle(recycler, mLayoutState);
        added = true;
    }
    if (!added) {
        recycle(recycler, mLayoutState);
    }
    final int diff;
    if (mLayoutState.mLayoutDirection == LayoutState.LAYOUT_START) {
        final int minStart = getMinStart(mPrimaryOrientation.getStartAfterPadding());
        diff = mPrimaryOrientation.getStartAfterPadding() - minStart;
    } else {
        final int maxEnd = getMaxEnd(mPrimaryOrientation.getEndAfterPadding());
        diff = maxEnd - mPrimaryOrientation.getEndAfterPadding();
    }
    return diff > 0 ? Math.min(layoutState.mAvailable, diff) : 0;
}

From source file:android.support.custom.view.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./*  www  .j av  a2  s  .c o m*/
 *
 * @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_VERTICAL:
                childTop = Math.max((height - child.getMeasuredHeight()) / 2, paddingTop);
                break;
            case Gravity.BOTTOM:
                childTop = height - paddingBottom - child.getMeasuredHeight();
                paddingBottom += child.getMeasuredHeight();
                break;
            }
            childTop += scrollY;

            final int childOffset = childTop - child.getTop();
            if (childOffset != 0) {
                child.offsetTopAndBottom(childOffset);
            }
        }
    }

    if (mSeenPositionMin < 0 || position < mSeenPositionMin) {
        mSeenPositionMin = position;
    }
    if (mSeenPositionMax < 0 || FloatMath.ceil(position + offset) > mSeenPositionMax) {
        mSeenPositionMax = position + 1;
    }

    if (mOnPageChangeListener != null) {
        mOnPageChangeListener.onPageScrolled(position, offset, offsetPixels);
    }
    if (mInternalPageChangeListener != null) {
        mInternalPageChangeListener.onPageScrolled(position, offset, offsetPixels);
    }

    if (mPageTransformer != null) {
        final int scrollY = getScrollY();
        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 float transformPos = (float) (child.getTop() - scrollY) / getHeight();
            mPageTransformer.transformPage(child, transformPos);
        }
    }

    mCalledSuper = true;
}

From source file:VerticalViewPager.java

private void removeNonDecorViews() {
    for (int i = 0; i < getChildCount(); i++) {
        final View child = getChildAt(i);
        final LayoutParams lp = (LayoutParams) child.getLayoutParams();
        if (!lp.isDecor) {
            removeViewAt(i);/*  w w w  .java  2 s  . co m*/
            i--;
        }
    }
}

From source file:caesar.feng.framework.widget.StaggeredGrid.ExtendableListView.java

protected LayoutParams generateWrapperLayoutParams(final View child) {
    LayoutParams layoutParams = null;/*from  w  w w .  j a va2  s  . co  m*/

    final ViewGroup.LayoutParams childParams = child.getLayoutParams();
    if (childParams != null) {
        if (childParams instanceof LayoutParams) {
            layoutParams = (LayoutParams) childParams;
        } else {
            layoutParams = new LayoutParams(childParams);
        }
    }
    if (layoutParams == null) {
        layoutParams = generateDefaultLayoutParams();
    }

    return layoutParams;
}

From source file:android.support.custom.view.VerticalViewPager.java

@SuppressWarnings("deprecation")
@Override/*from w w w  . jav  a  2s .c  o m*/
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    if (DEBUG)
        Log.d(TAG, "onMeasure");

    // For simple implementation, or internal size is always 0.
    // We depend on the container to specify the layout size of
    // our view.  We can't really know what it is since we will be
    // adding and removing different arbitrary views and do not
    // want the layout to change as this happens.
    setMeasuredDimension(getDefaultSize(0, widthMeasureSpec), getDefaultSize(0, heightMeasureSpec));

    final int measuredWidth = getMeasuredWidth();
    final int measuredHeight = getMeasuredHeight();

    final int maxGutterSize = measuredHeight / 10;
    mGutterSize = Math.min(maxGutterSize, mDefaultGutterSize);

    // Children are just made to fill our space.
    int childWidthSize = measuredWidth - getPaddingLeft() - getPaddingRight();
    int childHeightSize = measuredHeight - getPaddingTop() - getPaddingBottom();

    /*
     * Make sure all children have been properly measured. Decor views first.
     * Right now we cheat and make this less complicated by assuming decor
     * views won't intersect. We will pin to edges based on gravity.
     */
    int size = getChildCount();
    for (int i = 0; i < size; ++i) {
        final View child = getChildAt(i);
        if (child.getVisibility() != GONE) {
            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
            if (lp != null && lp.isDecor) {
                final int hgrav = lp.gravity & Gravity.HORIZONTAL_GRAVITY_MASK;
                final int vgrav = lp.gravity & Gravity.VERTICAL_GRAVITY_MASK;
                int widthMode = MeasureSpec.AT_MOST;
                int heightMode = MeasureSpec.AT_MOST;
                boolean consumeVertical = vgrav == Gravity.TOP || vgrav == Gravity.BOTTOM;
                boolean consumeHorizontal = hgrav == Gravity.LEFT || hgrav == Gravity.RIGHT;

                if (consumeVertical) {
                    widthMode = MeasureSpec.EXACTLY;
                } else if (consumeHorizontal) {
                    heightMode = MeasureSpec.EXACTLY;
                }

                int widthSize = childWidthSize;
                int heightSize = childHeightSize;
                if (lp.width != LayoutParams.WRAP_CONTENT) {
                    widthMode = MeasureSpec.EXACTLY;
                    if (lp.width != LayoutParams.FILL_PARENT) {
                        widthSize = lp.width;
                    }
                }
                if (lp.height != LayoutParams.WRAP_CONTENT) {
                    heightMode = MeasureSpec.EXACTLY;
                    if (lp.height != LayoutParams.FILL_PARENT) {
                        heightSize = lp.height;
                    }
                }
                final int widthSpec = MeasureSpec.makeMeasureSpec(widthSize, widthMode);
                final int heightSpec = MeasureSpec.makeMeasureSpec(heightSize, heightMode);
                child.measure(widthSpec, heightSpec);

                if (consumeVertical) {
                    childHeightSize -= child.getMeasuredHeight();
                } else if (consumeHorizontal) {
                    childWidthSize -= child.getMeasuredWidth();
                }
            }
        }
    }

    mChildWidthMeasureSpec = MeasureSpec.makeMeasureSpec(childWidthSize, MeasureSpec.EXACTLY);

    // Make sure we have created all fragments that we need to have shown.
    mInLayout = true;
    populate();
    mInLayout = false;

    // Page views next.
    size = getChildCount();
    for (int i = 0; i < size; ++i) {
        final View child = getChildAt(i);
        if (child.getVisibility() != GONE) {
            if (DEBUG)
                Log.v(TAG, "Measuring #" + i + " " + child + ": " + mChildWidthMeasureSpec);

            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
            if (lp == null || !lp.isDecor) {
                final int heightSpec = MeasureSpec.makeMeasureSpec((int) (childHeightSize * lp.heightFactor),
                        MeasureSpec.EXACTLY);
                child.measure(mChildWidthMeasureSpec, heightSpec);
            }
        }
    }
}