Skip to content

Commit

Permalink
fix bugs for CollapsingToolbarLayout in Lollipop
Browse files Browse the repository at this point in the history
  • Loading branch information
niorgai committed Nov 7, 2016
1 parent 32a0fda commit 431ba1c
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 49 deletions.
9 changes: 1 addition & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Add the dependency
```groovy
dependencies {
compile 'com.github.niorgai:StatusBarCompat:2.0.0'
compile 'com.github.niorgai:StatusBarCompat:2.1.0'
}
```

Expand Down Expand Up @@ -78,9 +78,6 @@ These problem only show in SDK between 19 and 21:
1. If work with `TabActivity`, It will show a black line in contentView, so you can call `StatusBarCompat.setStatusBarColor(Activity activity, int statusColor, int alpha)` and the suggest alpha is 112.
2. If first view is `DrawerLayout`, let its child view set `fitsSystemWindow` to false.

These problem only show in SDK above 21:
1. Sometime change tab between `translucentStatusBar` and `setStatusBarColorForCollapsingToolbar` in same Activity will make `CollapsingToolBarLayout` don't fitsSystemWindow.


# Link:
1. Blog [Android-translucent-status-bar](http://niorgai.github.io/2016/03/20/Android-transulcent-status-bar/).
Expand Down Expand Up @@ -130,10 +127,6 @@ Demo 只有一个 Activity , 分四个 Tab.
1. 如果用在 TabActivity 上, 会有一条黑线在状态栏下面, 推荐使用 `StatusBarCompat.setStatusBarColor(Activity activity, int statusColor, int alpha)` 方法, 推荐的透明值为 112.
2. 如果 layout 中第一个 View 为 DrawerLayout, 那么它的子 View 的 fitsSystemWindow 需要设置为 false.

这些问题仅仅出现在 5.0 上

1. 在同一个 Activity 上, `translucentStatusBar``setStatusBarColorForCollapsingToolbar` 切换时, 有时候 `CollapsingToolbarLayout` 没有 fitsSystemWindow.

# 更多
在我的博客[Android-translucent-status-bar](http://niorgai.github.io/2016/03/20/Android-transulcent-status-bar/)中可以查看更多细节.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
mCollapsingToolbarLayout.setTitle(getString(R.string.app_name));
mToolbar = (Toolbar) view.findViewById(R.id.toolbar);
((AppCompatActivity) getActivity()).setSupportActionBar(mToolbar);
((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
StatusBarCompat.setStatusBarColorForCollapsingToolbar(getActivity(), mAppBarLayout, mCollapsingToolbarLayout, mToolbar, Color.YELLOW);
}

Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.0'
classpath 'com.android.tools.build:gradle:2.2.2'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
14 changes: 8 additions & 6 deletions library/src/main/java/qiu/niorgai/StatusBarCompat.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import android.app.Activity;
import android.os.Build;
import android.support.annotation.ColorInt;
import android.support.annotation.NonNull;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v7.widget.Toolbar;
Expand Down Expand Up @@ -29,36 +31,36 @@ private static int calculateStatusBarColor(int color, int alpha) {
* @param statusColor color
* @param alpha 0 - 255
*/
public static void setStatusBarColor(Activity activity, int statusColor, int alpha) {
public static void setStatusBarColor(@NonNull Activity activity, @ColorInt int statusColor, int alpha) {
setStatusBarColor(activity, calculateStatusBarColor(statusColor, alpha));
}

public static void setStatusBarColor(Activity activity, int statusColor) {
public static void setStatusBarColor(@NonNull Activity activity, @ColorInt int statusColor) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
StatusBarCompatLollipop.setStatusBarColor(activity, statusColor);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
StatusBarCompatKitKat.setStatusBarColor(activity, statusColor);
}
}

public static void translucentStatusBar(Activity activity) {
public static void translucentStatusBar(@NonNull Activity activity) {
translucentStatusBar(activity, false);
}

/**
* change to full screen mode
* @param hideStatusBarBackground hide status bar alpha Background when SDK > 21, true if hide it
*/
public static void translucentStatusBar(Activity activity, boolean hideStatusBarBackground) {
public static void translucentStatusBar(@NonNull Activity activity, boolean hideStatusBarBackground) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
StatusBarCompatLollipop.translucentStatusBar(activity, hideStatusBarBackground);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
StatusBarCompatKitKat.translucentStatusBar(activity);
}
}

public static void setStatusBarColorForCollapsingToolbar(Activity activity, AppBarLayout appBarLayout, CollapsingToolbarLayout collapsingToolbarLayout,
Toolbar toolbar, int statusColor) {
public static void setStatusBarColorForCollapsingToolbar(@NonNull Activity activity, AppBarLayout appBarLayout, CollapsingToolbarLayout collapsingToolbarLayout,
Toolbar toolbar, @ColorInt int statusColor) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
StatusBarCompatLollipop.setStatusBarColorForCollapsingToolbar(activity, appBarLayout, collapsingToolbarLayout, toolbar, statusColor);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Expand Down
8 changes: 4 additions & 4 deletions library/src/main/java/qiu/niorgai/StatusBarCompatKitKat.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* Created by qiu on 8/27/16.
*/
@TargetApi(Build.VERSION_CODES.KITKAT)
public class StatusBarCompatKitKat {
class StatusBarCompatKitKat {

private static final String TAG_FAKE_STATUS_BAR_VIEW = "statusBarView";
private static final String TAG_MARGIN_ADDED = "marginAdded";
Expand Down Expand Up @@ -107,7 +107,7 @@ private static void removeMarginTopOfContentChild(View mContentChild, int status
* 4. addMarginTopToContentChild
* 5. cancel ContentChild's fitsSystemWindow
*/
public static void setStatusBarColor(Activity activity, int statusColor) {
static void setStatusBarColor(Activity activity, int statusColor) {
Window window = activity.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

Expand All @@ -131,7 +131,7 @@ public static void setStatusBarColor(Activity activity, int statusColor) {
* 3. removeMarginTopOfContentChild
* 4. cancel ContentChild's fitsSystemWindow
*/
public static void translucentStatusBar(Activity activity) {
static void translucentStatusBar(Activity activity) {
Window window = activity.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

Expand All @@ -153,7 +153,7 @@ public static void translucentStatusBar(Activity activity) {
* 4. removeMarginTopOfContentChild
* 5. add OnOffsetChangedListener to change statusBarView's alpha
*/
public static void setStatusBarColorForCollapsingToolbar(Activity activity, final AppBarLayout appBarLayout, final CollapsingToolbarLayout collapsingToolbarLayout,
static void setStatusBarColorForCollapsingToolbar(Activity activity, final AppBarLayout appBarLayout, final CollapsingToolbarLayout collapsingToolbarLayout,
Toolbar toolbar, int statusColor) {
Window window = activity.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
Expand Down
67 changes: 37 additions & 30 deletions library/src/main/java/qiu/niorgai/StatusBarCompatLollipop.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.support.design.widget.AppBarLayout;
Expand All @@ -20,12 +21,24 @@
* Created by qiu on 8/27/16.
*/
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public class StatusBarCompatLollipop {
class StatusBarCompatLollipop {

/**
* return statusBar's Height in pixels
*/
private static int getStatusBarHeight(Context context) {
int result = 0;
int resId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resId > 0) {
result = context.getResources().getDimensionPixelOffset(resId);
}
return result;
}

/**
* set StatusBarColor
*/
public static void setStatusBarColor(Activity activity, int statusColor) {
static void setStatusBarColor(Activity activity, int statusColor) {
Window window = activity.getWindow();

window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
Expand All @@ -36,13 +49,7 @@ public static void setStatusBarColor(Activity activity, int statusColor) {
ViewGroup mContentView = (ViewGroup) window.findViewById(Window.ID_ANDROID_CONTENT);
View mChildView = mContentView.getChildAt(0);
if (mChildView != null) {
ViewCompat.setOnApplyWindowInsetsListener(mChildView, new OnApplyWindowInsetsListener() {
@Override
public WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets) {
return insets;
}
});
ViewCompat.setFitsSystemWindows(mChildView, true);
ViewCompat.setFitsSystemWindows(mChildView, false);
ViewCompat.requestApplyInsets(mChildView);
}
}
Expand All @@ -51,7 +58,7 @@ public WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets)
* translucentStatusBar(full-screen)
* @param hideStatusBarBackground hide statusBar's shadow
*/
public static void translucentStatusBar(Activity activity, boolean hideStatusBarBackground) {
static void translucentStatusBar(Activity activity, boolean hideStatusBarBackground) {
Window window = activity.getWindow();

window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
Expand All @@ -67,22 +74,15 @@ public static void translucentStatusBar(Activity activity, boolean hideStatusBar
ViewGroup mContentView = (ViewGroup) window.findViewById(Window.ID_ANDROID_CONTENT);
View mChildView = mContentView.getChildAt(0);
if (mChildView != null) {
ViewCompat.setOnApplyWindowInsetsListener(mChildView, new OnApplyWindowInsetsListener() {
@Override
public WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets) {
return insets;
}
});
ViewCompat.setFitsSystemWindows(mChildView, false);
ViewCompat.requestApplyInsets(mChildView);

}
}

/**
* compat for CollapsingToolbarLayout
*/
public static void setStatusBarColorForCollapsingToolbar(Activity activity, final AppBarLayout appBarLayout, CollapsingToolbarLayout collapsingToolbarLayout,
static void setStatusBarColorForCollapsingToolbar(Activity activity, final AppBarLayout appBarLayout, CollapsingToolbarLayout collapsingToolbarLayout,
Toolbar toolbar, int statusColor) {
Window window = activity.getWindow();

Expand All @@ -94,22 +94,29 @@ public static void setStatusBarColorForCollapsingToolbar(Activity activity, fina
ViewGroup mContentView = (ViewGroup) window.findViewById(Window.ID_ANDROID_CONTENT);
View mChildView = mContentView.getChildAt(0);
if (mChildView != null) {
ViewCompat.setOnApplyWindowInsetsListener(mChildView, new OnApplyWindowInsetsListener() {
@Override
public WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets) {
return insets;
}
});
ViewCompat.setFitsSystemWindows(mChildView, true);
ViewCompat.setFitsSystemWindows(mChildView, false);
ViewCompat.requestApplyInsets(mChildView);
}

((View) appBarLayout.getParent()).setFitsSystemWindows(true);
appBarLayout.setFitsSystemWindows(true);
collapsingToolbarLayout.setFitsSystemWindows(true);
collapsingToolbarLayout.getChildAt(0).setFitsSystemWindows(true);
toolbar.setFitsSystemWindows(false);
((View) appBarLayout.getParent()).setFitsSystemWindows(false);
appBarLayout.setFitsSystemWindows(false);

toolbar.setFitsSystemWindows(true);
if (toolbar.getTag() == null) {
CollapsingToolbarLayout.LayoutParams lp = (CollapsingToolbarLayout.LayoutParams) toolbar.getLayoutParams();
lp.height += getStatusBarHeight(activity);
toolbar.setLayoutParams(lp);
toolbar.setTag(true);
}

collapsingToolbarLayout.setFitsSystemWindows(false);
ViewCompat.setOnApplyWindowInsetsListener(collapsingToolbarLayout, new OnApplyWindowInsetsListener() {
@Override
public WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets) {
return insets;
}
});
collapsingToolbarLayout.getChildAt(0).setFitsSystemWindows(false);
collapsingToolbarLayout.setStatusBarScrimColor(statusColor);
}
}

0 comments on commit 431ba1c

Please sign in to comment.