private int expandFlexItems(FlexLine flexLine,@FlexDirection int flexDirection,int maxMainSize,int paddingAlongMainAxis,int startIndex){
  int childIndex=startIndex;
  if (flexLine.mTotalFlexGrow <= 0 || maxMainSize < flexLine.mMainSize) {
    childIndex+=flexLine.mItemCount;
    return childIndex;
  }
  int sizeBeforeExpand=flexLine.mMainSize;
  boolean needsReexpand=false;
  float unitSpace=(maxMainSize - flexLine.mMainSize) / flexLine.mTotalFlexGrow;
  flexLine.mMainSize=paddingAlongMainAxis + flexLine.mDividerLengthInMainSize;
  float accumulatedRoundError=0;
  for (int i=0; i < flexLine.mItemCount; i++) {
    View child=getReorderedChildAt(childIndex);
    if (child == null) {
      continue;
    }
 else     if (child.getVisibility() == View.GONE) {
      childIndex++;
      continue;
    }
    LayoutParams lp=(LayoutParams)child.getLayoutParams();
    if (isMainAxisDirectionHorizontal(flexDirection)) {
      if (!mChildrenFrozen[childIndex]) {
        float rawCalculatedWidth=child.getMeasuredWidth() + unitSpace * lp.flexGrow;
        if (i == flexLine.mItemCount - 1) {
          rawCalculatedWidth+=accumulatedRoundError;
          accumulatedRoundError=0;
        }
        int newWidth=Math.round(rawCalculatedWidth);
        if (newWidth > lp.maxWidth) {
          needsReexpand=true;
          newWidth=lp.maxWidth;
          mChildrenFrozen[childIndex]=true;
          flexLine.mTotalFlexGrow-=lp.flexGrow;
        }
 else {
          accumulatedRoundError+=(rawCalculatedWidth - newWidth);
          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),MeasureSpec.makeMeasureSpec(child.getMeasuredHeight(),MeasureSpec.EXACTLY));
      }
      flexLine.mMainSize+=child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;
    }
 else {
      if (!mChildrenFrozen[childIndex]) {
        float rawCalculatedHeight=child.getMeasuredHeight() + unitSpace * lp.flexGrow;
        if (i == flexLine.mItemCount - 1) {
          rawCalculatedHeight+=accumulatedRoundError;
          accumulatedRoundError=0;
        }
        int newHeight=Math.round(rawCalculatedHeight);
        if (newHeight > lp.maxHeight) {
          needsReexpand=true;
          newHeight=lp.maxHeight;
          mChildrenFrozen[childIndex]=true;
          flexLine.mTotalFlexGrow-=lp.flexGrow;
        }
 else {
          accumulatedRoundError+=(rawCalculatedHeight - newHeight);
          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(),MeasureSpec.EXACTLY),MeasureSpec.makeMeasureSpec(newHeight,MeasureSpec.EXACTLY));
      }
      flexLine.mMainSize+=child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin;
    }
    childIndex++;
  }
  if (needsReexpand && sizeBeforeExpand != flexLine.mMainSize) {
    expandFlexItems(flexLine,flexDirection,maxMainSize,paddingAlongMainAxis,startIndex);
  }
  return childIndex;
}

private int expandFlexItems ( FlexLine flexLine , @ FlexDirection int flexDirection , int maxMainSize , int paddingAlongMainAxis , int startIndex ) { int childIndex = startIndex ; if ( flexLine . mTotalFlexGrow <= 0 || maxMainSize < flexLine . mMainSize ) { childIndex += flexLine . mItemCount ; return childIndex ; } int sizeBeforeExpand = flexLine . mMainSize ; boolean needsReexpand = false ; float unitSpace = ( maxMainSize - flexLine . mMainSize ) / flexLine . mTotalFlexGrow ; flexLine . mMainSize = paddingAlongMainAxis + flexLine . mDividerLengthInMainSize ; float accumulatedRoundError = 0 ; for ( int i = 0 ; i < flexLine . mItemCount ; i ++ ) { View child = getReorderedChildAt ( childIndex ) ; if ( child == null ) { continue ; } else if ( child . getVisibility ( ) == View . GONE ) { childIndex ++ ; continue ; } LayoutParams lp = ( LayoutParams ) child . getLayoutParams ( ) ; if ( isMainAxisDirectionHorizontal ( flexDirection ) ) { if ( ! mChildrenFrozen [ childIndex ] ) { float rawCalculatedWidth = child . getMeasuredWidth ( ) + unitSpace * lp . flexGrow ; if ( i == flexLine . mItemCount - 1 ) { rawCalculatedWidth += accumulatedRoundError ; accumulatedRoundError = 0 ; } int newWidth = Math . round ( rawCalculatedWidth ) ; if ( newWidth > lp . maxWidth ) { needsReexpand = true ; newWidth = lp . maxWidth ; mChildrenFrozen [ childIndex ] = true ; flexLine . mTotalFlexGrow -= lp . flexGrow ; } else { accumulatedRoundError += ( rawCalculatedWidth - newWidth ) ; 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 ) , MeasureSpec . makeMeasureSpec ( child . getMeasuredHeight ( ) , MeasureSpec . EXACTLY ) ) ; } flexLine . mMainSize += child . getMeasuredWidth ( ) + lp . leftMargin + lp . rightMargin ; } else { if ( ! mChildrenFrozen [ childIndex ] ) { float rawCalculatedHeight = child . getMeasuredHeight ( ) + unitSpace * lp . flexGrow ; if ( i == flexLine . mItemCount - 1 ) { rawCalculatedHeight += accumulatedRoundError ; accumulatedRoundError = 0 ; } int newHeight = Math . round ( rawCalculatedHeight ) ; if ( newHeight > lp . maxHeight ) { needsReexpand = true ; newHeight = lp . maxHeight ; mChildrenFrozen [ childIndex ] = true ; flexLine . mTotalFlexGrow -= lp . flexGrow ; } else { accumulatedRoundError += ( rawCalculatedHeight - newHeight ) ; 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 ( ) , MeasureSpec . EXACTLY ) , MeasureSpec . makeMeasureSpec ( newHeight , MeasureSpec . EXACTLY ) ) ; } flexLine . mMainSize += child . getMeasuredHeight ( ) + lp . topMargin + lp . bottomMargin ; } childIndex ++ ; } if ( needsReexpand && sizeBeforeExpand != flexLine . mMainSize ) { expandFlexItems ( flexLine , flexDirection , maxMainSize , paddingAlongMainAxis , startIndex ) ; } return childIndex ; }
expand the flex items along the main axis based on the individual flexgrow attribute .
