99
1010import com .blankj .utilcode .R ;
1111
12+ import java .lang .ref .ReferenceQueue ;
13+ import java .lang .ref .WeakReference ;
14+
1215/**
1316 * <pre>
1417 * author: Blankj
1518 * blog : http://blankj.com
1619 * time : 2016/10/16
17- * desc : SnackBar相关工具类
20+ * desc : Snackbar相关工具类
1821 * </pre>
1922 */
2023public class SnackbarUtils {
@@ -23,7 +26,7 @@ private SnackbarUtils() {
2326 throw new UnsupportedOperationException ("u can't instantiate me..." );
2427 }
2528
26- private static Snackbar snackbar ;
29+ private static WeakReference < Snackbar > snackbarWeakReference ;
2730
2831 /**
2932 * 显示短时snackbar
@@ -69,10 +72,10 @@ public static void showLongSnackbar(View parent, CharSequence text, int textColo
6972 /**
7073 * 显示长时snackbar
7174 *
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 背景色
7679 * @param actionText 事件文本
7780 * @param actionTextColor 事件文本颜色
7881 * @param listener 监听器
@@ -132,19 +135,19 @@ private static void showSnackbar(View parent, CharSequence text, int duration, i
132135 default :
133136 case Snackbar .LENGTH_SHORT :
134137 case Snackbar .LENGTH_LONG :
135- snackbar = Snackbar .make (parent , text , duration );
138+ snackbarWeakReference = new WeakReference <>( Snackbar .make (parent , text , duration ) );
136139 break ;
137140 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 ) );
139142 }
140- View view = snackbar .getView ();
143+ View view = snackbarWeakReference . get () .getView ();
141144 ((TextView ) view .findViewById (R .id .snackbar_text )).setTextColor (textColor );
142145 view .setBackgroundColor (bgColor );
143146 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 );
146149 }
147- snackbar .show ();
150+ snackbarWeakReference . get () .show ();
148151 }
149152
150153 /**
@@ -155,23 +158,26 @@ private static void showSnackbar(View parent, CharSequence text, int duration, i
155158 * @param index 位置(the position at which to add the child or -1 to add last)
156159 */
157160 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+ }
166172 }
167173
168174 /**
169175 * 取消snackbar显示
170176 */
171177 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 ;
175181 }
176182 }
177183}
0 commit comments