@@ -105,6 +105,7 @@ public class CollapsingToolbarLayout extends FrameLayout {
105
105
private Toolbar mToolbar ;
106
106
private View mToolbarDirectChild ;
107
107
private View mDummyView ;
108
+ private int mToolbarDrawIndex ;
108
109
109
110
private int mExpandedMarginStart ;
110
111
private int mExpandedMarginTop ;
@@ -303,15 +304,16 @@ public void draw(Canvas canvas) {
303
304
protected boolean drawChild (Canvas canvas , View child , long drawingTime ) {
304
305
// This is a little weird. Our scrim needs to be behind the Toolbar (if it is present),
305
306
// but in front of any other children which are behind it. To do this we intercept the
306
- // drawChild() call, and draw our scrim first when drawing the toolbar
307
- ensureToolbar ();
308
- if (child == mToolbar && mContentScrim != null && mScrimAlpha > 0 ) {
307
+ // drawChild() call, and draw our scrim after the preceding view is drawn
308
+ boolean invalidate = super .drawChild (canvas , child , drawingTime );
309
+
310
+ if (mContentScrim != null && mScrimAlpha > 0 && isToolbarChildDrawnNext (child )) {
309
311
mContentScrim .mutate ().setAlpha (mScrimAlpha );
310
312
mContentScrim .draw (canvas );
313
+ invalidate = true ;
311
314
}
312
315
313
- // Carry on drawing the child...
314
- return super .drawChild (canvas , child , drawingTime );
316
+ return invalidate ;
315
317
}
316
318
317
319
@ Override
@@ -357,6 +359,10 @@ private void ensureToolbar() {
357
359
mRefreshToolbar = false ;
358
360
}
359
361
362
+ private boolean isToolbarChildDrawnNext (View child ) {
363
+ return mToolbarDrawIndex >= 0 && mToolbarDrawIndex == indexOfChild (child ) + 1 ;
364
+ }
365
+
360
366
/**
361
367
* Returns the direct child of this layout, which itself is the ancestor of the
362
368
* given view.
@@ -452,6 +458,7 @@ protected void onLayout(boolean changed, int left, int top, int right, int botto
452
458
getViewOffsetHelper (child ).onViewLayout ();
453
459
}
454
460
461
+ ensureToolbar ();
455
462
// Finally, set our minimum height to enable proper AppBarLayout collapsing
456
463
if (mToolbar != null ) {
457
464
if (mCollapsingTitleEnabled && TextUtils .isEmpty (mCollapsingTextHelper .getText ())) {
@@ -460,10 +467,16 @@ protected void onLayout(boolean changed, int left, int top, int right, int botto
460
467
}
461
468
if (mToolbarDirectChild == null || mToolbarDirectChild == this ) {
462
469
setMinimumHeight (getHeightWithMargins (mToolbar ));
470
+ mToolbarDrawIndex = indexOfChild (mToolbar );
463
471
} else {
464
472
setMinimumHeight (getHeightWithMargins (mToolbarDirectChild ));
473
+ mToolbarDrawIndex = indexOfChild (mToolbarDirectChild );
465
474
}
475
+ } else {
476
+ mToolbarDrawIndex = -1 ;
466
477
}
478
+
479
+ updateScrimVisibility ();
467
480
}
468
481
469
482
private static int getHeightWithMargins (@ NonNull final View view ) {
@@ -1198,7 +1211,11 @@ final void updateScrimVisibility() {
1198
1211
final int getMaxOffsetForPinChild (View child ) {
1199
1212
final ViewOffsetHelper offsetHelper = getViewOffsetHelper (child );
1200
1213
final LayoutParams lp = (LayoutParams ) child .getLayoutParams ();
1201
- return getHeight () - (offsetHelper .getLayoutTop () + child .getHeight () + lp .bottomMargin );
1214
+ return getHeight ()
1215
+ + (mLastInsets != null ? mLastInsets .getSystemWindowInsetTop () : 0 )
1216
+ - offsetHelper .getLayoutTop ()
1217
+ - child .getHeight ()
1218
+ - lp .bottomMargin ;
1202
1219
}
1203
1220
1204
1221
private class OffsetUpdateListener implements AppBarLayout .OnOffsetChangedListener {
0 commit comments