Search in sources :

Example 76 with Recycler

use of android.support.v7.widget.RecyclerView.Recycler in project vlayout by alibaba.

the class ExposeLinearLayoutManagerEx method layoutChunk.

protected void layoutChunk(RecyclerView.Recycler recycler, RecyclerView.State state, LayoutState layoutState, com.alibaba.android.vlayout.layout.LayoutChunkResult result) {
    View view = layoutState.next(recycler);
    if (view == null) {
        if (DEBUG && layoutState.mScrapList == null) {
            throw new RuntimeException("received null view when unexpected");
        }
        // if we are laying out views in scrap, this may return null which means there is
        // no more items to layout.
        result.mFinished = true;
        return;
    }
    RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) view.getLayoutParams();
    if (layoutState.mScrapList == null) {
        // can not find in scrapList
        if (mShouldReverseLayoutExpose == (layoutState.mLayoutDirection == LayoutState.LAYOUT_START)) {
            addView(view);
        } else {
            addView(view, 0);
        }
    } else {
        if (mShouldReverseLayoutExpose == (layoutState.mLayoutDirection == LayoutState.LAYOUT_START)) {
            addDisappearingView(view);
        } else {
            addDisappearingView(view, 0);
        }
    }
    measureChildWithMargins(view, 0, 0);
    result.mConsumed = mOrientationHelper.getDecoratedMeasurement(view);
    int left, top, right, bottom;
    if (getOrientation() == VERTICAL) {
        if (isLayoutRTL()) {
            right = getWidth() - getPaddingRight();
            left = right - mOrientationHelper.getDecoratedMeasurementInOther(view);
        } else {
            left = getPaddingLeft();
            right = left + mOrientationHelper.getDecoratedMeasurementInOther(view);
        }
        if (layoutState.mLayoutDirection == LayoutState.LAYOUT_START) {
            bottom = layoutState.mOffset;
            top = layoutState.mOffset - result.mConsumed;
        } else {
            top = layoutState.mOffset;
            bottom = layoutState.mOffset + result.mConsumed;
        }
    } else {
        top = getPaddingTop();
        bottom = top + mOrientationHelper.getDecoratedMeasurementInOther(view);
        // whether this layout pass is layout form start to end or in reverse
        if (layoutState.mLayoutDirection == LayoutState.LAYOUT_START) {
            right = layoutState.mOffset;
            left = layoutState.mOffset - result.mConsumed;
        } else {
            left = layoutState.mOffset;
            right = layoutState.mOffset + result.mConsumed;
        }
    }
    // We calculate everything with View's bounding box (which includes decor and margins)
    // To calculate correct layout position, we subtract margins.
    layoutDecorated(view, left + params.leftMargin, top + params.topMargin, right - params.rightMargin, bottom - params.bottomMargin);
    if (DEBUG) {
        Log.d(TAG, "laid out child at position " + getPosition(view) + ", with l:" + (left + params.leftMargin) + ", t:" + (top + params.topMargin) + ", r:" + (right - params.rightMargin) + ", b:" + (bottom - params.bottomMargin));
    }
    // Consume the available space if the view is not removed OR changed
    if (params.isItemRemoved() || params.isItemChanged()) {
        result.mIgnoreConsumed = true;
    }
    result.mFocusable = view.isFocusable();
}
Also used : RecyclerView(android.support.v7.widget.RecyclerView) RecyclerView(android.support.v7.widget.RecyclerView) View(android.view.View) SuppressLint(android.annotation.SuppressLint)

Example 77 with Recycler

use of android.support.v7.widget.RecyclerView.Recycler in project vlayout by alibaba.

the class VirtualLayoutManager method onMeasure.

@Override
public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state, int widthSpec, int heightSpec) {
    if (!mNoScrolling && !mNestedScrolling) {
        super.onMeasure(recycler, state, widthSpec, heightSpec);
        return;
    }
    int initialSize = MAX_NO_SCROLLING_SIZE;
    if (mRecyclerView != null && mNestedScrolling) {
        if (mMaxMeasureSize > 0) {
            initialSize = mMaxMeasureSize;
        } else {
            ViewParent parent = mRecyclerView.getParent();
            if (parent instanceof View) {
                initialSize = ((View) parent).getMeasuredHeight();
            }
        }
    }
    int measuredSize = mSpaceMeasured ? mMeasuredFullSpace : initialSize;
    if (mNoScrolling) {
        mSpaceMeasuring = !mSpaceMeasured;
        if (getChildCount() > 0 || getChildCount() != getItemCount()) {
            View lastChild = getChildAt(getChildCount() - 1);
            int bottom = mMeasuredFullSpace;
            if (lastChild != null) {
                RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) lastChild.getLayoutParams();
                bottom = getDecoratedBottom(lastChild) + params.bottomMargin + computeAlignOffset(lastChild, true, false);
            }
            if (getChildCount() != getItemCount() || (lastChild != null && bottom != mMeasuredFullSpace)) {
                measuredSize = MAX_NO_SCROLLING_SIZE;
                mSpaceMeasured = false;
                mSpaceMeasuring = true;
            }
        } else if (getItemCount() == 0) {
            measuredSize = 0;
            mSpaceMeasured = true;
            mSpaceMeasuring = false;
        }
    }
    if (getOrientation() == VERTICAL) {
        super.onMeasure(recycler, state, widthSpec, View.MeasureSpec.makeMeasureSpec(measuredSize, View.MeasureSpec.AT_MOST));
    } else {
        super.onMeasure(recycler, state, View.MeasureSpec.makeMeasureSpec(measuredSize, View.MeasureSpec.AT_MOST), heightSpec);
    }
}
Also used : ViewParent(android.view.ViewParent) RecyclerView(android.support.v7.widget.RecyclerView) View(android.view.View) RecyclerView(android.support.v7.widget.RecyclerView)

Example 78 with Recycler

use of android.support.v7.widget.RecyclerView.Recycler in project vlayout by alibaba.

the class VirtualLayoutManager method detachAndScrapViewAt.

@Override
public void detachAndScrapViewAt(int index, RecyclerView.Recycler recycler) {
    View child = getChildAt(index);
    RecyclerView.ViewHolder holder = getChildViewHolder(child);
    if (holder instanceof CacheViewHolder && ((CacheViewHolder) holder).needCached()) {
        // mark not invalid
        ViewHolderWrapper.setFlags(holder, 0, FLAG_INVALID);
    }
    super.detachAndScrapViewAt(index, recycler);
}
Also used : RecyclerView(android.support.v7.widget.RecyclerView) View(android.view.View) RecyclerView(android.support.v7.widget.RecyclerView)

Example 79 with Recycler

use of android.support.v7.widget.RecyclerView.Recycler in project vlayout by alibaba.

the class ColumnLayoutHelper method layoutViews.

@Override
public void layoutViews(RecyclerView.Recycler recycler, RecyclerView.State state, VirtualLayoutManager.LayoutStateWrapper layoutState, LayoutChunkResult result, LayoutManagerHelper helper) {
    // reach the end of this layout
    if (isOutOfRange(layoutState.getCurrentPosition())) {
        return;
    }
    final boolean layoutInVertical = helper.getOrientation() == VERTICAL;
    final OrientationHelper orientationHelper = helper.getMainOrientationHelper();
    final int itemCount = getItemCount();
    if (mViews == null || mViews.length != itemCount) {
        mViews = new View[itemCount];
    }
    if (mEqViews == null || mEqViews.length != itemCount) {
        mEqViews = new View[itemCount];
    } else {
        Arrays.fill(mEqViews, null);
    }
    final int count = getAllChildren(mViews, recycler, layoutState, result, helper);
    if (layoutInVertical) {
        // TODO: only handle vertical layout now
        int maxVMargin = 0;
        int lastHMargin = 0;
        int totalMargin = 0;
        for (int i = 0; i < count; i++) {
            View view = mViews[i];
            ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
            if (layoutParams instanceof RecyclerView.LayoutParams) {
                RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) layoutParams;
                params.leftMargin = Math.max(lastHMargin, params.leftMargin);
                totalMargin += params.leftMargin;
                if (i != count - 1) {
                    // not last item
                    lastHMargin = params.rightMargin;
                    params.rightMargin = 0;
                } else {
                    totalMargin += params.rightMargin;
                }
                maxVMargin = Math.max(maxVMargin, params.topMargin + params.bottomMargin);
            }
        }
        final int totalWidth = helper.getContentWidth() - helper.getPaddingLeft() - helper.getPaddingRight() - getHorizontalMargin() - getHorizontalPadding();
        final int availableWidth = totalWidth - totalMargin;
        int usedWidth = 0;
        int minHeight = Integer.MAX_VALUE;
        int uniformHeight = -1;
        if (!Float.isNaN(mAspectRatio)) {
            uniformHeight = (int) (totalWidth / mAspectRatio + 0.5f);
        }
        int eqSize = 0;
        for (int i = 0; i < count; i++) {
            View view = mViews[i];
            VirtualLayoutManager.LayoutParams params = (VirtualLayoutManager.LayoutParams) view.getLayoutParams();
            int heightSpec = helper.getChildMeasureSpec(helper.getContentHeight() - helper.getPaddingTop() - helper.getPaddingBottom(), uniformHeight > 0 ? uniformHeight : params.height, true);
            if (mWeights != null && i < mWeights.length && !Float.isNaN(mWeights[i]) && mWeights[i] >= 0) {
                // calculate width with weight in percentage
                int resizeWidth = (int) (mWeights[i] * 1.0f / 100 * availableWidth + 0.5f);
                if (!Float.isNaN(params.mAspectRatio)) {
                    int specialHeight = (int) (resizeWidth / params.mAspectRatio + 0.5f);
                    heightSpec = View.MeasureSpec.makeMeasureSpec(specialHeight, View.MeasureSpec.EXACTLY);
                }
                helper.measureChild(view, View.MeasureSpec.makeMeasureSpec(resizeWidth, View.MeasureSpec.EXACTLY), heightSpec);
                // add width into usedWidth
                usedWidth += resizeWidth;
                // find minHeight
                minHeight = Math.min(minHeight, view.getMeasuredHeight());
            } else {
                mEqViews[eqSize++] = view;
            }
        }
        for (int i = 0; i < eqSize; i++) {
            View view = mEqViews[i];
            VirtualLayoutManager.LayoutParams params = (VirtualLayoutManager.LayoutParams) view.getLayoutParams();
            int heightSpec;
            int resizeWidth = (int) ((availableWidth - usedWidth) * 1.0f / eqSize + 0.5f);
            if (!Float.isNaN(params.mAspectRatio)) {
                int specialHeight = (int) (resizeWidth / params.mAspectRatio + 0.5f);
                heightSpec = View.MeasureSpec.makeMeasureSpec(specialHeight, View.MeasureSpec.EXACTLY);
            } else {
                heightSpec = helper.getChildMeasureSpec(helper.getContentHeight() - helper.getPaddingTop() - helper.getPaddingBottom(), uniformHeight > 0 ? uniformHeight : params.height, true);
            }
            //if cols' length is less than view's count, then remainder views share the rest space
            helper.measureChild(view, View.MeasureSpec.makeMeasureSpec(resizeWidth, View.MeasureSpec.EXACTLY), heightSpec);
            // find minHeight
            minHeight = Math.min(minHeight, view.getMeasuredHeight());
        }
        // uniform all views into min height
        for (int i = 0; i < count; i++) {
            View view = mViews[i];
            if (view.getMeasuredHeight() != minHeight) {
                //noinspection ResourceType
                helper.measureChild(view, View.MeasureSpec.makeMeasureSpec(view.getMeasuredWidth(), View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(minHeight, View.MeasureSpec.EXACTLY));
            }
        }
        result.mConsumed = minHeight + maxVMargin + getVerticalMargin() + getVerticalPadding();
        calculateRect(minHeight + maxVMargin, mTempArea, layoutState, helper);
        // do layout
        int left = mTempArea.left;
        for (int i = 0; i < count; i++) {
            View view = mViews[i];
            int top = mTempArea.top, bottom = mTempArea.bottom;
            int right = left + orientationHelper.getDecoratedMeasurementInOther(view);
            layoutChild(view, left, top, right, bottom, helper);
            left = right;
        }
    }
    Arrays.fill(mViews, null);
    Arrays.fill(mEqViews, null);
}
Also used : OrientationHelper(android.support.v7.widget.OrientationHelper) ViewGroup(android.view.ViewGroup) RecyclerView(android.support.v7.widget.RecyclerView) VirtualLayoutManager(com.alibaba.android.vlayout.VirtualLayoutManager) RecyclerView(android.support.v7.widget.RecyclerView) View(android.view.View)

Example 80 with Recycler

use of android.support.v7.widget.RecyclerView.Recycler in project vlayout by alibaba.

the class FloatLayoutHelper method layoutViews.

@Override
public void layoutViews(RecyclerView.Recycler recycler, RecyclerView.State state, VirtualLayoutManager.LayoutStateWrapper layoutState, LayoutChunkResult result, final LayoutManagerHelper helper) {
    // reach the end of this layout
    if (isOutOfRange(layoutState.getCurrentPosition())) {
        return;
    }
    // find view in currentPosition
    View view = mFixView;
    if (view == null)
        view = layoutState.next(recycler);
    else {
        layoutState.skipCurrentPosition();
    }
    if (view == null) {
        result.mFinished = true;
        return;
    }
    helper.getChildViewHolder(view).setIsRecyclable(false);
    mDoNormalHandle = state.isPreLayout();
    if (mDoNormalHandle) {
        // in PreLayout do normal layout
        helper.addChildView(layoutState, view);
    }
    mFixView = view;
    mFixView.setClickable(true);
    doMeasureAndLayout(view, helper);
    result.mConsumed = 0;
    result.mIgnoreConsumed = true;
    handleStateOnResult(result, view);
}
Also used : RecyclerView(android.support.v7.widget.RecyclerView) View(android.view.View)

Aggregations

RecyclerView (android.support.v7.widget.RecyclerView)108 View (android.view.View)102 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)20 TextView (android.widget.TextView)19 ImageView (android.widget.ImageView)13 OrientationHelper (android.support.v7.widget.OrientationHelper)12 GridLayoutManager (android.support.v7.widget.GridLayoutManager)10 ViewHolder (android.support.v7.widget.RecyclerView.ViewHolder)10 Point (android.graphics.Point)7 Bundle (android.os.Bundle)7 VirtualLayoutManager (com.alibaba.android.vlayout.VirtualLayoutManager)7 SuppressLint (android.annotation.SuppressLint)6 Context (android.content.Context)6 ViewGroup (android.view.ViewGroup)6 ActivityManager (android.app.ActivityManager)5 AccessibilityNodeInfoCompat (android.support.v4.view.accessibility.AccessibilityNodeInfoCompat)5 SpanSizeLookup (android.support.v7.widget.GridLayoutManager.SpanSizeLookup)5 LayoutParams (android.support.v7.widget.RecyclerView.LayoutParams)5 Recycler (android.support.v7.widget.RecyclerView.Recycler)5 RecyclerListener (android.support.v7.widget.RecyclerView.RecyclerListener)5