9
9
10
10
import com .blankj .utilcode .R ;
11
11
12
+ import java .lang .ref .ReferenceQueue ;
13
+ import java .lang .ref .WeakReference ;
14
+
12
15
/**
13
16
* <pre>
14
17
* author: Blankj
15
18
* blog : http://blankj.com
16
19
* time : 2016/10/16
17
- * desc : SnackBar相关工具类
20
+ * desc : Snackbar相关工具类
18
21
* </pre>
19
22
*/
20
23
public class SnackbarUtils {
@@ -23,7 +26,7 @@ private SnackbarUtils() {
23
26
throw new UnsupportedOperationException ("u can't instantiate me..." );
24
27
}
25
28
26
- private static Snackbar snackbar ;
29
+ private static WeakReference < Snackbar > snackbarWeakReference ;
27
30
28
31
/**
29
32
* 显示短时snackbar
@@ -69,10 +72,10 @@ public static void showLongSnackbar(View parent, CharSequence text, int textColo
69
72
/**
70
73
* 显示长时snackbar
71
74
*
72
- * @param parent 视图(CoordinatorLayout或者DecorView)
73
- * @param text 文本
74
- * @param textColor 文本颜色
75
- * @param bgColor 背景色
75
+ * @param parent 视图(CoordinatorLayout或者DecorView)
76
+ * @param text 文本
77
+ * @param textColor 文本颜色
78
+ * @param bgColor 背景色
76
79
* @param actionText 事件文本
77
80
* @param actionTextColor 事件文本颜色
78
81
* @param listener 监听器
@@ -132,19 +135,19 @@ private static void showSnackbar(View parent, CharSequence text, int duration, i
132
135
default :
133
136
case Snackbar .LENGTH_SHORT :
134
137
case Snackbar .LENGTH_LONG :
135
- snackbar = Snackbar .make (parent , text , duration );
138
+ snackbarWeakReference = new WeakReference <>( Snackbar .make (parent , text , duration ) );
136
139
break ;
137
140
case Snackbar .LENGTH_INDEFINITE :
138
- snackbar = Snackbar .make (parent , text , Snackbar .LENGTH_INDEFINITE ).setDuration (duration );
141
+ snackbarWeakReference = new WeakReference <>( Snackbar .make (parent , text , Snackbar .LENGTH_INDEFINITE ).setDuration (duration ) );
139
142
}
140
- View view = snackbar .getView ();
143
+ View view = snackbarWeakReference . get () .getView ();
141
144
((TextView ) view .findViewById (R .id .snackbar_text )).setTextColor (textColor );
142
145
view .setBackgroundColor (bgColor );
143
146
if (actionText != null && actionText .length () > 0 && listener != null ) {
144
- snackbar .setActionTextColor (actionTextColor );
145
- snackbar .setAction (actionText , listener );
147
+ snackbarWeakReference . get () .setActionTextColor (actionTextColor );
148
+ snackbarWeakReference . get () .setAction (actionText , listener );
146
149
}
147
- snackbar .show ();
150
+ snackbarWeakReference . get () .show ();
148
151
}
149
152
150
153
/**
@@ -155,23 +158,26 @@ private static void showSnackbar(View parent, CharSequence text, int duration, i
155
158
* @param index 位置(the position at which to add the child or -1 to add last)
156
159
*/
157
160
public static void addView (int layoutId , int index ) {
158
- View view = snackbar .getView ();
159
- Snackbar .SnackbarLayout layout = (Snackbar .SnackbarLayout ) view ;
160
- View child = LayoutInflater .from (view .getContext ()).inflate (layoutId , null );
161
- LinearLayout .LayoutParams params = new LinearLayout .LayoutParams (
162
- LinearLayout .LayoutParams .WRAP_CONTENT ,
163
- LinearLayout .LayoutParams .WRAP_CONTENT );
164
- params .gravity = Gravity .CENTER_VERTICAL ;
165
- layout .addView (child , index , params );
161
+ Snackbar snackbar = snackbarWeakReference .get ();
162
+ if (snackbar != null ) {
163
+ View view = snackbar .getView ();
164
+ Snackbar .SnackbarLayout layout = (Snackbar .SnackbarLayout ) view ;
165
+ View child = LayoutInflater .from (view .getContext ()).inflate (layoutId , null );
166
+ LinearLayout .LayoutParams params = new LinearLayout .LayoutParams (
167
+ LinearLayout .LayoutParams .WRAP_CONTENT ,
168
+ LinearLayout .LayoutParams .WRAP_CONTENT );
169
+ params .gravity = Gravity .CENTER_VERTICAL ;
170
+ layout .addView (child , index , params );
171
+ }
166
172
}
167
173
168
174
/**
169
175
* 取消snackbar显示
170
176
*/
171
177
public static void dismissSnackbar () {
172
- if (snackbar != null ) {
173
- snackbar .dismiss ();
174
- snackbar = null ;
178
+ if (snackbarWeakReference != null && snackbarWeakReference . get () != null ) {
179
+ snackbarWeakReference . get () .dismiss ();
180
+ snackbarWeakReference = null ;
175
181
}
176
182
}
177
183
}
0 commit comments