@@ -583,6 +583,7 @@ private void measureHorizontal(int widthMeasureSpec, int heightMeasureSpec) {
583
583
continue ;
584
584
} else if (child .getVisibility () == View .GONE ) {
585
585
flexLine .mItemCount ++;
586
+ flexLine .mGoneItemCount ++;
586
587
addFlexLineIfLastFlexItem (i , childCount , flexLine );
587
588
continue ;
588
589
}
@@ -627,7 +628,7 @@ private void measureHorizontal(int widthMeasureSpec, int heightMeasureSpec) {
627
628
if (isWrapRequired (widthMode , widthSize , flexLine .mMainSize ,
628
629
child .getMeasuredWidth () + lp .leftMargin + lp .rightMargin , lp ,
629
630
i , indexInFlexLine )) {
630
- if (flexLine .mItemCount > 0 ) {
631
+ if (flexLine .getLayoutVisibleItemCount () > 0 ) {
631
632
addFlexLine (flexLine );
632
633
}
633
634
@@ -681,17 +682,18 @@ private void measureHorizontal(int widthMeasureSpec, int heightMeasureSpec) {
681
682
for (int i = viewIndex ; i < viewIndex + flexLine .mItemCount ; i ++) {
682
683
View child = getReorderedChildAt (i );
683
684
LayoutParams lp = (LayoutParams ) child .getLayoutParams ();
685
+
684
686
if (mFlexWrap != FLEX_WRAP_WRAP_REVERSE ) {
685
687
int marginTop = flexLine .mMaxBaseline - child .getBaseline ();
686
688
marginTop = Math .max (marginTop , lp .topMargin );
687
689
largestHeightInLine = Math .max (largestHeightInLine ,
688
- child .getHeight () + marginTop + lp .bottomMargin );
690
+ child .getMeasuredHeight () + marginTop + lp .bottomMargin );
689
691
} else {
690
692
int marginBottom = flexLine .mMaxBaseline - child .getMeasuredHeight () +
691
693
child .getBaseline ();
692
694
marginBottom = Math .max (marginBottom , lp .bottomMargin );
693
695
largestHeightInLine = Math .max (largestHeightInLine ,
694
- child .getHeight () + lp .topMargin + marginBottom );
696
+ child .getMeasuredHeight () + lp .topMargin + marginBottom );
695
697
}
696
698
}
697
699
flexLine .mCrossSize = largestHeightInLine ;
@@ -745,6 +747,7 @@ private void measureVertical(int widthMeasureSpec, int heightMeasureSpec) {
745
747
continue ;
746
748
} else if (child .getVisibility () == View .GONE ) {
747
749
flexLine .mItemCount ++;
750
+ flexLine .mGoneItemCount ++;
748
751
addFlexLineIfLastFlexItem (i , childCount , flexLine );
749
752
continue ;
750
753
}
@@ -790,7 +793,7 @@ private void measureVertical(int widthMeasureSpec, int heightMeasureSpec) {
790
793
if (isWrapRequired (heightMode , heightSize , flexLine .mMainSize ,
791
794
child .getMeasuredHeight () + lp .topMargin + lp .bottomMargin , lp ,
792
795
i , indexInFlexLine )) {
793
- if (flexLine .mItemCount > 0 ) {
796
+ if (flexLine .getLayoutVisibleItemCount () > 0 ) {
794
797
addFlexLine (flexLine );
795
798
}
796
799
@@ -862,7 +865,7 @@ private void checkSizeConstraints(View view) {
862
865
}
863
866
864
867
private void addFlexLineIfLastFlexItem (int childIndex , int childCount , FlexLine flexLine ) {
865
- if (childIndex == childCount - 1 && flexLine .mItemCount != 0 ) {
868
+ if (childIndex == childCount - 1 && flexLine .getLayoutVisibleItemCount () != 0 ) {
866
869
// Add the flex line if this item is the last item
867
870
addFlexLine (flexLine );
868
871
}
@@ -1661,20 +1664,25 @@ private void layoutHorizontal(boolean isRtl, int left, int top, int right, int b
1661
1664
childLeft = paddingLeft + (width - flexLine .mMainSize ) / 2f ;
1662
1665
childRight = width - paddingRight - (width - flexLine .mMainSize ) / 2f ;
1663
1666
break ;
1664
- case JUSTIFY_CONTENT_SPACE_AROUND :
1665
- if (flexLine .mItemCount != 0 ) {
1667
+ case JUSTIFY_CONTENT_SPACE_AROUND : {
1668
+ int visibleCount = flexLine .getLayoutVisibleItemCount ();
1669
+ if (visibleCount != 0 ) {
1666
1670
spaceBetweenItem = (width - flexLine .mMainSize )
1667
- / (float ) flexLine . mItemCount ;
1671
+ / (float ) visibleCount ;
1668
1672
}
1669
1673
childLeft = paddingLeft + spaceBetweenItem / 2f ;
1670
1674
childRight = width - paddingRight - spaceBetweenItem / 2f ;
1671
1675
break ;
1672
- case JUSTIFY_CONTENT_SPACE_BETWEEN :
1676
+ }
1677
+ case JUSTIFY_CONTENT_SPACE_BETWEEN : {
1678
+ int visibleCount = flexLine .getLayoutVisibleItemCount ();
1679
+ float denominator = visibleCount != 1 ? visibleCount - 1 : 1f ;
1680
+
1673
1681
childLeft = paddingLeft ;
1674
- float denominator = flexLine .mItemCount != 1 ? flexLine .mItemCount - 1 : 1f ;
1675
1682
spaceBetweenItem = (width - flexLine .mMainSize ) / denominator ;
1676
1683
childRight = width - paddingRight ;
1677
1684
break ;
1685
+ }
1678
1686
default :
1679
1687
throw new IllegalStateException (
1680
1688
"Invalid justifyContent is set: " + mJustifyContent );
@@ -1878,20 +1886,25 @@ private void layoutVertical(boolean isRtl, boolean fromBottomToTop, int left, in
1878
1886
childTop = paddingTop + (height - flexLine .mMainSize ) / 2f ;
1879
1887
childBottom = height - paddingBottom - (height - flexLine .mMainSize ) / 2f ;
1880
1888
break ;
1881
- case JUSTIFY_CONTENT_SPACE_AROUND :
1882
- if (flexLine .mItemCount != 0 ) {
1889
+ case JUSTIFY_CONTENT_SPACE_AROUND : {
1890
+ int visibleCount = flexLine .getLayoutVisibleItemCount ();
1891
+ if (visibleCount != 0 ) {
1883
1892
spaceBetweenItem = (height - flexLine .mMainSize )
1884
- / (float ) flexLine . mItemCount ;
1893
+ / (float ) visibleCount ;
1885
1894
}
1886
1895
childTop = paddingTop + spaceBetweenItem / 2f ;
1887
1896
childBottom = height - paddingBottom - spaceBetweenItem / 2f ;
1888
1897
break ;
1889
- case JUSTIFY_CONTENT_SPACE_BETWEEN :
1898
+ }
1899
+ case JUSTIFY_CONTENT_SPACE_BETWEEN : {
1900
+ int visibleCount = flexLine .getLayoutVisibleItemCount ();
1901
+ float denominator = visibleCount != 1 ? visibleCount - 1 : 1f ;
1902
+
1890
1903
childTop = paddingTop ;
1891
- float denominator = flexLine .mItemCount != 1 ? flexLine .mItemCount - 1 : 1f ;
1892
1904
spaceBetweenItem = (height - flexLine .mMainSize ) / denominator ;
1893
1905
childBottom = height - paddingBottom ;
1894
1906
break ;
1907
+ }
1895
1908
default :
1896
1909
throw new IllegalStateException (
1897
1910
"Invalid justifyContent is set: " + mJustifyContent );
@@ -2085,6 +2098,11 @@ private void drawDividersHorizontal(Canvas canvas, boolean isRtl, boolean fromBo
2085
2098
FlexLine flexLine = mFlexLines .get (i );
2086
2099
for (int j = 0 ; j < flexLine .mItemCount ; j ++) {
2087
2100
View view = getReorderedChildAt (currentViewIndex );
2101
+
2102
+ if (view == null || view .getVisibility () == View .GONE ) {
2103
+ continue ;
2104
+ }
2105
+
2088
2106
LayoutParams lp = (LayoutParams ) view .getLayoutParams ();
2089
2107
2090
2108
// Judge if the beginning or middle divider is needed
@@ -2165,6 +2183,11 @@ private void drawDividersVertical(Canvas canvas, boolean isRtl, boolean fromBott
2165
2183
// Draw horizontal dividers if needed
2166
2184
for (int j = 0 ; j < flexLine .mItemCount ; j ++) {
2167
2185
View view = getReorderedChildAt (currentViewIndex );
2186
+
2187
+ if (view == null || view .getVisibility () == View .GONE ) {
2188
+ continue ;
2189
+ }
2190
+
2168
2191
LayoutParams lp = (LayoutParams ) view .getLayoutParams ();
2169
2192
2170
2193
// Judge if the beginning or middle divider is needed
@@ -2332,11 +2355,20 @@ public void setAlignContent(@AlignContent int alignContent) {
2332
2355
}
2333
2356
2334
2357
/**
2335
- * @return the flex lines composing this flex container. This method returns an unmodifiable
2336
- * list. Thus any changes of the returned list are not supported.
2358
+ * @return the flex lines composing this flex container. This method returns a copy of the
2359
+ * original list excluding a dummy flex line (flex line that doesn't have any flex items in it
2360
+ * but used for the alignment along the cross axis).
2361
+ * Thus any changes of the returned list are not reflected to the original list.
2337
2362
*/
2338
2363
public List <FlexLine > getFlexLines () {
2339
- return Collections .unmodifiableList (mFlexLines );
2364
+ List <FlexLine > result = new ArrayList <>(mFlexLines .size ());
2365
+ for (FlexLine flexLine : mFlexLines ) {
2366
+ if (flexLine .getLayoutVisibleItemCount () == 0 ) {
2367
+ continue ;
2368
+ }
2369
+ result .add (flexLine );
2370
+ }
2371
+ return result ;
2340
2372
}
2341
2373
2342
2374
/**
@@ -2535,7 +2567,7 @@ private boolean hasDividerBeforeFlexLine(int flexLineIndex) {
2535
2567
2536
2568
private boolean allFlexLinesAreDummyBefore (int flexLineIndex ) {
2537
2569
for (int i = 0 ; i < flexLineIndex ; i ++) {
2538
- if (mFlexLines .get (i ).mItemCount > 0 ) {
2570
+ if (mFlexLines .get (i ).getLayoutVisibleItemCount () > 0 ) {
2539
2571
return false ;
2540
2572
}
2541
2573
}
@@ -2554,7 +2586,7 @@ private boolean hasEndDividerAfterFlexLine(int flexLineIndex) {
2554
2586
}
2555
2587
2556
2588
for (int i = flexLineIndex + 1 ; i < mFlexLines .size (); i ++) {
2557
- if (mFlexLines .get (i ).mItemCount > 0 ) {
2589
+ if (mFlexLines .get (i ).getLayoutVisibleItemCount () > 0 ) {
2558
2590
return false ;
2559
2591
}
2560
2592
}
0 commit comments