@@ -223,7 +223,6 @@ public FlexboxLayoutManager(Context context, @FlexDirection int flexDirection,
223223 setFlexDirection (flexDirection );
224224 setFlexWrap (flexWrap );
225225 setAlignItems (AlignItems .STRETCH );
226- setAutoMeasureEnabled (true );
227226 mContext = context ;
228227 }
229228
@@ -262,10 +261,14 @@ public FlexboxLayoutManager(Context context, AttributeSet attrs, int defStyleAtt
262261 }
263262 setFlexWrap (FlexWrap .WRAP );
264263 setAlignItems (AlignItems .STRETCH );
265- setAutoMeasureEnabled (true );
266264 mContext = context ;
267265 }
268266
267+ @ Override
268+ public boolean isAutoMeasureEnabled () {
269+ return true ;
270+ }
271+
269272 // From here, methods from FlexContainer
270273 @ FlexDirection
271274 @ Override
@@ -371,6 +374,7 @@ public void setMaxLine(int maxLine) {
371374 }
372375
373376 @ Override
377+ @ NonNull
374378 public List <FlexLine > getFlexLines () {
375379 List <FlexLine > result = new ArrayList <>(mFlexLines .size ());
376380 for (int i = 0 , size = mFlexLines .size (); i < size ; i ++) {
@@ -537,7 +541,11 @@ public PointF computeScrollVectorForPosition(int targetPosition) {
537541 if (getChildCount () == 0 ) {
538542 return null ;
539543 }
540- int firstChildPos = getPosition (getChildAt (0 ));
544+ View view = getChildAt (0 );
545+ if (view == null ) {
546+ return null ;
547+ }
548+ int firstChildPos = getPosition (view );
541549 int direction = targetPosition < firstChildPos ? -1 : 1 ;
542550 if (isMainAxisDirectionHorizontal ()) {
543551 return new PointF (0 , direction );
@@ -738,8 +746,8 @@ public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State
738746
739747 int startOffset ;
740748 int endOffset ;
749+ int filledToEnd = fill (recycler , state , mLayoutState );
741750 if (mAnchorInfo .mLayoutFromEnd ) {
742- int filledToEnd = fill (recycler , state , mLayoutState );
743751 if (DEBUG ) {
744752 Log .d (TAG , String .format ("filled: %d toward start" , filledToEnd ));
745753 }
@@ -751,7 +759,6 @@ public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State
751759 }
752760 endOffset = mLayoutState .mOffset ;
753761 } else {
754- int filledToEnd = fill (recycler , state , mLayoutState );
755762 if (DEBUG ) {
756763 Log .d (TAG , String .format ("filled: %d toward end" , filledToEnd ));
757764 }
@@ -1112,8 +1119,11 @@ private boolean updateAnchorFromPendingState(RecyclerView.State state, AnchorInf
11121119 : mOrientationHelper .getDecoratedStart (anchorView );
11131120 } else {
11141121 if (getChildCount () > 0 ) {
1115- int position = getPosition (getChildAt (0 ));
1116- anchorInfo .mLayoutFromEnd = mPendingScrollPosition < position ;
1122+ View view = getChildAt (0 );
1123+ if (view != null ) {
1124+ int position = getPosition (view );
1125+ anchorInfo .mLayoutFromEnd = mPendingScrollPosition < position ;
1126+ }
11171127 }
11181128 anchorInfo .assignCoordinateFromPadding ();
11191129 }
@@ -1229,6 +1239,9 @@ private View findReferenceChild(int start, int end, int itemCount) {
12291239 int diff = end > start ? 1 : -1 ;
12301240 for (int i = start ; i != end ; i += diff ) {
12311241 View view = getChildAt (i );
1242+ if (view == null ) {
1243+ continue ;
1244+ }
12321245 int position = getPosition (view );
12331246 if (position >= 0 && position < itemCount ) {
12341247 if (((RecyclerView .LayoutParams ) view .getLayoutParams ()).isItemRemoved ()) {
@@ -1327,7 +1340,9 @@ private void recycleFlexLinesFromStart(RecyclerView.Recycler recycler,
13271340 return ;
13281341 }
13291342 View firstView = getChildAt (0 );
1330-
1343+ if (firstView == null ) {
1344+ return ;
1345+ }
13311346 int currentLineIndex = mFlexboxHelper .mIndexToFlexLine [getPosition (firstView )];
13321347 if (currentLineIndex == NO_POSITION ) {
13331348 return ;
@@ -1336,6 +1351,9 @@ private void recycleFlexLinesFromStart(RecyclerView.Recycler recycler,
13361351 int recycleTo = -1 ;
13371352 for (int i = 0 ; i < childCount ; i ++) {
13381353 View view = getChildAt (i );
1354+ if (view == null ) {
1355+ continue ;
1356+ }
13391357 if (canViewBeRecycledFromStart (view , layoutState .mScrollingOffset )) {
13401358 if (flexLine .mLastIndex == getPosition (view )) {
13411359 // Recycle the views in a flex line if all views end positions are lower than
@@ -1371,13 +1389,15 @@ private void recycleFlexLinesFromEnd(RecyclerView.Recycler recycler, LayoutState
13711389 return ;
13721390 }
13731391 assert mFlexboxHelper .mIndexToFlexLine != null ;
1374- int limit = mOrientationHelper .getEnd () - layoutState .mScrollingOffset ;
13751392 int childCount = getChildCount ();
13761393 if (childCount == 0 ) {
13771394 return ;
13781395 }
13791396
13801397 View lastView = getChildAt (childCount - 1 );
1398+ if (lastView == null ) {
1399+ return ;
1400+ }
13811401 int currentLineIndex = mFlexboxHelper .mIndexToFlexLine [getPosition (lastView )];
13821402 if (currentLineIndex == NO_POSITION ) {
13831403 return ;
@@ -1387,6 +1407,9 @@ private void recycleFlexLinesFromEnd(RecyclerView.Recycler recycler, LayoutState
13871407 FlexLine flexLine = mFlexLines .get (currentLineIndex );
13881408 for (int i = childCount - 1 ; i >= 0 ; i --) {
13891409 View view = getChildAt (i );
1410+ if (view == null ) {
1411+ continue ;
1412+ }
13901413 if (canViewBeRecycledFromEnd (view , layoutState .mScrollingOffset )) {
13911414 if (flexLine .mFirstIndex == getPosition (view )) {
13921415 // Recycle the views in a flex line if all views start positions are beyond the
@@ -1914,8 +1937,7 @@ public boolean canScrollVertically() {
19141937 @ Override
19151938 public int scrollHorizontallyBy (int dx , RecyclerView .Recycler recycler ,
19161939 RecyclerView .State state ) {
1917- if (!isMainAxisDirectionHorizontal () ||
1918- (mFlexWrap == FlexWrap .NOWRAP && isMainAxisDirectionHorizontal ())) {
1940+ if (!isMainAxisDirectionHorizontal () || (mFlexWrap == FlexWrap .NOWRAP )) {
19191941 int scrolled = handleScrollingMainOrientation (dx , recycler , state );
19201942 mViewCache .clear ();
19211943 return scrolled ;
@@ -2046,6 +2068,9 @@ private void updateLayoutState(int layoutDirection, int absDelta) {
20462068 boolean columnAndRtl = !mainAxisHorizontal && mIsRtl ;
20472069 if (layoutDirection == LayoutState .LAYOUT_END ) {
20482070 View lastVisible = getChildAt (getChildCount () - 1 );
2071+ if (lastVisible == null ) {
2072+ return ;
2073+ }
20492074 mLayoutState .mOffset = mOrientationHelper .getDecoratedEnd (lastVisible );
20502075 int lastVisiblePosition = getPosition (lastVisible );
20512076 int lastVisibleLinePosition = mFlexboxHelper .mIndexToFlexLine [lastVisiblePosition ];
@@ -2067,8 +2092,7 @@ private void updateLayoutState(int layoutDirection, int absDelta) {
20672092 mLayoutState .mOffset = mOrientationHelper .getDecoratedStart (referenceView );
20682093 mLayoutState .mScrollingOffset = -mOrientationHelper .getDecoratedStart (referenceView )
20692094 + mOrientationHelper .getStartAfterPadding ();
2070- mLayoutState .mScrollingOffset = mLayoutState .mScrollingOffset >= 0 ?
2071- mLayoutState .mScrollingOffset : 0 ;
2095+ mLayoutState .mScrollingOffset = Math .max (mLayoutState .mScrollingOffset , 0 );
20722096 } else {
20732097 mLayoutState .mOffset = mOrientationHelper .getDecoratedEnd (referenceView );
20742098 mLayoutState .mScrollingOffset = mOrientationHelper .getDecoratedEnd (referenceView )
@@ -2100,7 +2124,9 @@ private void updateLayoutState(int layoutDirection, int absDelta) {
21002124 }
21012125 } else {
21022126 View firstVisible = getChildAt (0 );
2103-
2127+ if (firstVisible == null ) {
2128+ return ;
2129+ }
21042130 mLayoutState .mOffset = mOrientationHelper .getDecoratedStart (firstVisible );
21052131 int firstVisiblePosition = getPosition (firstVisible );
21062132 int firstVisibleLinePosition = mFlexboxHelper .mIndexToFlexLine [firstVisiblePosition ];
@@ -2129,8 +2155,7 @@ private void updateLayoutState(int layoutDirection, int absDelta) {
21292155 mLayoutState .mOffset = mOrientationHelper .getDecoratedEnd (referenceView );
21302156 mLayoutState .mScrollingOffset = mOrientationHelper .getDecoratedEnd (referenceView )
21312157 - mOrientationHelper .getEndAfterPadding ();
2132- mLayoutState .mScrollingOffset = mLayoutState .mScrollingOffset >= 0 ?
2133- mLayoutState .mScrollingOffset : 0 ;
2158+ mLayoutState .mScrollingOffset = Math .max (mLayoutState .mScrollingOffset , 0 );
21342159 } else {
21352160 mLayoutState .mOffset = mOrientationHelper .getDecoratedStart (referenceView );
21362161 mLayoutState .mScrollingOffset = -mOrientationHelper .getDecoratedStart (referenceView )
@@ -2207,7 +2232,7 @@ private View findLastReferenceViewInLine(View lastView, FlexLine lastVisibleLine
22072232 }
22082233
22092234 @ Override
2210- public int computeHorizontalScrollExtent (RecyclerView .State state ) {
2235+ public int computeHorizontalScrollExtent (@ NonNull RecyclerView .State state ) {
22112236 int scrollExtent = computeScrollExtent (state );
22122237 if (DEBUG ) {
22132238 Log .d (TAG , "computeHorizontalScrollExtent: " + scrollExtent );
@@ -2216,7 +2241,7 @@ public int computeHorizontalScrollExtent(RecyclerView.State state) {
22162241 }
22172242
22182243 @ Override
2219- public int computeVerticalScrollExtent (RecyclerView .State state ) {
2244+ public int computeVerticalScrollExtent (@ NonNull RecyclerView .State state ) {
22202245 int scrollExtent = computeScrollExtent (state );
22212246 if (DEBUG ) {
22222247 Log .d (TAG , "computeVerticalScrollExtent: " + scrollExtent );
@@ -2242,7 +2267,7 @@ private int computeScrollExtent(RecyclerView.State state) {
22422267 }
22432268
22442269 @ Override
2245- public int computeHorizontalScrollOffset (RecyclerView .State state ) {
2270+ public int computeHorizontalScrollOffset (@ NonNull RecyclerView .State state ) {
22462271 int scrollOffset = computeScrollOffset (state );
22472272 if (DEBUG ) {
22482273 Log .d (TAG , "computeHorizontalScrollOffset: " + scrollOffset );
@@ -2251,7 +2276,7 @@ public int computeHorizontalScrollOffset(RecyclerView.State state) {
22512276 }
22522277
22532278 @ Override
2254- public int computeVerticalScrollOffset (RecyclerView .State state ) {
2279+ public int computeVerticalScrollOffset (@ NonNull RecyclerView .State state ) {
22552280 int scrollOffset = computeScrollOffset (state );
22562281 if (DEBUG ) {
22572282 Log .d (TAG , "computeVerticalScrollOffset: " + scrollOffset );
@@ -2288,7 +2313,7 @@ private int computeScrollOffset(RecyclerView.State state) {
22882313 }
22892314
22902315 @ Override
2291- public int computeHorizontalScrollRange (RecyclerView .State state ) {
2316+ public int computeHorizontalScrollRange (@ NonNull RecyclerView .State state ) {
22922317 int scrollRange = computeScrollRange (state );
22932318 if (DEBUG ) {
22942319 Log .d (TAG , "computeHorizontalScrollRange: " + scrollRange );
@@ -2297,7 +2322,7 @@ public int computeHorizontalScrollRange(RecyclerView.State state) {
22972322 }
22982323
22992324 @ Override
2300- public int computeVerticalScrollRange (RecyclerView .State state ) {
2325+ public int computeVerticalScrollRange (@ NonNull RecyclerView .State state ) {
23012326 int scrollRange = computeScrollRange (state );
23022327 if (DEBUG ) {
23032328 Log .d (TAG , "computeVerticalScrollRange: " + scrollRange );
@@ -2929,6 +2954,7 @@ private void assignFromView(View anchor) {
29292954 }
29302955
29312956 @ Override
2957+ @ NonNull
29322958 public String toString () {
29332959 return "AnchorInfo{" +
29342960 "mPosition=" + mPosition +
@@ -3001,6 +3027,7 @@ private boolean hasMore(RecyclerView.State state, List<FlexLine> flexLines) {
30013027 }
30023028
30033029 @ Override
3030+ @ NonNull
30043031 public String toString () {
30053032 return "LayoutState{" +
30063033 "mAvailable=" + mAvailable +
@@ -3074,6 +3101,7 @@ public SavedState[] newArray(int size) {
30743101 };
30753102
30763103 @ Override
3104+ @ NonNull
30773105 public String toString () {
30783106 return "SavedState{" +
30793107 "mAnchorPosition=" + mAnchorPosition +
0 commit comments