Search in sources :

Example 21 with Recycler

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

the class OnePlusNLayoutHelperEx method layoutViews.

@Override
public void layoutViews(RecyclerView.Recycler recycler, RecyclerView.State state, LayoutStateWrapper layoutState, LayoutChunkResult result, LayoutManagerHelper helper) {
    // reach the end of this layout
    final int originCurPos = layoutState.getCurrentPosition();
    if (isOutOfRange(originCurPos)) {
        return;
    }
    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 == 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 = lp3.rightMargin;
            if (!Float.isNaN(mAspectRatio)) {
                lp1.height = (int) ((parentWidth - parentHPadding) / mAspectRatio);
            }
            int availableSpace = parentWidth - parentHPadding - lp1.leftMargin - lp1.rightMargin - lp2.leftMargin - lp2.rightMargin - lp3.leftMargin - lp3.rightMargin;
            int width1 = Float.isNaN(weight1) ? (int) (availableSpace / 3.0f + 0.5f) : (int) (availableSpace * weight1 / 100 + 0.5f);
            int width2 = Float.isNaN(weight2) ? (availableSpace - width1) / 2 : (int) (availableSpace * weight2 / 100 + 0.5f);
            int width3 = Float.isNaN(weight3) ? width2 : (int) (availableSpace * weight3 / 100 + 0.5f);
            int width4 = Float.isNaN(weight4) ? width2 : (int) (availableSpace * weight4 / 100 + 0.5f);
            int width5 = Float.isNaN(weight5) ? width2 : (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 = (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 = right2 + orientationHelper.getDecoratedMeasurementInOther(child3);
            layoutChild(child3, right2, mAreaRect.top, right3, mAreaRect.top + orientationHelper.getDecoratedMeasurement(child3), helper);
            int right4 = right1 + orientationHelper.getDecoratedMeasurementInOther(child4);
            layoutChild(child4, right1, mAreaRect.bottom - orientationHelper.getDecoratedMeasurement(child4), right4, 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);
    } else if (count == 6) {
        // added at 2017/3/7 can extract method
        final View child1 = mChildrenViews[0];
        final VirtualLayoutManager.LayoutParams lp1 = new VirtualLayoutManager.LayoutParams(child1.getLayoutParams());
        final View child2 = helper.getReverseLayout() ? mChildrenViews[5] : mChildrenViews[1];
        final VirtualLayoutManager.LayoutParams lp2 = new VirtualLayoutManager.LayoutParams(child2.getLayoutParams());
        final View child3 = helper.getReverseLayout() ? mChildrenViews[4] : mChildrenViews[2];
        final VirtualLayoutManager.LayoutParams lp3 = new VirtualLayoutManager.LayoutParams(child3.getLayoutParams());
        final View child4 = helper.getReverseLayout() ? mChildrenViews[3] : mChildrenViews[3];
        final VirtualLayoutManager.LayoutParams lp4 = new VirtualLayoutManager.LayoutParams(child4.getLayoutParams());
        final View child5 = helper.getReverseLayout() ? mChildrenViews[2] : mChildrenViews[4];
        final VirtualLayoutManager.LayoutParams lp5 = new VirtualLayoutManager.LayoutParams(child5.getLayoutParams());
        final View child6 = helper.getReverseLayout() ? mChildrenViews[1] : mChildrenViews[5];
        final VirtualLayoutManager.LayoutParams lp6 = new VirtualLayoutManager.LayoutParams(child6.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);
        final float weight6 = getViewMainWeight(lp1, 5);
        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) ? availableSpace - width1 : (int) (availableSpace * weight2 / 100 + 0.5f);
            int width3 = Float.isNaN(weight3) ? width2 : (int) (availableSpace * weight3 / 100 + 0.5);
            int bottomavailableSpace = parentWidth - parentHPadding - lp4.leftMargin - lp4.rightMargin - lp5.leftMargin - lp5.rightMargin - lp6.leftMargin - lp6.rightMargin;
            int width4 = Float.isNaN(weight4) ? (int) (bottomavailableSpace / 3.0f + 0.5f) : (int) (availableSpace * weight4 / 100 + 0.5f);
            int width5 = Float.isNaN(weight5) ? width4 : (int) (availableSpace * weight5 / 100 + 0.5f);
            int width6 = Float.isNaN(weight6) ? width4 : (int) (availableSpace * weight6 / 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 = (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));
            helper.measureChild(child6, MeasureSpec.makeMeasureSpec(width6 + lp6.leftMargin + lp6.rightMargin, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(height3 + lp6.topMargin + lp6.bottomMargin, MeasureSpec.EXACTLY));
            int maxTopHeight = Math.max(height1 + lp1.topMargin + lp1.bottomMargin, (height2 + lp2.topMargin + lp2.bottomMargin) * 2);
            int maxBottomHeight = Math.max(height3 + lp4.topMargin + lp4.bottomMargin, Math.max(height3 + lp5.topMargin + lp5.bottomMargin, height3 + lp6.topMargin + lp6.bottomMargin));
            mainConsumed = maxTopHeight + maxBottomHeight + 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 - orientationHelper.getDecoratedMeasurement(child4), 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.top + orientationHelper.getDecoratedMeasurement(child3), right3, mAreaRect.bottom - orientationHelper.getDecoratedMeasurement(child4), helper);
            int right4 = mAreaRect.left + orientationHelper.getDecoratedMeasurementInOther(child4);
            layoutChild(child4, mAreaRect.left, mAreaRect.bottom - orientationHelper.getDecoratedMeasurement(child4), right4, mAreaRect.bottom, helper);
            int right5 = right4 + orientationHelper.getDecoratedMeasurementInOther(child5);
            layoutChild(child5, right4, mAreaRect.bottom - orientationHelper.getDecoratedMeasurement(child5), right5, mAreaRect.bottom, helper);
            int right6 = right5 + orientationHelper.getDecoratedMeasurementInOther(child6);
            layoutChild(child6, right5, mAreaRect.bottom - orientationHelper.getDecoratedMeasurement(child6), right6, mAreaRect.bottom, helper);
        } else {
        // TODO: horizontal support
        }
        handleStateOnResult(result, child1, child2, child3, child4, child5, child6);
    } else if (count == 7) {
        final View child1 = mChildrenViews[0];
        final VirtualLayoutManager.LayoutParams lp1 = new VirtualLayoutManager.LayoutParams(child1.getLayoutParams());
        final View child2 = helper.getReverseLayout() ? mChildrenViews[6] : mChildrenViews[1];
        final VirtualLayoutManager.LayoutParams lp2 = new VirtualLayoutManager.LayoutParams(child2.getLayoutParams());
        final View child3 = helper.getReverseLayout() ? mChildrenViews[5] : mChildrenViews[2];
        final VirtualLayoutManager.LayoutParams lp3 = new VirtualLayoutManager.LayoutParams(child3.getLayoutParams());
        final View child4 = helper.getReverseLayout() ? mChildrenViews[4] : mChildrenViews[3];
        final VirtualLayoutManager.LayoutParams lp4 = new VirtualLayoutManager.LayoutParams(child4.getLayoutParams());
        final View child5 = helper.getReverseLayout() ? mChildrenViews[3] : mChildrenViews[4];
        final VirtualLayoutManager.LayoutParams lp5 = new VirtualLayoutManager.LayoutParams(child5.getLayoutParams());
        final View child6 = helper.getReverseLayout() ? mChildrenViews[2] : mChildrenViews[5];
        final VirtualLayoutManager.LayoutParams lp6 = new VirtualLayoutManager.LayoutParams(child6.getLayoutParams());
        final View child7 = helper.getReverseLayout() ? mChildrenViews[1] : mChildrenViews[6];
        final VirtualLayoutManager.LayoutParams lp7 = new VirtualLayoutManager.LayoutParams(child7.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);
        final float weight6 = getViewMainWeight(lp1, 5);
        final float weight7 = getViewMainWeight(lp1, 6);
        if (layoutInVertical) {
            if (!Float.isNaN(mAspectRatio)) {
                lp1.height = (int) ((parentWidth - parentHPadding) / mAspectRatio);
            }
            int availableSpace = parentWidth - parentHPadding - lp1.leftMargin - lp1.rightMargin - lp2.leftMargin - lp2.rightMargin - lp3.leftMargin - lp3.rightMargin;
            int width1 = Float.isNaN(weight1) ? (int) (availableSpace / 3.0f + 0.5f) : (int) (availableSpace * weight1 / 100 + 0.5f);
            int width2 = Float.isNaN(weight2) ? (availableSpace - width1) / 2 : (int) (availableSpace * weight2 / 100 + 0.5f);
            int width3 = Float.isNaN(weight3) ? width2 : (int) (availableSpace * weight3 / 100 + 0.5);
            int width4 = Float.isNaN(weight4) ? width2 : (int) (availableSpace * weight4 / 100 + 0.5f);
            int width5 = Float.isNaN(weight5) ? width2 : (int) (availableSpace * weight5 / 100 + 0.5f);
            int width6 = Float.isNaN(weight6) ? width2 : (int) (availableSpace * weight6 / 100 + 0.5f);
            int width7 = Float.isNaN(weight6) ? width2 : (int) (availableSpace * weight7 / 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) / 3.0f + 0.5f) : (int) ((height1 - lp2.bottomMargin - lp3.topMargin) * mRowWeight / 100 + 0.5f);
            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(height2 + lp3.topMargin + lp3.bottomMargin, MeasureSpec.EXACTLY));
            helper.measureChild(child4, MeasureSpec.makeMeasureSpec(width4 + lp4.leftMargin + lp4.rightMargin, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(height2 + lp4.topMargin + lp4.bottomMargin, MeasureSpec.EXACTLY));
            helper.measureChild(child5, MeasureSpec.makeMeasureSpec(width5 + lp5.leftMargin + lp5.rightMargin, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(height2 + lp5.topMargin + lp5.bottomMargin, MeasureSpec.EXACTLY));
            helper.measureChild(child6, MeasureSpec.makeMeasureSpec(width6 + lp6.leftMargin + lp6.rightMargin, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(height2 + lp6.topMargin + lp6.bottomMargin, MeasureSpec.EXACTLY));
            helper.measureChild(child7, MeasureSpec.makeMeasureSpec(width7 + lp7.leftMargin + lp7.rightMargin, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(height2 + lp7.topMargin + lp7.bottomMargin, MeasureSpec.EXACTLY));
            int maxRightHeight = Math.max(height2 + lp2.topMargin + lp2.bottomMargin, height2 + lp3.topMargin + lp3.bottomMargin) + Math.max(height2 + lp4.topMargin + lp4.bottomMargin, height2 + lp5.topMargin + lp5.bottomMargin) + Math.max(height2 + lp6.topMargin + lp6.bottomMargin, height2 + lp7.topMargin + lp7.bottomMargin);
            int maxHeight = Math.max(height1 + lp1.topMargin + lp1.bottomMargin, maxRightHeight);
            mainConsumed = maxHeight + 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 = right2 + orientationHelper.getDecoratedMeasurementInOther(child3);
            layoutChild(child3, right2, mAreaRect.top, right3, mAreaRect.top + orientationHelper.getDecoratedMeasurement(child3), helper);
            int right4 = right1 + orientationHelper.getDecoratedMeasurementInOther(child4);
            layoutChild(child4, right1, mAreaRect.top + orientationHelper.getDecoratedMeasurement(child2), right4, mAreaRect.bottom - orientationHelper.getDecoratedMeasurement(child6), helper);
            int right5 = right4 + orientationHelper.getDecoratedMeasurementInOther(child5);
            layoutChild(child5, right4, mAreaRect.top + orientationHelper.getDecoratedMeasurement(child2), right5, mAreaRect.bottom - orientationHelper.getDecoratedMeasurement(child7), helper);
            int right6 = right1 + orientationHelper.getDecoratedMeasurementInOther(child6);
            layoutChild(child6, right1, mAreaRect.bottom - orientationHelper.getDecoratedMeasurement(child6), right6, mAreaRect.bottom, helper);
            layoutChild(child7, right6, mAreaRect.bottom - orientationHelper.getDecoratedMeasurement(child7), right6 + orientationHelper.getDecoratedMeasurementInOther(child7), mAreaRect.bottom, helper);
        } else {
        // TODO: horizontal support
        }
        handleStateOnResult(result, child1, child2, child3, child4, child5, child6);
    }
    result.mConsumed = mainConsumed;
    Arrays.fill(mChildrenViews, null);
}
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 22 with Recycler

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

the class SingleLayoutHelper 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;
    }
    View view = layoutState.next(recycler);
    if (view == null) {
        result.mFinished = true;
        return;
    }
    helper.addChildView(layoutState, view);
    final VirtualLayoutManager.LayoutParams params = (VirtualLayoutManager.LayoutParams) view.getLayoutParams();
    final boolean layoutInVertical = helper.getOrientation() == VERTICAL;
    int parentWidth = helper.getContentWidth() - helper.getPaddingLeft() - helper.getPaddingRight() - getHorizontalMargin() - getHorizontalPadding();
    int parentHeight = helper.getContentHeight() - helper.getPaddingTop() - helper.getPaddingBottom() - getVerticalMargin() - getVerticalPadding();
    if (!Float.isNaN(mAspectRatio)) {
        if (layoutInVertical) {
            parentHeight = (int) (parentWidth / mAspectRatio + 0.5f);
        } else {
            parentWidth = (int) (parentHeight * mAspectRatio + 0.5f);
        }
    }
    if (layoutInVertical) {
        final int widthSpec = helper.getChildMeasureSpec(parentWidth, Float.isNaN(mAspectRatio) ? params.width : parentWidth, !layoutInVertical && Float.isNaN(mAspectRatio));
        final int heightSpec = helper.getChildMeasureSpec(parentHeight, Float.isNaN(params.mAspectRatio) ? (Float.isNaN(mAspectRatio) ? params.height : parentHeight) : (int) (parentWidth / params.mAspectRatio + 0.5f), layoutInVertical && Float.isNaN(mAspectRatio));
        // do measurement
        helper.measureChild(view, widthSpec, heightSpec);
    } else {
        final int widthSpec = helper.getChildMeasureSpec(parentWidth, Float.isNaN(params.mAspectRatio) ? (Float.isNaN(mAspectRatio) ? params.width : parentWidth) : (int) (parentHeight * params.mAspectRatio + 0.5f), !layoutInVertical && Float.isNaN(mAspectRatio));
        final int heightSpec = helper.getChildMeasureSpec(parentHeight, Float.isNaN(mAspectRatio) ? params.height : parentHeight, layoutInVertical && Float.isNaN(mAspectRatio));
        // do measurement
        helper.measureChild(view, widthSpec, heightSpec);
    }
    OrientationHelper orientationHelper = helper.getMainOrientationHelper();
    result.mConsumed = orientationHelper.getDecoratedMeasurement(view);
    // do layout
    int left, top, right, bottom;
    if (layoutInVertical) {
        int viewWidth = orientationHelper.getDecoratedMeasurementInOther(view);
        int available = parentWidth - viewWidth;
        if (available < 0) {
            available = 0;
        }
        left = mMarginLeft + mPaddingLeft + helper.getPaddingLeft() + available / 2;
        right = helper.getContentWidth() - mMarginRight - mPaddingRight - helper.getPaddingRight() - available / 2;
        if (layoutState.getLayoutDirection() == VirtualLayoutManager.LayoutStateWrapper.LAYOUT_START) {
            bottom = layoutState.getOffset() - mMarginBottom - mPaddingBottom;
            top = bottom - result.mConsumed;
        } else {
            top = layoutState.getOffset() + mMarginTop + mPaddingTop;
            bottom = top + result.mConsumed;
        }
    } else {
        int viewHeight = orientationHelper.getDecoratedMeasurementInOther(view);
        int available = parentHeight - viewHeight;
        if (available < 0) {
            available = 0;
        }
        top = helper.getPaddingTop() + mMarginTop + mPaddingTop + available / 2;
        bottom = helper.getContentHeight() - -mMarginBottom - mPaddingBottom - helper.getPaddingBottom() - available / 2;
        if (layoutState.getLayoutDirection() == VirtualLayoutManager.LayoutStateWrapper.LAYOUT_START) {
            right = layoutState.getOffset() - mMarginRight - mPaddingRight;
            left = right - result.mConsumed;
        } else {
            left = layoutState.getOffset() + mMarginLeft + mPaddingLeft;
            right = left + result.mConsumed;
        }
    }
    if (layoutInVertical) {
        result.mConsumed += getVerticalMargin() + getVerticalPadding();
    } else {
        result.mConsumed += getHorizontalMargin() + getHorizontalPadding();
    }
    layoutChild(view, left, top, right, bottom, helper);
}
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 23 with Recycler

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

the class VirtualLayoutManager method detachAndScrapAttachedViews.

@Override
public void detachAndScrapAttachedViews(RecyclerView.Recycler recycler) {
    int childCount = this.getChildCount();
    for (int i = childCount - 1; i >= 0; --i) {
        View v = this.getChildAt(i);
        RecyclerView.ViewHolder holder = getChildViewHolder(v);
        if (holder instanceof CacheViewHolder && ((CacheViewHolder) holder).needCached()) {
            // mark not invalid, ignore DataSetChange(), make the ViewHolder itself to maitain the data
            ViewHolderWrapper.setFlags(holder, 0, FLAG_INVALID | FLAG_UPDATED);
        }
    }
    super.detachAndScrapAttachedViews(recycler);
}
Also used : RecyclerView(android.support.v7.widget.RecyclerView) View(android.view.View) RecyclerView(android.support.v7.widget.RecyclerView)

Example 24 with Recycler

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

the class VirtualLayoutManager method recycleChildren.

@Override
protected void recycleChildren(RecyclerView.Recycler recycler, int startIndex, int endIndex) {
    if (startIndex == endIndex) {
        return;
    }
    if (sDebuggable) {
        Log.d(TAG, "Recycling " + Math.abs(startIndex - endIndex) + " items");
    }
    if (endIndex > startIndex) {
        View endView = getChildAt(endIndex - 1);
        View startView = getChildAt(startIndex);
        int startPos = getPosition(startView);
        int endPos = getPosition(endView);
        int idx = startIndex;
        for (int i = startIndex; i < endIndex; i++) {
            View v = getChildAt(idx);
            int pos = getPosition(v);
            if (pos != RecyclerView.NO_POSITION) {
                LayoutHelper layoutHelper = mHelperFinder.getLayoutHelper(pos);
                if (layoutHelper == null || layoutHelper.isRecyclable(pos, startPos, endPos, this, true)) {
                    removeAndRecycleViewAt(idx, recycler);
                } else {
                    idx++;
                }
            } else
                removeAndRecycleViewAt(idx, recycler);
        }
    } else {
        View endView = getChildAt(startIndex);
        View startView = getChildAt(endIndex + 1);
        int startPos = getPosition(startView);
        int endPos = getPosition(endView);
        for (int i = startIndex; i > endIndex; i--) {
            View v = getChildAt(i);
            int pos = getPosition(v);
            if (pos != RecyclerView.NO_POSITION) {
                LayoutHelper layoutHelper = mHelperFinder.getLayoutHelper(pos);
                if (layoutHelper == null || layoutHelper.isRecyclable(pos, startPos, endPos, this, false)) {
                    removeAndRecycleViewAt(i, recycler);
                }
            } else
                removeAndRecycleViewAt(i, recycler);
        }
    }
}
Also used : DefaultLayoutHelper(com.alibaba.android.vlayout.layout.DefaultLayoutHelper) BaseLayoutHelper(com.alibaba.android.vlayout.layout.BaseLayoutHelper) FixAreaLayoutHelper(com.alibaba.android.vlayout.layout.FixAreaLayoutHelper) View(android.view.View) RecyclerView(android.support.v7.widget.RecyclerView)

Example 25 with Recycler

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

the class VirtualLayoutManager method onLayoutChildren.

@Override
public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
        Trace.beginSection(TRACE_LAYOUT);
    }
    if (mNoScrolling && state.didStructureChange()) {
        mSpaceMeasured = false;
        mSpaceMeasuring = true;
    }
    runPreLayout(recycler, state);
    try {
        super.onLayoutChildren(recycler, state);
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    } finally {
        // MaX_VALUE means invalidate scrolling offset - no scroll
        // hack to indicate its an initial layout
        runPostLayout(recycler, state, Integer.MAX_VALUE);
    }
    if ((mNestedScrolling || mNoScrolling) && mSpaceMeasuring) {
        // measure required, so do measure
        mSpaceMeasured = true;
        // get last child
        int childCount = getChildCount();
        View lastChild = getChildAt(childCount - 1);
        if (lastChild != null) {
            RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) lastChild.getLayoutParams();
            // found the end of last child view
            mMeasuredFullSpace = getDecoratedBottom(lastChild) + params.bottomMargin + computeAlignOffset(lastChild, true, false);
            if (mRecyclerView != null && mNestedScrolling) {
                ViewParent parent = mRecyclerView.getParent();
                if (parent instanceof View) {
                    // make sure the fullspace be the min value of measured space and parent's height
                    mMeasuredFullSpace = Math.min(mMeasuredFullSpace, ((View) parent).getMeasuredHeight());
                }
            }
        } else {
            mSpaceMeasuring = false;
        }
        mSpaceMeasuring = false;
        if (mRecyclerView != null && getItemCount() > 0) {
            // relayout
            mRecyclerView.post(new Runnable() {

                @Override
                public void run() {
                    // post relayout
                    if (mRecyclerView != null)
                        mRecyclerView.requestLayout();
                }
            });
        }
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
        Trace.endSection();
    }
}
Also used : ViewParent(android.view.ViewParent) RecyclerView(android.support.v7.widget.RecyclerView) View(android.view.View) RecyclerView(android.support.v7.widget.RecyclerView)

Aggregations

RecyclerView (android.support.v7.widget.RecyclerView)106 View (android.view.View)100 TextView (android.widget.TextView)19 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)15 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 VirtualLayoutManager (com.alibaba.android.vlayout.VirtualLayoutManager)7 SuppressLint (android.annotation.SuppressLint)6 Context (android.content.Context)6 Bundle (android.os.Bundle)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 ViewGroup (android.view.ViewGroup)5