LoadingDialogUtils.java
4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package com.xdy.ui.loading;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface.OnDismissListener;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.xdy.ui.R;
import com.pitt.loadingview.library.LoadingView;
import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
* @author 蒋洪波
* @file LoadingDialogUtils.java
* @brief 获得加载进行中的工具类
* @date 2017-6-10
* Copyright (c) 2017
* All rights reserved.
*/
public class LoadingDialogUtils {
/**
* 这里使用强引用是因为我们必须通过他找到关联的view,最后一定要保证从该MAP中都被移除掉,否则还会泄漏
*/
private static HashMap<Context, List<WeakReference<View>>> loadingViewMap = new HashMap<>();
public static void clearAllContext() {
Iterator<Map.Entry<Context, List<WeakReference<View>>>> iterator = loadingViewMap.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<Context, List<WeakReference<View>>> next = iterator.next();
List<WeakReference<View>> value = next.getValue();
for (WeakReference<View> avLoadingIndicatorView : value) {
View loadingIndicatorView = avLoadingIndicatorView.get();
// if (loadingIndicatorView != null)
// loadingIndicatorView.clearContext();
}
}
loadingViewMap.clear();
}
public static void clearContext(Context ctx) {
List<WeakReference<View>> avLoadingIndicatorViews = loadingViewMap.get(ctx);
if (avLoadingIndicatorViews != null) {
for (WeakReference<View> avLoadingIndicatorView : avLoadingIndicatorViews) {
View loadingIndicatorView = avLoadingIndicatorView.get();
// if (loadingIndicatorView != null)
// loadingIndicatorView.clearContext();
}
loadingViewMap.remove(ctx);
}
}
/**
* @param context
* @param msg
* @return
* @brief 等待对话框
*/
public static Dialog createLoadingDialog(Context context, String msg) {
return createLoadingDialog(context, msg, null);
}
/**
* @param listener 消失回调
* @param context
* @param msg
* @return
* @brief 一个有消失回调的等待对话框
*/
public static Dialog createLoadingDialog(Context context, String msg, OnDismissListener listener) {
if (context == null)
return null;
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.ui_dialog_loadingview, null);// 得到加载view
LinearLayout layout = (LinearLayout) v.findViewById(R.id.dialog_view);// 加载布局
TextView tipTextView = (TextView) v.findViewById(R.id.tipTextView);// 提示文字
final LoadingView loadingView = (LoadingView) v.findViewById(R.id.loading_view);// 提示文字
if (!TextUtils.isEmpty(msg)) {
tipTextView.setText(msg);// 设置加载信息
tipTextView.setVisibility(View.VISIBLE);
}
// List<WeakReference<View>> list = null;
// if ((list = loadingViewMap.get(context)) == null) {
// list = new ArrayList<>();
// loadingViewMap.put(context, list);
// }
// list.add(new WeakReference<>(loadingView));
Dialog loadingDialog = new Dialog(context, R.style.ui_loading_style);// 创建自定义样式dialog
// 设置取消监听
if (listener != null)
loadingDialog.setOnDismissListener(listener);
loadingDialog.setCanceledOnTouchOutside(false);// 设置外部不可点击
loadingDialog.setCancelable(true);// 不可以用“返回键”取消
loadingDialog.setContentView(layout, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT));// 设置布局
return loadingDialog;
}
}