Skip to content

Commit

Permalink
add weakreference
Browse files Browse the repository at this point in the history
  • Loading branch information
niorgai committed Jun 21, 2019
1 parent 36809fc commit d38c6ba
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Add the dependency
```groovy
dependencies {
// After AndroidX
implementation ('com.github.niorgai:StatusBarCompat:2.3.1', {
implementation ('com.github.niorgai:StatusBarCompat:2.3.2', {
exclude group: 'androidx.appcompat:appcompat'
exclude group: 'com.google.android.material:material'
})
Expand Down
24 changes: 15 additions & 9 deletions library/src/main/java/qiu/niorgai/StatusBarCompatLollipop.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.appbar.CollapsingToolbarLayout;

import java.lang.ref.WeakReference;

import androidx.appcompat.widget.Toolbar;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.core.view.OnApplyWindowInsetsListener;
Expand Down Expand Up @@ -103,7 +105,7 @@ static void translucentStatusBar(Activity activity, boolean hideStatusBarBackgro
*/
static void setStatusBarColorForCollapsingToolbar(Activity activity, final AppBarLayout appBarLayout, final CollapsingToolbarLayout collapsingToolbarLayout,
Toolbar toolbar, final int statusColor) {
final Window window = activity.getWindow();
Window window = activity.getWindow();

window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
Expand Down Expand Up @@ -150,16 +152,20 @@ public WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets)
}

collapsingToolbarLayout.setFitsSystemWindows(false);
final WeakReference<Window> windowWeakReference = new WeakReference<>(window);
appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
@Override
public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
if (Math.abs(verticalOffset) > appBarLayout.getHeight() - collapsingToolbarLayout.getScrimVisibleHeightTrigger()) {
if (window.getStatusBarColor() != statusColor) {
startColorAnimation(window.getStatusBarColor(), statusColor, collapsingToolbarLayout.getScrimAnimationDuration(), window);
}
} else {
if (window.getStatusBarColor() != Color.TRANSPARENT) {
startColorAnimation(window.getStatusBarColor(), Color.TRANSPARENT, collapsingToolbarLayout.getScrimAnimationDuration(), window);
final Window weakWindow = windowWeakReference.get();
if (weakWindow != null) {
if (Math.abs(verticalOffset) > appBarLayout.getHeight() - collapsingToolbarLayout.getScrimVisibleHeightTrigger()) {
if (weakWindow.getStatusBarColor() != statusColor) {
startColorAnimation(weakWindow.getStatusBarColor(), statusColor, collapsingToolbarLayout.getScrimAnimationDuration(), weakWindow);
}
} else {
if (weakWindow.getStatusBarColor() != Color.TRANSPARENT) {
startColorAnimation(weakWindow.getStatusBarColor(), Color.TRANSPARENT, collapsingToolbarLayout.getScrimAnimationDuration(), weakWindow);
}
}
}
}
Expand All @@ -171,7 +177,7 @@ public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
/**
* use ValueAnimator to change statusBarColor when using collapsingToolbarLayout
*/
static void startColorAnimation(int startColor, int endColor, long duration, final Window window) {
private static void startColorAnimation(int startColor, int endColor, long duration, final Window window) {
if (sAnimator != null) {
sAnimator.cancel();
}
Expand Down

0 comments on commit d38c6ba

Please sign in to comment.