Search in sources :

Example 1 with OrientationHelper

use of android.support.v7.widget.OrientationHelper in project Signal-Android by WhisperSystems.

the class RecyclerViewPositionHelper method findOneVisibleChild.

View findOneVisibleChild(int fromIndex, int toIndex, boolean completelyVisible, boolean acceptPartiallyVisible) {
    OrientationHelper helper;
    if (layoutManager.canScrollVertically()) {
        helper = OrientationHelper.createVerticalHelper(layoutManager);
    } else {
        helper = OrientationHelper.createHorizontalHelper(layoutManager);
    }
    final int start = helper.getStartAfterPadding();
    final int end = helper.getEndAfterPadding();
    final int next = toIndex > fromIndex ? 1 : -1;
    View partiallyVisible = null;
    for (int i = fromIndex; i != toIndex; i += next) {
        final View child = layoutManager.getChildAt(i);
        final int childStart = helper.getDecoratedStart(child);
        final int childEnd = helper.getDecoratedEnd(child);
        if (childStart < end && childEnd > start) {
            if (completelyVisible) {
                if (childStart >= start && childEnd <= end) {
                    return child;
                } else if (acceptPartiallyVisible && partiallyVisible == null) {
                    partiallyVisible = child;
                }
            } else {
                return child;
            }
        }
    }
    return partiallyVisible;
}
Also used : OrientationHelper(android.support.v7.widget.OrientationHelper) RecyclerView(android.support.v7.widget.RecyclerView) View(android.view.View)

Example 2 with OrientationHelper

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

the class FixLayoutHelper method doMeasureAndLayout.

private void doMeasureAndLayout(View view, LayoutManagerHelper helper) {
    if (view == null || helper == null) {
        return;
    }
    final VirtualLayoutManager.LayoutParams params = (VirtualLayoutManager.LayoutParams) view.getLayoutParams();
    final OrientationHelper orientationHelper = helper.getMainOrientationHelper();
    final boolean layoutInVertical = helper.getOrientation() == VERTICAL;
    if (layoutInVertical) {
        final int widthSpec = helper.getChildMeasureSpec(helper.getContentWidth() - helper.getPaddingLeft() - helper.getPaddingRight(), params.width >= 0 ? params.width : ((mSketchMeasure && layoutInVertical) ? LayoutParams.MATCH_PARENT : LayoutParams.WRAP_CONTENT), false);
        int heightSpec;
        if (!Float.isNaN(params.mAspectRatio) && params.mAspectRatio > 0) {
            heightSpec = helper.getChildMeasureSpec(helper.getContentHeight() - helper.getPaddingTop() - helper.getPaddingBottom(), (int) (View.MeasureSpec.getSize(widthSpec) / params.mAspectRatio + 0.5f), false);
        } else if (!Float.isNaN(mAspectRatio) && mAspectRatio > 0) {
            heightSpec = helper.getChildMeasureSpec(helper.getContentHeight() - helper.getPaddingTop() - helper.getPaddingBottom(), (int) (View.MeasureSpec.getSize(widthSpec) / mAspectRatio + 0.5f), false);
        } else {
            heightSpec = helper.getChildMeasureSpec(helper.getContentHeight() - helper.getPaddingTop() - helper.getPaddingBottom(), params.height >= 0 ? params.height : ((mSketchMeasure && !layoutInVertical) ? LayoutParams.MATCH_PARENT : LayoutParams.WRAP_CONTENT), false);
        }
        // do measurement
        helper.measureChild(view, widthSpec, heightSpec);
    } else {
        final int heightSpec = helper.getChildMeasureSpec(helper.getContentHeight() - helper.getPaddingTop() - helper.getPaddingBottom(), params.height >= 0 ? params.height : ((mSketchMeasure && !layoutInVertical) ? LayoutParams.MATCH_PARENT : LayoutParams.WRAP_CONTENT), false);
        int widthSpec;
        if (!Float.isNaN(params.mAspectRatio) && params.mAspectRatio > 0) {
            widthSpec = helper.getChildMeasureSpec(helper.getContentWidth() - helper.getPaddingLeft() - helper.getPaddingRight(), (int) (View.MeasureSpec.getSize(heightSpec) * params.mAspectRatio + 0.5f), false);
        } else if (!Float.isNaN(mAspectRatio) && mAspectRatio > 0) {
            widthSpec = helper.getChildMeasureSpec(helper.getContentWidth() - helper.getPaddingLeft() - helper.getPaddingRight(), (int) (View.MeasureSpec.getSize(heightSpec) * mAspectRatio + 0.5f), false);
        } else {
            widthSpec = helper.getChildMeasureSpec(helper.getContentWidth() - helper.getPaddingLeft() - helper.getPaddingRight(), params.width >= 0 ? params.width : ((mSketchMeasure && layoutInVertical) ? LayoutParams.MATCH_PARENT : LayoutParams.WRAP_CONTENT), false);
        }
        // do measurement
        helper.measureChild(view, widthSpec, heightSpec);
    }
    int left, top, right, bottom;
    if (mAlignType == TOP_RIGHT) {
        top = helper.getPaddingTop() + mY + mAdjuster.top;
        right = helper.getContentWidth() - helper.getPaddingRight() - mX - mAdjuster.right;
        left = right - params.leftMargin - params.rightMargin - view.getMeasuredWidth();
        bottom = top + params.topMargin + params.bottomMargin + view.getMeasuredHeight();
    } else if (mAlignType == BOTTOM_LEFT) {
        left = helper.getPaddingLeft() + mX + mAdjuster.left;
        bottom = helper.getContentHeight() - helper.getPaddingBottom() - mY - mAdjuster.bottom;
        right = left + params.leftMargin + params.rightMargin + view.getMeasuredWidth();
        top = bottom - view.getMeasuredHeight() - params.topMargin - params.bottomMargin;
    } else if (mAlignType == BOTTOM_RIGHT) {
        right = helper.getContentWidth() - helper.getPaddingRight() - mX - mAdjuster.right;
        bottom = helper.getContentHeight() - helper.getPaddingBottom() - mY - mAdjuster.bottom;
        left = right - params.leftMargin - params.rightMargin - view.getMeasuredWidth();
        top = bottom - view.getMeasuredHeight() - params.topMargin - params.bottomMargin;
    } else {
        // TOP_LEFT
        left = helper.getPaddingLeft() + mX + mAdjuster.left;
        top = helper.getPaddingTop() + mY + mAdjuster.top;
        right = left + (layoutInVertical ? orientationHelper.getDecoratedMeasurementInOther(view) : orientationHelper.getDecoratedMeasurement(view));
        bottom = top + (layoutInVertical ? orientationHelper.getDecoratedMeasurement(view) : orientationHelper.getDecoratedMeasurementInOther(view));
    }
    layoutChild(view, left, top, right, bottom, helper);
}
Also used : LayoutParams(android.view.ViewGroup.LayoutParams) OrientationHelper(android.support.v7.widget.OrientationHelper) VirtualLayoutManager(com.alibaba.android.vlayout.VirtualLayoutManager)

Example 3 with OrientationHelper

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

the class FloatLayoutHelper method doMeasureAndLayout.

private void doMeasureAndLayout(View view, LayoutManagerHelper helper) {
    if (view == null || helper == null)
        return;
    final VirtualLayoutManager.LayoutParams params = (VirtualLayoutManager.LayoutParams) view.getLayoutParams();
    final boolean layoutInVertical = helper.getOrientation() == VERTICAL;
    if (layoutInVertical) {
        final int widthSpec = helper.getChildMeasureSpec(helper.getContentWidth() - helper.getPaddingLeft() - helper.getPaddingRight(), params.width, !layoutInVertical);
        int heightSpec;
        if (!Float.isNaN(params.mAspectRatio) && params.mAspectRatio > 0) {
            heightSpec = helper.getChildMeasureSpec(helper.getContentHeight() - helper.getPaddingTop() - helper.getPaddingBottom(), (int) (View.MeasureSpec.getSize(widthSpec) / params.mAspectRatio + 0.5f), layoutInVertical);
        } else if (!Float.isNaN(mAspectRatio) && mAspectRatio > 0) {
            heightSpec = helper.getChildMeasureSpec(helper.getContentHeight() - helper.getPaddingTop() - helper.getPaddingBottom(), (int) (View.MeasureSpec.getSize(widthSpec) / mAspectRatio + 0.5f), layoutInVertical);
        } else {
            heightSpec = helper.getChildMeasureSpec(helper.getContentHeight() - helper.getPaddingTop() - helper.getPaddingBottom(), params.height, layoutInVertical);
        }
        // do measurement
        helper.measureChild(view, widthSpec, heightSpec);
    } else {
        int widthSpec;
        final int heightSpec = helper.getChildMeasureSpec(helper.getContentHeight() - helper.getPaddingTop() - helper.getPaddingBottom(), params.height, layoutInVertical);
        if (!Float.isNaN(params.mAspectRatio) && params.mAspectRatio > 0) {
            widthSpec = helper.getChildMeasureSpec(helper.getContentWidth() - helper.getPaddingLeft() - helper.getPaddingRight(), (int) (View.MeasureSpec.getSize(heightSpec) * params.mAspectRatio + 0.5f), !layoutInVertical);
        } else if (!Float.isNaN(mAspectRatio) && mAspectRatio > 0) {
            widthSpec = helper.getChildMeasureSpec(helper.getContentWidth() - helper.getPaddingLeft() - helper.getPaddingRight(), (int) (View.MeasureSpec.getSize(heightSpec) * mAspectRatio + 0.5f), !layoutInVertical);
        } else {
            widthSpec = helper.getChildMeasureSpec(helper.getContentWidth() - helper.getPaddingLeft() - helper.getPaddingRight(), params.width, !layoutInVertical);
        }
        // do measurement
        helper.measureChild(view, widthSpec, heightSpec);
    }
    final OrientationHelper orientationHelper = helper.getMainOrientationHelper();
    int left, top, right, bottom;
    if (mAlignType == TOP_RIGHT) {
        top = helper.getPaddingTop() + mY + mAdjuster.top;
        right = helper.getContentWidth() - helper.getPaddingRight() - mX - mAdjuster.right;
        left = right - params.leftMargin - params.rightMargin - view.getMeasuredWidth();
        bottom = top + params.topMargin + params.bottomMargin + view.getMeasuredHeight();
    } else if (mAlignType == BOTTOM_LEFT) {
        left = helper.getPaddingLeft() + mX + mAdjuster.left;
        bottom = helper.getContentHeight() - helper.getPaddingBottom() - mY - mAdjuster.bottom;
        right = left + params.leftMargin + params.rightMargin + view.getMeasuredWidth();
        top = bottom - view.getMeasuredHeight() - params.topMargin - params.bottomMargin;
    } else if (mAlignType == BOTTOM_RIGHT) {
        right = helper.getContentWidth() - helper.getPaddingRight() - mX - mAdjuster.right;
        bottom = helper.getContentHeight() - helper.getPaddingBottom() - mY - mAdjuster.bottom;
        left = right - (layoutInVertical ? orientationHelper.getDecoratedMeasurementInOther(view) : orientationHelper.getDecoratedMeasurement(view));
        top = bottom - (layoutInVertical ? orientationHelper.getDecoratedMeasurement(view) : orientationHelper.getDecoratedMeasurementInOther(view));
    } else {
        // TOP_LEFT
        left = helper.getPaddingLeft() + mX + mAdjuster.left;
        top = helper.getPaddingTop() + mY + mAdjuster.top;
        right = left + (layoutInVertical ? orientationHelper.getDecoratedMeasurementInOther(view) : orientationHelper.getDecoratedMeasurement(view));
        bottom = top + (layoutInVertical ? orientationHelper.getDecoratedMeasurement(view) : orientationHelper.getDecoratedMeasurementInOther(view));
    }
    if (left < helper.getPaddingLeft() + mAdjuster.left) {
        left = helper.getPaddingLeft() + mAdjuster.left;
        right = left + (layoutInVertical ? orientationHelper.getDecoratedMeasurementInOther(view) : orientationHelper.getDecoratedMeasurement(view));
    }
    if (right > helper.getContentWidth() - helper.getPaddingRight() - mAdjuster.right) {
        right = helper.getContentWidth() - helper.getPaddingRight() - mAdjuster.right;
        left = right - params.leftMargin - params.rightMargin - view.getMeasuredWidth();
    }
    if (top < helper.getPaddingTop() + mAdjuster.top) {
        top = helper.getPaddingTop() + mAdjuster.top;
        bottom = top + (layoutInVertical ? orientationHelper.getDecoratedMeasurement(view) : orientationHelper.getDecoratedMeasurementInOther(view));
    }
    if (bottom > helper.getContentHeight() - helper.getPaddingBottom() - mAdjuster.bottom) {
        bottom = helper.getContentHeight() - helper.getPaddingBottom() - mAdjuster.bottom;
        top = bottom - (layoutInVertical ? orientationHelper.getDecoratedMeasurement(view) : orientationHelper.getDecoratedMeasurementInOther(view));
    }
    layoutChild(view, left, top, right, bottom, helper);
}
Also used : OrientationHelper(android.support.v7.widget.OrientationHelper) VirtualLayoutManager(com.alibaba.android.vlayout.VirtualLayoutManager)

Example 4 with OrientationHelper

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

the class LinearLayoutHelper method layoutViews.

/**
     * In {@link LinearLayoutHelper}, each iteration only consume one item,
     * so it can let parent LayoutManager to decide whether the next item is in the range of this helper
     */
@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;
    }
    int currentPosition = layoutState.getCurrentPosition();
    // find corresponding layout container
    View view = nextView(recycler, layoutState, helper, result);
    if (view == null) {
        return;
    }
    VirtualLayoutManager.LayoutParams params = (VirtualLayoutManager.LayoutParams) view.getLayoutParams();
    final boolean layoutInVertical = helper.getOrientation() == VERTICAL;
    int startSpace = 0, endSpace = 0, gap = 0;
    boolean isLayoutEnd = layoutState.getLayoutDirection() == VirtualLayoutManager.LayoutStateWrapper.LAYOUT_END;
    boolean isStartLine = isLayoutEnd ? currentPosition == getRange().getLower().intValue() : currentPosition == getRange().getUpper().intValue();
    boolean isEndLine = isLayoutEnd ? currentPosition == getRange().getUpper().intValue() : currentPosition == getRange().getLower().intValue();
    if (isStartLine) {
        startSpace = layoutInVertical ? (isLayoutEnd ? mMarginTop + mPaddingTop : mMarginBottom + mPaddingBottom) : (isLayoutEnd ? mMarginLeft + mPaddingLeft : mMarginRight + mPaddingRight);
    }
    if (isEndLine) {
        endSpace = layoutInVertical ? (isLayoutEnd ? mMarginBottom + mPaddingBottom : mMarginTop + mPaddingTop) : (isLayoutEnd ? mMarginRight + mPaddingRight : mMarginLeft + mPaddingLeft);
    }
    if (!isStartLine) {
        gap = mDividerHeight;
    }
    final int widthSize = helper.getContentWidth() - helper.getPaddingLeft() - helper.getPaddingRight() - getHorizontalMargin() - getHorizontalPadding();
    int widthSpec = helper.getChildMeasureSpec(widthSize, params.width, !layoutInVertical);
    int heightSpec;
    float viewAspectRatio = params.mAspectRatio;
    if (!Float.isNaN(viewAspectRatio) && viewAspectRatio > 0) {
        heightSpec = View.MeasureSpec.makeMeasureSpec((int) (widthSize / viewAspectRatio + 0.5f), View.MeasureSpec.EXACTLY);
    } else if (!Float.isNaN(mAspectRatio) && mAspectRatio > 0) {
        heightSpec = View.MeasureSpec.makeMeasureSpec((int) (widthSize / mAspectRatio + 0.5), View.MeasureSpec.EXACTLY);
    } else {
        heightSpec = helper.getChildMeasureSpec(helper.getContentHeight() - helper.getPaddingTop() - helper.getPaddingBottom() - getVerticalMargin() - getVerticalPadding(), params.height, layoutInVertical);
    }
    helper.measureChild(view, widthSpec, heightSpec);
    OrientationHelper orientationHelper = helper.getMainOrientationHelper();
    result.mConsumed = orientationHelper.getDecoratedMeasurement(view) + startSpace + endSpace + gap;
    int left, top, right, bottom;
    if (helper.getOrientation() == VERTICAL) {
        // not support RTL now
        if (helper.isDoLayoutRTL()) {
            right = helper.getContentWidth() - helper.getPaddingRight() - mMarginRight - mPaddingRight;
            left = right - orientationHelper.getDecoratedMeasurementInOther(view);
        } else {
            left = helper.getPaddingLeft() + mMarginLeft + mPaddingLeft;
            right = left + orientationHelper.getDecoratedMeasurementInOther(view);
        }
        // whether this layout pass is layout to start or to end
        if (layoutState.getLayoutDirection() == VirtualLayoutManager.LayoutStateWrapper.LAYOUT_START) {
            // fill start, from bottom to top
            bottom = layoutState.getOffset() - startSpace - (isStartLine ? 0 : mDividerHeight);
            top = bottom - orientationHelper.getDecoratedMeasurement(view);
        } else {
            // fill end, from top to bottom
            top = layoutState.getOffset() + startSpace + (isStartLine ? 0 : mDividerHeight);
            bottom = top + orientationHelper.getDecoratedMeasurement(view);
        }
    } else {
        top = helper.getPaddingTop() + mMarginTop + mPaddingTop;
        bottom = top + orientationHelper.getDecoratedMeasurementInOther(view);
        if (layoutState.getLayoutDirection() == VirtualLayoutManager.LayoutStateWrapper.LAYOUT_START) {
            // fill left, from right to left
            right = layoutState.getOffset() - startSpace - (isStartLine ? 0 : mDividerHeight);
            left = right - orientationHelper.getDecoratedMeasurement(view);
        } else {
            // fill right, from left to right
            left = layoutState.getOffset() + startSpace + (isStartLine ? 0 : mDividerHeight);
            right = left + orientationHelper.getDecoratedMeasurement(view);
        }
    }
    // We calculate everything with View's bounding box (which includes decor and margins)
    // To calculate correct layout position, we subtract margins.
    layoutChild(view, left, top, right, bottom, helper);
    if (DEBUG) {
        Log.d(TAG, "laid out child at position " + helper.getPosition(view) + ", with l:" + (left + params.leftMargin) + ", t:" + (top + params.topMargin) + ", r:" + (right - params.rightMargin) + ", b:" + (bottom - params.bottomMargin));
    }
    handleStateOnResult(result, view);
}
Also used : OrientationHelper(android.support.v7.widget.OrientationHelper) VirtualLayoutManager(com.alibaba.android.vlayout.VirtualLayoutManager) RecyclerView(android.support.v7.widget.RecyclerView) View(android.view.View)

Example 5 with OrientationHelper

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

the class OnePlusNLayoutHelper method layoutViews.

@Override
public void layoutViews(RecyclerView.Recycler recycler, RecyclerView.State state, LayoutStateWrapper layoutState, LayoutChunkResult result, LayoutManagerHelper helper) {
    // reach the end of this layout
    if (isOutOfRange(layoutState.getCurrentPosition())) {
        return;
    }
    final int originCurPos = layoutState.getCurrentPosition();
    if (mChildrenViews == null || mChildrenViews.length != getItemCount()) {
        mChildrenViews = new View[getItemCount()];
    }
    int count = getAllChildren(mChildrenViews, recycler, layoutState, result, helper);
    if (count != getItemCount()) {
        Log.w(TAG, "The real number of children is not match with range of LayoutHelper");
    }
    final boolean layoutInVertical = helper.getOrientation() == VERTICAL;
    final OrientationHelper orientationHelper = helper.getMainOrientationHelper();
    final int parentWidth = helper.getContentWidth();
    final int parentHeight = helper.getContentHeight();
    final int parentHPadding = helper.getPaddingLeft() + helper.getPaddingRight() + getHorizontalMargin() + getHorizontalPadding();
    final int parentVPadding = helper.getPaddingTop() + helper.getPaddingBottom() + getVerticalMargin() + getVerticalPadding();
    int mainConsumed = 0;
    if (count == 1) {
        View view = mChildrenViews[0];
        final ViewGroup.MarginLayoutParams lp = new ViewGroup.MarginLayoutParams(view.getLayoutParams());
        if (!Float.isNaN(mAspectRatio)) {
            if (layoutInVertical) {
                lp.height = (int) ((parentWidth - parentHPadding) / mAspectRatio);
            } else {
                lp.width = (int) ((parentHeight - parentVPadding) * mAspectRatio);
            }
        }
        final float weight = getViewMainWeight(lp, 0);
        // fill width
        int widthSpec = helper.getChildMeasureSpec(Float.isNaN(weight) ? (parentWidth - parentHPadding) : (int) ((parentWidth - parentHPadding) * weight), layoutInVertical ? MATCH_PARENT : lp.width, !layoutInVertical);
        int heightSpec = helper.getChildMeasureSpec(parentHeight - parentVPadding, layoutInVertical ? lp.height : MeasureSpec.EXACTLY, layoutInVertical);
        helper.measureChild(view, widthSpec, heightSpec);
        mainConsumed = orientationHelper.getDecoratedMeasurement(view) + (layoutInVertical ? getVerticalMargin() + getVerticalPadding() : getHorizontalMargin() + getHorizontalPadding());
        calculateRect(mainConsumed, mAreaRect, layoutState, helper);
        layoutChild(view, mAreaRect.left, mAreaRect.top, mAreaRect.right, mAreaRect.bottom, helper);
        handleStateOnResult(result, view);
    } else if (count == 2) {
        final View child1 = mChildrenViews[0];
        final ViewGroup.MarginLayoutParams lp1 = new ViewGroup.MarginLayoutParams(child1.getLayoutParams());
        final View child2 = mChildrenViews[1];
        final ViewGroup.MarginLayoutParams lp2 = new ViewGroup.MarginLayoutParams(child2.getLayoutParams());
        final float weight1 = getViewMainWeight(lp1, 0);
        final float weight2 = getViewMainWeight(lp1, 1);
        if (layoutInVertical) {
            if (!Float.isNaN(mAspectRatio)) {
                lp1.height = lp2.height = (int) ((parentWidth - parentHPadding) / mAspectRatio);
            }
            lp2.topMargin = lp1.topMargin;
            lp2.bottomMargin = lp1.bottomMargin;
            int availableSpace = parentWidth - parentHPadding - lp1.leftMargin - lp1.rightMargin - lp2.leftMargin - lp2.rightMargin;
            int width1 = Float.isNaN(weight1) ? (int) (availableSpace / 2.0f + 0.5f) : (int) (availableSpace * weight1 / 100 + 0.5f);
            int width2 = Float.isNaN(weight2) ? (availableSpace - width1) : (int) (availableSpace * weight2 / 100 + 0.5f);
            helper.measureChild(child1, MeasureSpec.makeMeasureSpec(width1 + lp1.leftMargin + lp1.rightMargin, MeasureSpec.EXACTLY), helper.getChildMeasureSpec(helper.getContentHeight(), lp1.height, true));
            helper.measureChild(child2, MeasureSpec.makeMeasureSpec(width2 + lp2.leftMargin + lp2.rightMargin, MeasureSpec.EXACTLY), helper.getChildMeasureSpec(helper.getContentHeight(), lp2.height, true));
            mainConsumed = Math.max(orientationHelper.getDecoratedMeasurement(child1), orientationHelper.getDecoratedMeasurement(child2)) + getVerticalMargin() + getVerticalPadding();
            calculateRect(mainConsumed - getVerticalMargin() - getVerticalPadding(), mAreaRect, layoutState, helper);
            int right1 = mAreaRect.left + orientationHelper.getDecoratedMeasurementInOther(child1);
            layoutChild(child1, mAreaRect.left, mAreaRect.top, right1, mAreaRect.bottom, helper);
            layoutChild(child2, right1, mAreaRect.top, right1 + orientationHelper.getDecoratedMeasurementInOther(child2), mAreaRect.bottom, helper);
        } else {
            if (!Float.isNaN(mAspectRatio)) {
                lp1.width = lp2.width = (int) ((parentHeight - parentVPadding) * mAspectRatio);
            }
            int availableSpace = parentHeight - parentVPadding - lp1.topMargin - lp1.bottomMargin - lp2.topMargin - lp2.bottomMargin;
            int height1 = Float.isNaN(weight1) ? (int) (availableSpace / 2.0f + 0.5f) : (int) (availableSpace * weight1 / 100 + 0.5f);
            int height2 = Float.isNaN(weight2) ? (int) (availableSpace - height1) : (int) (availableSpace * weight2 / 100 + 0.5f);
            helper.measureChild(child1, helper.getChildMeasureSpec(helper.getContentWidth(), lp1.width, true), MeasureSpec.makeMeasureSpec(height1 + lp1.topMargin + lp1.bottomMargin, MeasureSpec.EXACTLY));
            int width = child1.getMeasuredWidth();
            helper.measureChild(child2, MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(height2 + lp2.topMargin + lp2.bottomMargin, MeasureSpec.EXACTLY));
            mainConsumed = Math.max(orientationHelper.getDecoratedMeasurement(child1), orientationHelper.getDecoratedMeasurement(child2)) + getHorizontalMargin() + getHorizontalPadding();
            calculateRect(mainConsumed - getHorizontalPadding() - getHorizontalMargin(), mAreaRect, layoutState, helper);
            int bottom1 = mAreaRect.top + orientationHelper.getDecoratedMeasurementInOther(child1);
            layoutChild(child1, mAreaRect.left, mAreaRect.top, mAreaRect.right, bottom1, helper);
            layoutChild(child2, mAreaRect.left, bottom1, mAreaRect.right, bottom1 + orientationHelper.getDecoratedMeasurementInOther(child2), helper);
        }
        handleStateOnResult(result, child1, child2);
    } else if (count == 3) {
        final View child1 = mChildrenViews[0];
        final ViewGroup.MarginLayoutParams lp1 = new ViewGroup.MarginLayoutParams(child1.getLayoutParams());
        final View child2 = helper.getReverseLayout() ? mChildrenViews[2] : mChildrenViews[1];
        final View child3 = helper.getReverseLayout() ? mChildrenViews[1] : mChildrenViews[2];
        final ViewGroup.MarginLayoutParams lp2 = new ViewGroup.MarginLayoutParams(child2.getLayoutParams());
        final ViewGroup.MarginLayoutParams lp3 = new ViewGroup.MarginLayoutParams(child3.getLayoutParams());
        final float weight1 = getViewMainWeight(lp1, 0);
        final float weight2 = getViewMainWeight(lp1, 1);
        final float weight3 = getViewMainWeight(lp1, 2);
        if (layoutInVertical) {
            if (!Float.isNaN(mAspectRatio)) {
                lp1.height = (int) ((parentWidth - parentHPadding) / mAspectRatio);
            }
            // make border consistent
            lp2.topMargin = lp1.topMargin;
            lp3.bottomMargin = lp1.bottomMargin;
            lp3.leftMargin = lp2.leftMargin;
            lp3.rightMargin = lp2.rightMargin;
            int availableSpace = parentWidth - parentHPadding - lp1.leftMargin - lp1.rightMargin - lp2.leftMargin - lp2.rightMargin;
            int width1 = Float.isNaN(weight1) ? (int) (availableSpace / 2.0f + 0.5f) : (int) (availableSpace * weight1 / 100 + 0.5f);
            int width2 = Float.isNaN(weight2) ? (int) (availableSpace - width1) : (int) (availableSpace * weight2 / 100 + 0.5);
            int width3 = Float.isNaN(weight3) ? (int) (width2) : (int) (availableSpace * weight3 / 100 + 0.5);
            helper.measureChild(child1, MeasureSpec.makeMeasureSpec(width1 + lp1.leftMargin + lp1.rightMargin, MeasureSpec.EXACTLY), helper.getChildMeasureSpec(helper.getContentHeight(), lp1.height, true));
            int height1 = child1.getMeasuredHeight();
            int height2 = Float.isNaN(mRowWeight) ? (int) ((height1 - lp2.bottomMargin - lp3.topMargin) / 2.0f + 0.5f) : (int) ((height1 - lp2.bottomMargin - lp3.topMargin) * mRowWeight / 100 + 0.5f);
            int height3 = height1 - lp2.bottomMargin - lp3.topMargin - height2;
            helper.measureChild(child2, MeasureSpec.makeMeasureSpec(width2 + lp2.leftMargin + lp2.rightMargin, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(height2 + lp2.topMargin + lp2.bottomMargin, MeasureSpec.EXACTLY));
            helper.measureChild(child3, MeasureSpec.makeMeasureSpec(width3 + lp3.leftMargin + lp3.rightMargin, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(height3 + lp3.topMargin + lp3.bottomMargin, MeasureSpec.EXACTLY));
            mainConsumed = Math.max(height1 + lp1.topMargin + lp1.bottomMargin, height2 + lp2.topMargin + lp2.bottomMargin + height3 + lp3.topMargin + lp3.bottomMargin) + getVerticalMargin() + getVerticalPadding();
            calculateRect(mainConsumed - getVerticalMargin() - getVerticalPadding(), mAreaRect, layoutState, helper);
            int right1 = mAreaRect.left + orientationHelper.getDecoratedMeasurementInOther(child1);
            layoutChild(child1, mAreaRect.left, mAreaRect.top, right1, mAreaRect.bottom, helper);
            int right2 = right1 + orientationHelper.getDecoratedMeasurementInOther(child2);
            layoutChild(child2, right1, mAreaRect.top, right2, mAreaRect.top + child2.getMeasuredHeight() + lp2.topMargin + lp2.bottomMargin, helper);
            layoutChild(child3, right1, mAreaRect.bottom - orientationHelper.getDecoratedMeasurement(child3), right1 + orientationHelper.getDecoratedMeasurementInOther(child3), mAreaRect.bottom, helper);
        } else {
        // TODO: horizontal support
        }
        handleStateOnResult(result, child1, child2, child3);
    } else if (count == 4) {
        final View child1 = mChildrenViews[0];
        final VirtualLayoutManager.LayoutParams lp1 = new VirtualLayoutManager.LayoutParams(child1.getLayoutParams());
        final View child2 = helper.getReverseLayout() ? mChildrenViews[3] : mChildrenViews[1];
        final VirtualLayoutManager.LayoutParams lp2 = new VirtualLayoutManager.LayoutParams(child2.getLayoutParams());
        final View child3 = mChildrenViews[2];
        final VirtualLayoutManager.LayoutParams lp3 = new VirtualLayoutManager.LayoutParams(child3.getLayoutParams());
        final View child4 = helper.getReverseLayout() ? mChildrenViews[1] : mChildrenViews[3];
        final VirtualLayoutManager.LayoutParams lp4 = new VirtualLayoutManager.LayoutParams(child4.getLayoutParams());
        final float weight1 = getViewMainWeight(lp1, 0);
        final float weight2 = getViewMainWeight(lp1, 1);
        final float weight3 = getViewMainWeight(lp1, 2);
        final float weight4 = getViewMainWeight(lp1, 3);
        if (layoutInVertical) {
            lp2.topMargin = lp1.topMargin;
            lp3.bottomMargin = lp4.bottomMargin = lp1.bottomMargin;
            lp3.leftMargin = lp2.leftMargin;
            lp4.rightMargin = lp2.rightMargin;
            if (!Float.isNaN(mAspectRatio)) {
                lp1.height = (int) ((parentWidth - parentHPadding) / mAspectRatio);
            }
            int availableSpace = parentWidth - parentHPadding - lp1.leftMargin - lp1.rightMargin - lp2.leftMargin - lp2.rightMargin;
            int width1 = Float.isNaN(weight1) ? (int) (availableSpace / 2.0f + 0.5f) : (int) (availableSpace * weight1 / 100 + 0.5f);
            int width2 = Float.isNaN(weight2) ? (int) (availableSpace - width1) : (int) (availableSpace * weight2 / 100 + 0.5f);
            int width3 = Float.isNaN(weight3) ? (int) ((width2 - lp3.rightMargin - lp4.leftMargin) / 2.0f + 0.5f) : (int) (availableSpace * weight3 / 100 + 0.5f);
            int width4 = Float.isNaN(weight4) ? (int) ((width2 - lp3.rightMargin - lp4.leftMargin - width3)) : (int) (availableSpace * weight4 / 100 + 0.5f);
            helper.measureChild(child1, MeasureSpec.makeMeasureSpec(width1 + lp1.leftMargin + lp1.rightMargin, MeasureSpec.EXACTLY), helper.getChildMeasureSpec(helper.getContentHeight(), lp1.height, true));
            int height1 = child1.getMeasuredHeight();
            int height2 = Float.isNaN(mRowWeight) ? (int) ((height1 - lp2.bottomMargin - lp3.topMargin) / 2.0f + 0.5f) : (int) ((height1 - lp2.bottomMargin - lp3.topMargin) * mRowWeight / 100 + 0.5f);
            int height3 = (int) ((height1 - lp2.bottomMargin - lp3.topMargin) - height2);
            helper.measureChild(child2, MeasureSpec.makeMeasureSpec(width2 + lp2.leftMargin + lp2.rightMargin, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(height2 + lp2.topMargin + lp2.bottomMargin, MeasureSpec.EXACTLY));
            helper.measureChild(child3, MeasureSpec.makeMeasureSpec(width3 + lp3.leftMargin + lp3.rightMargin, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(height3 + lp3.topMargin + lp3.bottomMargin, MeasureSpec.EXACTLY));
            helper.measureChild(child4, MeasureSpec.makeMeasureSpec(width4 + lp4.leftMargin + lp4.rightMargin, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(height3 + lp4.topMargin + lp4.bottomMargin, MeasureSpec.EXACTLY));
            mainConsumed = Math.max(height1 + lp1.topMargin + lp1.bottomMargin, height2 + lp2.topMargin + lp2.bottomMargin + Math.max(height3 + lp3.topMargin + lp3.bottomMargin, height3 + lp4.topMargin + lp4.bottomMargin)) + getVerticalMargin() + getVerticalPadding();
            calculateRect(mainConsumed - getVerticalMargin() - getVerticalPadding(), mAreaRect, layoutState, helper);
            int right1 = mAreaRect.left + orientationHelper.getDecoratedMeasurementInOther(child1);
            layoutChild(child1, mAreaRect.left, mAreaRect.top, right1, mAreaRect.bottom, helper);
            int right2 = right1 + orientationHelper.getDecoratedMeasurementInOther(child2);
            layoutChild(child2, right1, mAreaRect.top, right2, mAreaRect.top + orientationHelper.getDecoratedMeasurement(child2), helper);
            int right3 = right1 + orientationHelper.getDecoratedMeasurementInOther(child3);
            layoutChild(child3, right1, mAreaRect.bottom - orientationHelper.getDecoratedMeasurement(child3), right3, mAreaRect.bottom, helper);
            layoutChild(child4, right3, mAreaRect.bottom - orientationHelper.getDecoratedMeasurement(child4), right3 + orientationHelper.getDecoratedMeasurementInOther(child4), mAreaRect.bottom, helper);
        } else {
        // TODO: horizontal support
        }
        handleStateOnResult(result, child1, child2, child3, child4);
    } else if (count == 5) {
        final View child1 = mChildrenViews[0];
        final VirtualLayoutManager.LayoutParams lp1 = new VirtualLayoutManager.LayoutParams(child1.getLayoutParams());
        final View child2 = helper.getReverseLayout() ? mChildrenViews[4] : mChildrenViews[1];
        final VirtualLayoutManager.LayoutParams lp2 = new VirtualLayoutManager.LayoutParams(child2.getLayoutParams());
        final View child3 = helper.getReverseLayout() ? mChildrenViews[3] : mChildrenViews[2];
        final VirtualLayoutManager.LayoutParams lp3 = new VirtualLayoutManager.LayoutParams(child3.getLayoutParams());
        final View child4 = helper.getReverseLayout() ? mChildrenViews[2] : mChildrenViews[3];
        final VirtualLayoutManager.LayoutParams lp4 = new VirtualLayoutManager.LayoutParams(child4.getLayoutParams());
        final View child5 = helper.getReverseLayout() ? mChildrenViews[1] : mChildrenViews[4];
        final VirtualLayoutManager.LayoutParams lp5 = new VirtualLayoutManager.LayoutParams(child5.getLayoutParams());
        final float weight1 = getViewMainWeight(lp1, 0);
        final float weight2 = getViewMainWeight(lp1, 1);
        final float weight3 = getViewMainWeight(lp1, 2);
        final float weight4 = getViewMainWeight(lp1, 3);
        final float weight5 = getViewMainWeight(lp1, 4);
        if (layoutInVertical) {
            lp2.topMargin = lp1.topMargin;
            lp3.bottomMargin = lp4.bottomMargin = lp1.bottomMargin;
            lp3.leftMargin = lp2.leftMargin;
            lp4.rightMargin = lp2.rightMargin;
            lp5.rightMargin = lp2.rightMargin;
            if (!Float.isNaN(mAspectRatio)) {
                lp1.height = (int) ((parentWidth - parentHPadding) / mAspectRatio);
            }
            int availableSpace = parentWidth - parentHPadding - lp1.leftMargin - lp1.rightMargin - lp2.leftMargin - lp2.rightMargin;
            int width1 = Float.isNaN(weight1) ? (int) (availableSpace / 2.0f + 0.5f) : (int) (availableSpace * weight1 / 100 + 0.5f);
            int width2 = Float.isNaN(weight2) ? (int) (availableSpace - width1) : (int) (availableSpace * weight2 / 100 + 0.5f);
            int width3 = Float.isNaN(weight3) ? (int) ((width2 - lp3.rightMargin - lp4.leftMargin) / 3.0f + 0.5f) : (int) (availableSpace * weight3 / 100 + 0.5f);
            int width4 = Float.isNaN(weight4) ? (int) ((width2 - lp3.rightMargin - lp4.leftMargin) / 3.0f + 0.5f) : (int) (availableSpace * weight4 / 100 + 0.5f);
            int width5 = Float.isNaN(weight5) ? (int) ((width2 - lp3.rightMargin - lp4.leftMargin - width3 - width4)) : (int) (availableSpace * weight5 / 100 + 0.5f);
            helper.measureChild(child1, MeasureSpec.makeMeasureSpec(width1 + lp1.leftMargin + lp1.rightMargin, MeasureSpec.EXACTLY), helper.getChildMeasureSpec(helper.getContentHeight(), lp1.height, true));
            int height1 = child1.getMeasuredHeight();
            int height2 = Float.isNaN(mRowWeight) ? (int) ((height1 - lp2.bottomMargin - lp3.topMargin) / 2.0f + 0.5f) : (int) ((height1 - lp2.bottomMargin - lp3.topMargin) * mRowWeight / 100 + 0.5f);
            int height3 = (int) ((height1 - lp2.bottomMargin - lp3.topMargin) - height2);
            helper.measureChild(child2, MeasureSpec.makeMeasureSpec(width2 + lp2.leftMargin + lp2.rightMargin, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(height2 + lp2.topMargin + lp2.bottomMargin, MeasureSpec.EXACTLY));
            helper.measureChild(child3, MeasureSpec.makeMeasureSpec(width3 + lp3.leftMargin + lp3.rightMargin, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(height3 + lp3.topMargin + lp3.bottomMargin, MeasureSpec.EXACTLY));
            helper.measureChild(child4, MeasureSpec.makeMeasureSpec(width4 + lp4.leftMargin + lp4.rightMargin, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(height3 + lp4.topMargin + lp4.bottomMargin, MeasureSpec.EXACTLY));
            helper.measureChild(child5, MeasureSpec.makeMeasureSpec(width5 + lp5.leftMargin + lp5.rightMargin, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(height3 + lp5.topMargin + lp5.bottomMargin, MeasureSpec.EXACTLY));
            mainConsumed = Math.max(height1 + lp1.topMargin + lp1.bottomMargin, height2 + lp2.topMargin + lp2.bottomMargin + Math.max(height3 + lp3.topMargin + lp3.bottomMargin, height3 + lp4.topMargin + lp4.bottomMargin)) + getVerticalMargin() + getVerticalPadding();
            calculateRect(mainConsumed - getVerticalMargin() - getVerticalPadding(), mAreaRect, layoutState, helper);
            int right1 = mAreaRect.left + orientationHelper.getDecoratedMeasurementInOther(child1);
            layoutChild(child1, mAreaRect.left, mAreaRect.top, right1, mAreaRect.bottom, helper);
            int right2 = right1 + orientationHelper.getDecoratedMeasurementInOther(child2);
            layoutChild(child2, right1, mAreaRect.top, right2, mAreaRect.top + orientationHelper.getDecoratedMeasurement(child2), helper);
            int right3 = right1 + orientationHelper.getDecoratedMeasurementInOther(child3);
            layoutChild(child3, right1, mAreaRect.bottom - orientationHelper.getDecoratedMeasurement(child3), right3, mAreaRect.bottom, helper);
            int right4 = right3 + orientationHelper.getDecoratedMeasurementInOther(child4);
            layoutChild(child4, right3, mAreaRect.bottom - orientationHelper.getDecoratedMeasurement(child4), right3 + orientationHelper.getDecoratedMeasurementInOther(child4), mAreaRect.bottom, helper);
            layoutChild(child5, right4, mAreaRect.bottom - orientationHelper.getDecoratedMeasurement(child5), right4 + orientationHelper.getDecoratedMeasurementInOther(child5), mAreaRect.bottom, helper);
        } else {
        // TODO: horizontal support
        }
        handleStateOnResult(result, child1, child2, child3, child4, child5);
    }
    result.mConsumed = mainConsumed;
    Arrays.fill(mChildrenViews, null);
}
Also used : OrientationHelper(android.support.v7.widget.OrientationHelper) ViewGroup(android.view.ViewGroup) VirtualLayoutManager(com.alibaba.android.vlayout.VirtualLayoutManager) RecyclerView(android.support.v7.widget.RecyclerView) View(android.view.View)

Aggregations

OrientationHelper (android.support.v7.widget.OrientationHelper)21 RecyclerView (android.support.v7.widget.RecyclerView)19 View (android.view.View)19 VirtualLayoutManager (com.alibaba.android.vlayout.VirtualLayoutManager)10 LayoutParams (android.support.v7.widget.RecyclerView.LayoutParams)4 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)2 ViewGroup (android.view.ViewGroup)2 LayoutParams (android.view.ViewGroup.LayoutParams)1 TextView (android.widget.TextView)1 LayoutHelper (com.alibaba.android.vlayout.LayoutHelper)1 LayoutParams (com.alibaba.android.vlayout.VirtualLayoutManager.LayoutParams)1