8
8
import android .os .Build ;
9
9
import android .support .design .widget .AppBarLayout ;
10
10
import android .support .design .widget .CollapsingToolbarLayout ;
11
+ import android .support .design .widget .CoordinatorLayout ;
11
12
import android .support .v4 .view .OnApplyWindowInsetsListener ;
12
13
import android .support .v4 .view .ViewCompat ;
13
14
import android .support .v4 .view .WindowInsetsCompat ;
@@ -92,10 +93,11 @@ static void translucentStatusBar(Activity activity, boolean hideStatusBarBackgro
92
93
* compat for CollapsingToolbarLayout
93
94
*
94
95
* 1. change to full-screen mode(like translucentStatusBar).
95
- * 2. set View 's FitsSystemWindow to false .
96
- * 3. adjust toolbar 's height to layout .
97
- * 4. cancel CollapsingToolbarLayout 's WindowInsets, let it layout as normal(now setStatusBarScrimColor is useless) .
96
+ * 2. cancel CollapsingToolbarLayout 's WindowInsets, let it layout as normal(now setStatusBarScrimColor is useless) .
97
+ * 3. set View 's FitsSystemWindow to false .
98
+ * 4. adjust toolbar 's height to layout.
98
99
* 5. change statusBarColor by AppBarLayout's offset.
100
+ * 6. add Listener to change statusBarColor
99
101
*/
100
102
static void setStatusBarColorForCollapsingToolbar (Activity activity , final AppBarLayout appBarLayout , final CollapsingToolbarLayout collapsingToolbarLayout ,
101
103
Toolbar toolbar , final int statusColor ) {
@@ -106,6 +108,13 @@ static void setStatusBarColorForCollapsingToolbar(Activity activity, final AppBa
106
108
window .setStatusBarColor (Color .TRANSPARENT );
107
109
window .getDecorView ().setSystemUiVisibility (View .SYSTEM_UI_FLAG_VISIBLE );
108
110
111
+ ViewCompat .setOnApplyWindowInsetsListener (collapsingToolbarLayout , new OnApplyWindowInsetsListener () {
112
+ @ Override
113
+ public WindowInsetsCompat onApplyWindowInsets (View v , WindowInsetsCompat insets ) {
114
+ return insets ;
115
+ }
116
+ });
117
+
109
118
ViewGroup mContentView = (ViewGroup ) window .findViewById (Window .ID_ANDROID_CONTENT );
110
119
View mChildView = mContentView .getChildAt (0 );
111
120
if (mChildView != null ) {
@@ -124,12 +133,18 @@ static void setStatusBarColorForCollapsingToolbar(Activity activity, final AppBa
124
133
toolbar .setTag (true );
125
134
}
126
135
127
- ViewCompat .setOnApplyWindowInsetsListener (collapsingToolbarLayout , new OnApplyWindowInsetsListener () {
128
- @ Override
129
- public WindowInsetsCompat onApplyWindowInsets (View v , WindowInsetsCompat insets ) {
130
- return insets ;
136
+ CoordinatorLayout .Behavior behavior = ((CoordinatorLayout .LayoutParams ) appBarLayout .getLayoutParams ()).getBehavior ();
137
+ if (behavior != null && behavior instanceof AppBarLayout .Behavior ) {
138
+ int verticalOffset = ((AppBarLayout .Behavior ) behavior ).getTopAndBottomOffset ();
139
+ if (Math .abs (verticalOffset ) > appBarLayout .getHeight () - collapsingToolbarLayout .getScrimVisibleHeightTrigger ()) {
140
+ window .setStatusBarColor (statusColor );
141
+ } else {
142
+ window .setStatusBarColor (Color .TRANSPARENT );
131
143
}
132
- });
144
+ } else {
145
+ window .setStatusBarColor (Color .TRANSPARENT );
146
+ }
147
+
133
148
collapsingToolbarLayout .setFitsSystemWindows (false );
134
149
appBarLayout .addOnOffsetChangedListener (new AppBarLayout .OnOffsetChangedListener () {
135
150
@ Override
0 commit comments