首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >滚动视图中的FlowLayout

滚动视图中的FlowLayout
EN

Stack Overflow用户
提问于 2015-09-03 13:52:23
回答 1查看 782关注 0票数 2

我正在滚动视图中创建一个流布局。现在,我不能解决的问题是,如果流布局的高度小于滚动视图(即match_parent),那么我希望我的流布局视图是完全换行的,否则高度应该是match_parent。

这是我的onMeasure方法。

代码语言:javascript
复制
@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));
}
EN

回答 1

Stack Overflow用户

发布于 2015-09-03 17:02:24

修好了。下面是新的onMeasure

代码语言:javascript
复制
 @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);
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32367888

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档