Flex grow sometimes causes elements with flexGrow 0 at the end of a flex line to shrink

This commit is contained in:
Panayot Cankov
2016-10-24 14:04:22 +03:00
parent 2c7efac368
commit dd345e65c1

View File

@@ -954,33 +954,22 @@ public class FlexboxLayout extends ViewGroup {
if (isMainAxisDirectionHorizontal(flexDirection)) { if (isMainAxisDirectionHorizontal(flexDirection)) {
// The direction of the main axis is horizontal // The direction of the main axis is horizontal
if (!mChildrenFrozen[childIndex]) { if (!mChildrenFrozen[childIndex]) {
float rawCalculatedWidth = child.getMeasuredWidth() + unitSpace * lp.flexGrow; float rawCalculatedWidth = child.getMeasuredWidth() + unitSpace * lp.flexGrow + accumulatedRoundError;
if (i == flexLine.mItemCount - 1) { int roundedCalculatedWidth = Math.round(rawCalculatedWidth);
rawCalculatedWidth += accumulatedRoundError; if (roundedCalculatedWidth > lp.maxWidth) {
accumulatedRoundError = 0;
}
int newWidth = Math.round(rawCalculatedWidth);
if (newWidth > lp.maxWidth) {
// This means the child can't expand beyond the value of the maxWidth attribute. // This means the child can't expand beyond the value of the maxWidth attribute.
// To adjust the flex line length to the size of maxMainSize, remaining // To adjust the flex line length to the size of maxMainSize, remaining
// positive free space needs to be re-distributed to other flex items // positive free space needs to be re-distributed to other flex items
// (children views). In that case, invoke this method again with the same // (children views). In that case, invoke this method again with the same
// startIndex. // startIndex.
needsReexpand = true; needsReexpand = true;
newWidth = lp.maxWidth; roundedCalculatedWidth = lp.maxWidth;
mChildrenFrozen[childIndex] = true; mChildrenFrozen[childIndex] = true;
flexLine.mTotalFlexGrow -= lp.flexGrow; flexLine.mTotalFlexGrow -= lp.flexGrow;
} else { } else {
accumulatedRoundError += (rawCalculatedWidth - newWidth); accumulatedRoundError = (rawCalculatedWidth - roundedCalculatedWidth);
if (accumulatedRoundError > 1.0) {
newWidth += 1;
accumulatedRoundError -= 1.0;
} else if (accumulatedRoundError < -1.0) {
newWidth -= 1;
accumulatedRoundError += 1.0;
}
} }
child.measure(MeasureSpec.makeMeasureSpec(newWidth, MeasureSpec.EXACTLY), child.measure(MeasureSpec.makeMeasureSpec(roundedCalculatedWidth, MeasureSpec.EXACTLY),
MeasureSpec MeasureSpec
.makeMeasureSpec(child.getMeasuredHeight(), .makeMeasureSpec(child.getMeasuredHeight(),
MeasureSpec.EXACTLY)); MeasureSpec.EXACTLY));
@@ -990,12 +979,8 @@ public class FlexboxLayout extends ViewGroup {
// The direction of the main axis is vertical // The direction of the main axis is vertical
if (!mChildrenFrozen[childIndex]) { if (!mChildrenFrozen[childIndex]) {
float rawCalculatedHeight = child.getMeasuredHeight() + unitSpace * lp.flexGrow; float rawCalculatedHeight = child.getMeasuredHeight() + unitSpace * lp.flexGrow;
if (i == flexLine.mItemCount - 1) { int roundedCalculatedHeight = Math.round(rawCalculatedHeight);
rawCalculatedHeight += accumulatedRoundError; if (roundedCalculatedHeight > lp.maxHeight) {
accumulatedRoundError = 0;
}
int newHeight = Math.round(rawCalculatedHeight);
if (newHeight > lp.maxHeight) {
// This means the child can't expand beyond the value of the maxHeight // This means the child can't expand beyond the value of the maxHeight
// attribute. // attribute.
// To adjust the flex line length to the size of maxMainSize, remaining // To adjust the flex line length to the size of maxMainSize, remaining
@@ -1003,22 +988,15 @@ public class FlexboxLayout extends ViewGroup {
// (children views). In that case, invoke this method again with the same // (children views). In that case, invoke this method again with the same
// startIndex. // startIndex.
needsReexpand = true; needsReexpand = true;
newHeight = lp.maxHeight; roundedCalculatedHeight = lp.maxHeight;
mChildrenFrozen[childIndex] = true; mChildrenFrozen[childIndex] = true;
flexLine.mTotalFlexGrow -= lp.flexGrow; flexLine.mTotalFlexGrow -= lp.flexGrow;
} else { } else {
accumulatedRoundError += (rawCalculatedHeight - newHeight); accumulatedRoundError = rawCalculatedHeight - roundedCalculatedHeight;
if (accumulatedRoundError > 1.0) {
newHeight += 1;
accumulatedRoundError -= 1.0;
} else if (accumulatedRoundError < -1.0) {
newHeight -= 1;
accumulatedRoundError += 1.0;
}
} }
child.measure(MeasureSpec.makeMeasureSpec(child.getMeasuredWidth(), child.measure(MeasureSpec.makeMeasureSpec(child.getMeasuredWidth(),
MeasureSpec.EXACTLY), MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(newHeight, MeasureSpec.EXACTLY)); MeasureSpec.makeMeasureSpec(roundedCalculatedHeight, MeasureSpec.EXACTLY));
} }
flexLine.mMainSize += child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin; flexLine.mMainSize += child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin;
} }