2

I am creating a flow layout inside the scrollview. Now the issue which I am unable to get my head around is that I want my flowlayout view to be exactly match_parent if the height of the flowlayout is less then the scrollview (which is match_parent) otherwise the height should be wrap content.

Here is my onMeasure method.

@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));
}
Farooq Arshed
  • 1,984
  • 2
  • 17
  • 26

1 Answers1

3

Fixed it. Below is the new 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);
}
Farooq Arshed
  • 1,984
  • 2
  • 17
  • 26