Skip to content

Commit ac8ea73

Browse files
authored
Merge pull request Blankj#112 from c0state/fix_snackbar_indefinite_duration
Fix indefinite duration on snackbar example and use proper root view for snackbar parent
2 parents e280ac2 + 7d593aa commit ac8ea73

File tree

5 files changed

+27
-83
lines changed

5 files changed

+27
-83
lines changed

app/src/main/java/com/blankj/androidutilcode/activities/SnackbarActivity.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import android.graphics.Color;
55
import android.os.Bundle;
66
import android.view.View;
7+
import android.view.ViewGroup;
78

89
import com.blankj.androidutilcode.R;
910
import com.blankj.utilcode.utils.SnackbarUtils;
@@ -38,12 +39,14 @@ protected void onCreate(Bundle savedInstanceState) {
3839

3940
@Override
4041
public void onClick(View view) {
42+
final View snackBarRootView = findViewById(android.R.id.content);
43+
4144
switch (view.getId()) {
4245
case R.id.btn_short_snackbar:
43-
SnackbarUtils.showShortSnackbar(getWindow().getDecorView(), "short snackbar", Color.WHITE, Color.BLUE);
46+
SnackbarUtils.showShortSnackbar(snackBarRootView, "short snackbar", Color.WHITE, Color.BLUE);
4447
break;
4548
case R.id.btn_short_snackbar_with_action:
46-
SnackbarUtils.showShortSnackbar(getWindow().getDecorView(), "short snackbar", Color.WHITE, Color.BLUE,
49+
SnackbarUtils.showShortSnackbar(snackBarRootView, "short snackbar", Color.WHITE, Color.BLUE,
4750
"Short", Color.YELLOW, new View.OnClickListener() {
4851
@Override
4952
public void onClick(View v) {
@@ -52,10 +55,10 @@ public void onClick(View v) {
5255
});
5356
break;
5457
case R.id.btn_long_snackbar:
55-
SnackbarUtils.showLongSnackbar(getWindow().getDecorView(), "long snackbar", Color.WHITE, Color.GREEN);
58+
SnackbarUtils.showLongSnackbar(snackBarRootView, "long snackbar", Color.WHITE, Color.GREEN);
5659
break;
5760
case R.id.btn_long_snackbar_with_action:
58-
SnackbarUtils.showLongSnackbar(getWindow().getDecorView(), "long snackbar", Color.WHITE, Color.GREEN,
61+
SnackbarUtils.showLongSnackbar(snackBarRootView, "long snackbar", Color.WHITE, Color.GREEN,
5962
"Long", Color.YELLOW, new View.OnClickListener() {
6063
@Override
6164
public void onClick(View v) {
@@ -64,10 +67,10 @@ public void onClick(View v) {
6467
});
6568
break;
6669
case R.id.btn_indefinite_snackbar:
67-
SnackbarUtils.showIndefiniteSnackbar(getWindow().getDecorView(), "Indefinite snackbar", 5000, Color.WHITE, Color.RED);
70+
SnackbarUtils.showIndefiniteSnackbar(snackBarRootView, "Indefinite snackbar", Color.WHITE, Color.RED);
6871
break;
6972
case R.id.btn_indefinite_snackbar_with_action:
70-
SnackbarUtils.showIndefiniteSnackbar(getWindow().getDecorView(), "Indefinite snackbar", 5000, Color.WHITE, Color.RED,
73+
SnackbarUtils.showIndefiniteSnackbar(snackBarRootView, "Indefinite snackbar", Color.WHITE, Color.RED,
7174
"Indefinite", Color.YELLOW, new View.OnClickListener() {
7275
@Override
7376
public void onClick(View v) {
@@ -76,11 +79,11 @@ public void onClick(View v) {
7679
});
7780
break;
7881
case R.id.btn_add_view:
79-
SnackbarUtils.showShortSnackbar(getWindow().getDecorView(), "short snackbar", Color.WHITE, Color.BLUE);
82+
SnackbarUtils.showShortSnackbar(snackBarRootView, "short snackbar", Color.WHITE, Color.BLUE);
8083
SnackbarUtils.addView(R.layout.snackbar_add, 0);
8184
break;
8285
case R.id.btn_add_view_with_action:
83-
SnackbarUtils.showLongSnackbar(getWindow().getDecorView(), "short snackbar", Color.WHITE, Color.BLUE,
86+
SnackbarUtils.showLongSnackbar(snackBarRootView, "short snackbar", Color.WHITE, Color.BLUE,
8487
"Short", Color.YELLOW, new View.OnClickListener() {
8588
@Override
8689
public void onClick(View v) {

app/src/main/res/values/string.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@
7070
<!--SnackBar相关工具类-->
7171
<string name="snackbar.show_short">Show Short Snackbar</string>
7272
<string name="snackbar.show_short_with_action">Show Short Snackbar With Action</string>
73-
<string name="snackbar.show_long">Show Short Snackbar</string>
74-
<string name="snackbar.show_long_with_action">Show Short Snackbar With Action</string>
73+
<string name="snackbar.show_long">Show Long Snackbar</string>
74+
<string name="snackbar.show_long_with_action">Show Long Snackbar With Action</string>
7575
<string name="snackbar.show_indefinite">Show Indefinite Snackbar</string>
7676
<string name="snackbar.show_indefinite_with_action">Show Indefinite Snackbar With Action</string>
7777
<string name="snackbar.add_view">Add View</string>

utilcode/src/main/java/com/blankj/utilcode/utils/SnackbarUtils.java

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.blankj.utilcode.utils;
22

33
import android.support.annotation.ColorInt;
4+
import android.support.design.widget.BaseTransientBottomBar;
45
import android.support.design.widget.Snackbar;
56
import android.view.Gravity;
67
import android.view.LayoutInflater;
@@ -92,8 +93,8 @@ public static void showLongSnackbar(View parent, CharSequence text, @ColorInt in
9293
* @param textColor 文本颜色
9394
* @param bgColor 背景色
9495
*/
95-
public static void showIndefiniteSnackbar(View parent, CharSequence text, int duration, @ColorInt int textColor, @ColorInt int bgColor) {
96-
showSnackbar(parent, text, duration, textColor, bgColor, null, -1, null);
96+
public static void showIndefiniteSnackbar(View parent, CharSequence text, @ColorInt int textColor, @ColorInt int bgColor) {
97+
showSnackbar(parent, text, Snackbar.LENGTH_INDEFINITE, textColor, bgColor, null, -1, null);
9798
}
9899

99100
/**
@@ -108,9 +109,9 @@ public static void showIndefiniteSnackbar(View parent, CharSequence text, int du
108109
* @param actionTextColor 事件文本颜色
109110
* @param listener 监听器
110111
*/
111-
public static void showIndefiniteSnackbar(View parent, CharSequence text, int duration, @ColorInt int textColor, @ColorInt int bgColor,
112+
public static void showIndefiniteSnackbar(View parent, CharSequence text, @ColorInt int textColor, @ColorInt int bgColor,
112113
CharSequence actionText, int actionTextColor, View.OnClickListener listener) {
113-
showSnackbar(parent, text, duration, textColor, bgColor,
114+
showSnackbar(parent, text, Snackbar.LENGTH_INDEFINITE, textColor, bgColor,
114115
actionText, actionTextColor, listener);
115116
}
116117

@@ -126,26 +127,21 @@ public static void showIndefiniteSnackbar(View parent, CharSequence text, int du
126127
* @param actionTextColor 事件文本颜色
127128
* @param listener 监听器
128129
*/
129-
private static void showSnackbar(View parent, CharSequence text, int duration, @ColorInt int textColor, @ColorInt int bgColor,
130-
CharSequence actionText, int actionTextColor, View.OnClickListener listener) {
131-
switch (duration) {
132-
default:
133-
case Snackbar.LENGTH_SHORT:
134-
case Snackbar.LENGTH_LONG:
135-
snackbarWeakReference = new WeakReference<>(Snackbar.make(parent, text, duration));
136-
break;
137-
case Snackbar.LENGTH_INDEFINITE:
138-
snackbarWeakReference = new WeakReference<>(Snackbar.make(parent, text, Snackbar.LENGTH_INDEFINITE).setDuration(duration));
139-
}
130+
private static void showSnackbar(View parent, CharSequence text,
131+
@BaseTransientBottomBar.Duration int duration,
132+
@ColorInt int textColor, @ColorInt int bgColor,
133+
CharSequence actionText, int actionTextColor,
134+
View.OnClickListener listener) {
135+
snackbarWeakReference = new WeakReference<>(Snackbar.make(parent, text, duration));
140136
Snackbar snackbar = snackbarWeakReference.get();
141137
View view = snackbar.getView();
142138
snackbar.setActionTextColor(textColor);
143139
view.setBackgroundColor(bgColor);
144140
if (actionText != null && actionText.length() > 0 && listener != null) {
145-
snackbarWeakReference.get().setActionTextColor(actionTextColor);
146-
snackbarWeakReference.get().setAction(actionText, listener);
141+
snackbar.setActionTextColor(actionTextColor);
142+
snackbar.setAction(actionText, listener);
147143
}
148-
snackbarWeakReference.get().show();
144+
snackbar.show();
149145
}
150146

151147
/**

utilcode/src/main/res/drawable/snackbar_action_bg.xml

Lines changed: 0 additions & 22 deletions
This file was deleted.

utilcode/src/main/res/drawable/snackbar_action_bg_focused.xml

Lines changed: 0 additions & 33 deletions
This file was deleted.

0 commit comments

Comments
 (0)