2

我正在滚动视图中创建一个流布局。现在我无法理解的问题是,如果 flowlayout 的高度小于滚动视图(即 match_parent),我希望我的 flowlayout 视图完全匹配父级,否则高度应该是包装内容。

这是我的 onMeasure 方法。

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

    int widthLimit = MeasureSpec.getSize(widthMeasureSpec) - getPaddingRight() - mHorizontalEndSpacingView;
    int widthMode = MeasureSpec.getMode(widthMeasureSpec);

    int heightLimit = MeasureSpec.getSize(heightMeasureSpec);
    int heightMode = MeasureSpec.getMode(heightMeasureSpec);

    boolean growHeight = widthMode != MeasureSpec.UNSPECIFIED;

    int width = 0;

    int currentWidth = getPaddingLeft() + mHorizontalStartSpacingView;
    int currentHeight = getPaddingTop() + mVerticalStartSpacingView;

    int maxChildHeight = 0;

    boolean breakLine = false;
    boolean newLine = false;
    int spacing = 0;

    final int count = getChildCount();
    for (int i = 0; i < count; i++) {
        View child = getChildAt(i);
        measureChild(child, MeasureSpec.makeMeasureSpec(widthLimit - getPaddingLeft() - mHorizontalStartSpacingView, MeasureSpec.AT_MOST), heightMeasureSpec);

        LayoutParams lp = (LayoutParams) child.getLayoutParams();
        spacing = mHorizontalCenterSpacingView;

        if (lp.horizontalSpacing > 0) {
            spacing = lp.horizontalSpacing;
        }

        if (growHeight && (breakLine || ((currentWidth + child.getMeasuredWidth()) > widthLimit))) {
            newLine = true;
            currentHeight += maxChildHeight + mVerticalCenterSpacingView;

            width = Math.max(width, currentWidth - spacing);

            currentWidth = getPaddingLeft() + mHorizontalStartSpacingView;
            maxChildHeight = 0;
        } else {
            newLine = false;
        }

        maxChildHeight = Math.max(maxChildHeight, child.getMeasuredHeight());

        lp.x = currentWidth;
        lp.y = currentHeight;

        currentWidth += child.getMeasuredWidth() + spacing;

        breakLine = lp.breakLine;
    }

    if (newLine == false) {
        width = Math.max(width, currentWidth - spacing);
    }

    width += getPaddingRight();
    int height = currentHeight + maxChildHeight + getPaddingBottom() + mVerticalEndStartSpacingView;

    setMeasuredDimension(resolveSize(width, widthMeasureSpec),
            resolveSize(height, heightMeasureSpec));
}
4

1 回答 1

3

解决它。下面是新的onMeasure

 @Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

    int widthLimit = MeasureSpec.getSize(widthMeasureSpec) - getPaddingRight() - mHorizontalEndSpacingView;
    int widthMode = MeasureSpec.getMode(widthMeasureSpec);

    int heightLimit = MeasureSpec.getSize(heightMeasureSpec);
    int heightMode = MeasureSpec.getMode(heightMeasureSpec);

    boolean growHeight = widthMode != MeasureSpec.UNSPECIFIED;

    int width = 0;

    int currentWidth = getPaddingLeft() + mHorizontalStartSpacingView;
    int currentHeight = getPaddingTop() + mVerticalStartSpacingView;

    int maxChildHeight = 0;

    boolean breakLine = false;
    boolean newLine = false;
    int spacing = 0;

    final int count = getChildCount();
    for (int i = 0; i < count; i++) {
        View child = getChildAt(i);
        measureChild(child, MeasureSpec.makeMeasureSpec(widthLimit - getPaddingLeft() - mHorizontalStartSpacingView, MeasureSpec.AT_MOST), heightMeasureSpec);

        LayoutParams lp = (LayoutParams) child.getLayoutParams();
        spacing = mHorizontalCenterSpacingView;

        if (lp.horizontalSpacing > 0) {
            spacing = lp.horizontalSpacing;
        }

        if (growHeight && (breakLine || ((currentWidth + child.getMeasuredWidth()) > widthLimit))) {
            newLine = true;
            currentHeight += maxChildHeight + mVerticalCenterSpacingView;

            width = Math.max(width, currentWidth - spacing);

            currentWidth = getPaddingLeft() + mHorizontalStartSpacingView;

            maxChildHeight = 0;
        } else {
            newLine = false;
        }

        maxChildHeight = Math.max(maxChildHeight, child.getMeasuredHeight());

        lp.x = currentWidth;
        lp.y = currentHeight;

        currentWidth += child.getMeasuredWidth() + spacing;

        breakLine = lp.breakLine;
    }

    if (newLine == false) {
        width = Math.max(width, currentWidth - spacing);
    }

    width += getPaddingRight();
    int height = currentHeight + maxChildHeight + getPaddingBottom() + mVerticalEndStartSpacingView;

    // The Fix.
    int parentViewHeight = ((ScrollView) getParent()).getHeight();
    if(parentViewHeight > height) {
        height = parentViewHeight;
    }
    // End

    int finalWidth = resolveSize(width, widthMeasureSpec);
    int finalHeight = resolveSize(height, heightMeasureSpec);
    setMeasuredDimension(finalWidth, finalHeight);
}
于 2015-09-03T09:02:24.000 回答