flexbox-layout icon indicating copy to clipboard operation
flexbox-layout copied to clipboard

setFlexBasisPercent does not take a percentage

Open zestyping opened this issue 4 years ago • 3 comments

The method FlexboxLayout.LayoutParams.setFlexBasisPercent is implemented incorrectly. It multiplies its argument by 100, and it should not.

If it is named set...Percent, it must take a percentage.

If it does not take a percentage, naming it setFlexBasisPercent is misleading.

zestyping avatar Jul 31 '19 18:07 zestyping

I don't see where it multiplies its argument by 100. Can you provide more information?

https://github.com/google/flexbox-layout/blob/1.1.1/flexbox/src/main/java/com/google/android/flexbox/FlexboxLayout.java#L1793-L1796

MGaetan89 avatar Sep 25 '19 06:09 MGaetan89

You're right—it doesn't directly multiply the value by 100, but it does use the value in such a way that the result is too large by a factor of 100.

Here's an example:

https://github.com/google/flexbox-layout/blob/9db6bb19de8b75b2b61787142415e40766697006/flexbox/src/main/java/com/google/android/flexbox/FlexboxHelper.java#L450

This expression:

childMainSize = Math.round(mainSize * flexItem.getFlexBasisPercent());

treats the result of getFlexBasisPercent() as a multiplier, not as a percentage. Setting the flex basis to 50% should make the child half the space If you set the percentage to 50, instead of representing a ratio of one-half, mainSize will be multiplied by 50.

This is really a naming issue—this field is being used as a fraction rather than a percentage.

zestyping avatar Sep 25 '19 17:09 zestyping

Agreed, this is confusing. Perhaps setFlexBasisRatio would have been a better name.

golddove avatar Nov 25 '20 20:11 golddove