Skip to content

Commit b0f09e2

Browse files
committed
see 08/31 log
1 parent 7068e6c commit b0f09e2

File tree

6 files changed

+80
-32
lines changed

6 files changed

+80
-32
lines changed

app/src/main/java/com/blankj/androidutilcode/core/snackbar/SnackbarActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,11 @@ public void onClick(View v) {
141141
break;
142142

143143
case R.id.btn_add_view:
144+
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
144145
SnackbarUtils.with(snackBarRootView)
145146
.setBgColor(Color.TRANSPARENT)
146147
.setDuration(SnackbarUtils.LENGTH_INDEFINITE)
147148
.show();
148-
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
149149
SnackbarUtils.addView(R.layout.snackbar_custom, params);
150150
break;
151151

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.blankj.androidutilcode.core.toast;
2+
3+
import android.support.annotation.NonNull;
4+
import android.view.View;
5+
import android.widget.TextView;
6+
7+
import com.blankj.androidutilcode.R;
8+
import com.blankj.utilcode.util.ToastUtils;
9+
10+
/**
11+
* <pre>
12+
* author: Blankj
13+
* blog : http://blankj.com
14+
* time : 2017/08/31
15+
* desc :
16+
* </pre>
17+
*/
18+
public class MyToast {
19+
20+
public static void showMyToast(@NonNull final String message){
21+
View toastView = ToastUtils.showCustomLongSafe(R.layout.toast_custom);
22+
TextView tvMessage = (TextView) toastView.findViewById(R.id.tv_toast_message);
23+
tvMessage.setText(message);
24+
}
25+
26+
}

app/src/main/java/com/blankj/androidutilcode/core/toast/ToastActivity.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,16 @@ public int bindLayout() {
4141
public void initView(Bundle savedInstanceState, View view) {
4242
getToolBar().setTitle(getString(R.string.demo_toast));
4343

44-
view.findViewById(R.id.btn_show_short_toast_safe).setOnClickListener(this);
45-
view.findViewById(R.id.btn_show_short_toast).setOnClickListener(this);
46-
view.findViewById(R.id.btn_show_long_toast_safe).setOnClickListener(this);
47-
view.findViewById(R.id.btn_show_long_toast).setOnClickListener(this);
48-
view.findViewById(R.id.btn_show_green_font).setOnClickListener(this);
49-
view.findViewById(R.id.btn_show_custom_bg).setOnClickListener(this);
50-
view.findViewById(R.id.btn_show_span).setOnClickListener(this);
51-
view.findViewById(R.id.btn_show_custom_view).setOnClickListener(this);
52-
view.findViewById(R.id.btn_show_middle).setOnClickListener(this);
53-
view.findViewById(R.id.btn_cancel_toast).setOnClickListener(this);
44+
findViewById(R.id.btn_show_short_toast_safe).setOnClickListener(this);
45+
findViewById(R.id.btn_show_short_toast).setOnClickListener(this);
46+
findViewById(R.id.btn_show_long_toast_safe).setOnClickListener(this);
47+
findViewById(R.id.btn_show_long_toast).setOnClickListener(this);
48+
findViewById(R.id.btn_show_green_font).setOnClickListener(this);
49+
findViewById(R.id.btn_show_custom_bg).setOnClickListener(this);
50+
findViewById(R.id.btn_show_span).setOnClickListener(this);
51+
findViewById(R.id.btn_show_custom_view).setOnClickListener(this);
52+
findViewById(R.id.btn_show_middle).setOnClickListener(this);
53+
findViewById(R.id.btn_cancel_toast).setOnClickListener(this);
5454
}
5555

5656
@Override
@@ -98,10 +98,11 @@ public void run() {
9898
.setFontSize(24, true)
9999
.setIconMargin(R.mipmap.ic_launcher, 32, SpanUtils.ALIGN_CENTER)
100100
.append(" ").setFontSize(0)
101-
.create());
101+
.create()
102+
);
102103
break;
103104
case R.id.btn_show_custom_view:
104-
ToastUtils.showCustomLong(R.layout.toast_custom);
105+
MyToast.showMyToast(getString(R.string.toast_custom_view));
105106
break;
106107
case R.id.btn_show_middle:
107108
ToastUtils.setGravity(Gravity.CENTER, 0, 0);
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<TextView
33
xmlns:android="http://schemas.android.com/apk/res/android"
4+
android:id="@+id/tv_toast_message"
45
android:layout_width="wrap_content"
56
android:layout_height="wrap_content"
67
android:layout_gravity="center"
@@ -9,6 +10,5 @@
910
android:drawablePadding="@dimen/spacing_16"
1011
android:gravity="center"
1112
android:padding="@dimen/spacing_16"
12-
android:text="@string/toast_custom_view"
1313
android:textColor="@color/white"
1414
android:textSize="@dimen/font_24" />

utilcode/src/main/java/com/blankj/utilcode/util/ToastUtils.java

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,21 @@ public static void setGravity(final int gravity, final int xOffset, final int yO
6060
*
6161
* @param layoutId 视图
6262
*/
63-
public static void setView(@LayoutRes final int layoutId) {
63+
public static View setView(@LayoutRes final int layoutId) {
6464
LayoutInflater inflate = (LayoutInflater) Utils.getApp().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
65-
sViewWeakReference = new WeakReference<>(inflate.inflate(layoutId, null));
65+
final View toastView = inflate.inflate(layoutId, null);
66+
sViewWeakReference = new WeakReference<>(toastView);
67+
return toastView;
6668
}
6769

6870
/**
6971
* 设置吐司view
7072
*
7173
* @param view 视图
7274
*/
73-
public static void setView(final View view) {
75+
public static View setView(final View view) {
7476
sViewWeakReference = view == null ? null : new WeakReference<>(view);
77+
return view;
7578
}
7679

7780
/**
@@ -312,85 +315,94 @@ public static void showLong(final String format, final Object... args) {
312315
/**
313316
* 安全地显示短时自定义吐司
314317
*/
315-
public static void showCustomShortSafe(@LayoutRes final int layoutId) {
318+
public static View showCustomShortSafe(@LayoutRes final int layoutId) {
319+
final View view = setView(layoutId);
316320
HANDLER.post(new Runnable() {
317321
@Override
318322
public void run() {
319-
setView(layoutId);
320323
show("", Toast.LENGTH_SHORT);
321324
}
322325
});
326+
return view;
323327
}
324328

325329
/**
326330
* 安全地显示长时自定义吐司
327331
*/
328-
public static void showCustomLongSafe(@LayoutRes final int layoutId) {
332+
public static View showCustomLongSafe(@LayoutRes final int layoutId) {
333+
final View view = setView(layoutId);
329334
HANDLER.post(new Runnable() {
330335
@Override
331336
public void run() {
332-
setView(layoutId);
333337
show("", Toast.LENGTH_LONG);
334338
}
335339
});
340+
return view;
336341
}
337342

338343
/**
339344
* 显示短时自定义吐司
340345
*/
341-
public static void showCustomShort(@LayoutRes final int layoutId) {
342-
setView(layoutId);
346+
public static View showCustomShort(@LayoutRes final int layoutId) {
347+
final View view = setView(layoutId);
343348
show("", Toast.LENGTH_SHORT);
349+
return view;
344350
}
345351

346352
/**
347353
* 显示长时自定义吐司
348354
*/
349-
public static void showCustomLong(@LayoutRes final int layoutId) {
350-
setView(layoutId);
355+
public static View showCustomLong(@LayoutRes final int layoutId) {
356+
final View view = setView(layoutId);
351357
show("", Toast.LENGTH_LONG);
358+
return view;
352359
}
353360

354361
/**
355362
* 安全地显示短时自定义吐司
356363
*/
357-
public static void showCustomShortSafe(@NonNull final View view) {
364+
public static View showCustomShortSafe(@NonNull final View view) {
365+
setView(view);
358366
HANDLER.post(new Runnable() {
359367
@Override
360368
public void run() {
361-
setView(view);
362369
show("", Toast.LENGTH_SHORT);
363370
}
364371
});
372+
return view;
365373
}
366374

367375
/**
368376
* 安全地显示长时自定义吐司
369377
*/
370-
public static void showCustomLongSafe(@NonNull final View view) {
378+
public static View showCustomLongSafe(@NonNull final View view) {
379+
setView(view);
371380
HANDLER.post(new Runnable() {
372381
@Override
373382
public void run() {
374383
setView(view);
375384
show("", Toast.LENGTH_LONG);
376385
}
377386
});
387+
return view;
378388
}
379389

380390
/**
381391
* 显示短时自定义吐司
382392
*/
383-
public static void showCustomShort(@NonNull final View view) {
393+
public static View showCustomShort(@NonNull final View view) {
384394
setView(view);
385395
show("", Toast.LENGTH_SHORT);
396+
return view;
386397
}
387398

388399
/**
389400
* 显示长时自定义吐司
390401
*/
391-
public static void showCustomLong(@NonNull final View view) {
402+
public static View showCustomLong(@NonNull final View view) {
392403
setView(view);
393404
show("", Toast.LENGTH_LONG);
405+
return view;
394406
}
395407

396408
/**
@@ -465,6 +477,15 @@ public static void cancel() {
465477
}
466478
}
467479

480+
/**
481+
* 如果自定义View的Context为Activity级别,需要调用releaseView,否则会内存泄露
482+
*/
483+
static void releaseView() {
484+
if (sToast != null) {
485+
sToast.setView(null);
486+
}
487+
}
488+
468489
private static View getViewFromWR() {
469490
if (sViewWeakReference != null) {
470491
final View view = sViewWeakReference.get();
@@ -474,4 +495,4 @@ private static View getViewFromWR() {
474495
}
475496
return null;
476497
}
477-
}
498+
}

utilcode/src/main/java/com/blankj/utilcode/util/Utils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {
6161
@Override
6262
public void onActivityDestroyed(Activity activity) {
6363
sActivityList.remove(activity);
64-
ToastUtils.cancel();
64+
ToastUtils.releaseView();
6565
}
6666
};
6767

0 commit comments

Comments
 (0)