Merge pull request #68 from NativeScript/fix-flex-shrink-zero

Setting flex-shrink to zero on the last items sometimes was still shrinking it with 1 pixel
This commit is contained in:
Panayot Cankov
2016-11-07 15:56:13 +02:00
committed by GitHub

View File

@@ -1051,34 +1051,17 @@ public class FlexboxLayout extends ViewGroup {
if (isMainAxisDirectionHorizontal(flexDirection)) { if (isMainAxisDirectionHorizontal(flexDirection)) {
// The direction of main axis is horizontal // The direction of main axis is horizontal
if (!mChildrenFrozen[childIndex]) { if (!mChildrenFrozen[childIndex]) {
float rawCalculatedWidth = child.getMeasuredWidth() float rawCalculatedWidth = child.getMeasuredWidth() - unitShrink * lp.flexShrink + accumulatedRoundError;
- unitShrink * lp.flexShrink; int roundedCalculatedWidth = Math.round(rawCalculatedWidth);
if (i == flexLine.mItemCount - 1) { if (roundedCalculatedWidth < lp.minWidth) {
rawCalculatedWidth += accumulatedRoundError;
accumulatedRoundError = 0;
}
int newWidth = Math.round(rawCalculatedWidth);
if (newWidth < lp.minWidth) {
// This means the child doesn't have enough space to distribute the negative
// free space. To adjust the flex line length down to the maxMainSize, remaining
// negative free space needs to be re-distributed to other flex items
// (children views). In that case, invoke this method again with the same
// startIndex.
needsReshrink = true; needsReshrink = true;
newWidth = lp.minWidth; roundedCalculatedWidth = lp.minWidth;
mChildrenFrozen[childIndex] = true; mChildrenFrozen[childIndex] = true;
flexLine.mTotalFlexShrink -= lp.flexShrink; flexLine.mTotalFlexShrink -= lp.flexShrink;
} else { } else {
accumulatedRoundError += (rawCalculatedWidth - newWidth); accumulatedRoundError = rawCalculatedWidth - roundedCalculatedWidth;
if (accumulatedRoundError > 1.0) {
newWidth += 1;
accumulatedRoundError -= 1;
} else if (accumulatedRoundError < -1.0) {
newWidth -= 1;
accumulatedRoundError += 1;
}
} }
child.measure(MeasureSpec.makeMeasureSpec(newWidth, MeasureSpec.EXACTLY), child.measure(MeasureSpec.makeMeasureSpec(roundedCalculatedWidth, MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(child.getMeasuredHeight(), MeasureSpec.makeMeasureSpec(child.getMeasuredHeight(),
MeasureSpec.EXACTLY)); MeasureSpec.EXACTLY));
} }
@@ -1086,32 +1069,19 @@ public class FlexboxLayout extends ViewGroup {
} else { } else {
// The direction of main axis is vertical // The direction of main axis is vertical
if (!mChildrenFrozen[childIndex]) { if (!mChildrenFrozen[childIndex]) {
float rawCalculatedHeight = child.getMeasuredHeight() float rawCalculatedHeight = child.getMeasuredHeight() - unitShrink * lp.flexShrink + accumulatedRoundError;
- unitShrink * lp.flexShrink; int roundedCalculatedHeight = Math.round(rawCalculatedHeight);
if (i == flexLine.mItemCount - 1) { if (roundedCalculatedHeight < lp.minHeight) {
rawCalculatedHeight += accumulatedRoundError;
accumulatedRoundError = 0;
}
int newHeight = Math.round(rawCalculatedHeight);
if (newHeight < lp.minHeight) {
// Need to invoke this method again like the case flex direction is vertical
needsReshrink = true; needsReshrink = true;
newHeight = lp.minHeight; roundedCalculatedHeight = lp.minHeight;
mChildrenFrozen[childIndex] = true; mChildrenFrozen[childIndex] = true;
flexLine.mTotalFlexShrink -= lp.flexShrink; flexLine.mTotalFlexShrink -= lp.flexShrink;
} else { } else {
accumulatedRoundError += (rawCalculatedHeight - newHeight); accumulatedRoundError = rawCalculatedHeight - roundedCalculatedHeight;
if (accumulatedRoundError > 1.0) {
newHeight += 1;
accumulatedRoundError -= 1;
} else if (accumulatedRoundError < -1.0) {
newHeight -= 1;
accumulatedRoundError += 1;
}
} }
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;
} }