banner
NEWS LETTER

BaseActivity和BaseFragment

Scroll down

写在前面

抽象出常用方法,方便使用

BaseActivity

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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
public abstract class BaseActivity extends AppCompatActivity {
private LoadingDialog dialog;
private Handler handler = new Handler();
private long lastClickTime;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//设置布局layout
setContentView();
//一些特定的方法,子类都会使用的
ViewUtils.inject(this);
//初始化title
initTitle();
//初始化界面
initView();
//初始化数据
initData();
}

/**
* 初始化数据
*/
protected abstract void initData();

/**
* 初始化界面
*/
protected abstract void initView();

/**
* 初始化title
*/
protected abstract void initTitle();

/**
* 设置布局layout
*/
protected abstract void setContentView();

/**
* 启动activity
*
* @param clazz = activity
*/
protected void startActivity(Class<?> clazz) {
startActivity(new Intent(this, clazz));
}

/**
* @param viewId =R.id.xx
* @param <T> view
* @return findViewById
*/
protected <T extends View> T viewById(int viewId) {
return findViewById(viewId);
}

/**
* 记录dialog 显示创建时间
*/
private long dialogCreateTime;

/**
* 显示加载 dialog
*/
public void showLoadingDialog(String msg) {
boolean isShow = dialog == null || (dialog.getDialog() != null && !dialog.getDialog().isShowing());
if (isShow) {
dialogCreateTime = System.currentTimeMillis();
dialog = new LoadingDialog();
dialog.setLoadingInformation(msg);
dialog.show(getSupportFragmentManager(), "loading_dialog");
}
}

/**
* 显示加载 dialog
*/
public void showLoadingDialog(int msgResId) {
showLoadingDialog(getString(msgResId));
}

/**
* 取消加载dialog
*/
public void dismissLoadingDialog() {
dismissLoadingDialog(null);
}

/**
* 取消加载dialog. 因为延迟, 所以要延时完成之后, 再在 runnable 中执行逻辑.
* <p>
* 延迟关闭时间是因为接口有时返回太快。
*/
public void dismissLoadingDialog(final Runnable runnable) {
if (dialog != null && dialog.getDialog() != null && dialog.getDialog().isShowing()) {
// 由于可能请求接口太快,则导致加载页面一闪问题, 所有再次做判断,
// 如果时间太快(小于 500ms), 则会延时 1s,再做关闭。
if (System.currentTimeMillis() - dialogCreateTime < 10) {
handler.postDelayed(() -> {
if (runnable != null) {
runnable.run();
}
if (dialog != null) {
dialog.dismiss();
dialog = null;
}
}, 500);

} else {
dialog.dismiss();
dialog = null;
if (runnable != null) {
runnable.run();
}
}
}
}

/**
* 为防止多次重复点击
*
* @return false
*/
public synchronized boolean isFastClick() {
long time = System.currentTimeMillis();
if (time - lastClickTime < 500) {
return true;
}
lastClickTime = time;
return false;
}

protected void onClick(View v, int id) {
}

@Override
public void onClick(View v) {
onClick(v, v.getId());
}
}

BaseFragment

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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
public abstract class BaseFragment extends Fragment implements View.OnClickListener {
private LoadingDialog dialog;
private Handler handler = new Handler();
private long lastClickTime;

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
int layoutResId = getLayoutResId();
if (layoutResId <= 0) {
return super.onCreateView(inflater, container, savedInstanceState);
}
View view = inflater.inflate(layoutResId, container, false);
onCreateView();
return view;
}


@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
onInitView(savedInstanceState, getActivity().getIntent());
onInitViewModel();
}

@Override
public void onClick(View v) {
onClick(v, v.getId());
}

/**
* 通过 id 查询当前布局的 View 控件。
*
* @param id 控件 id
* @param <T> 控件类型
* @return 返回对应的控件
*/
public <T extends View> T findView(int id) {
return findView(id, false);
}

/**
* 通过 id 查询当前布局的 View 控件。并是否对此控件设置点击监听
*
* @param id 控件 id
* @param isClick 是否设置点击点击监听
* @param <T> 控件类型
* @return 返回对应的控件
*/
public <T extends View> T findView(int id, boolean isClick) {
View viewById = getView().findViewById(id);
if (isClick) {
viewById.setOnClickListener(this);
}
return (T) viewById;
}

/**
* 通过 id 查询当前布局的 View 控件。并是否对此控件设置点击监听
*
* @param view 当前的布局
* @param id 控件 id
* @param isClick 是否设置点击点击监听
* @param <T> 控件类型
* @return 返回对应的控件
*/
public <T extends View> T findView(View view, int id, boolean isClick) {
View viewById = view.findViewById(id);
if (isClick) {
viewById.setOnClickListener(this);
}
return (T) viewById;
}

public void showToast(String text) {
//toast
ToastUtils.showToast(text);
}

public void showToast(int resId) {
showToast(getString(resId));
}

/**
* 设置布局资源id
*
* @return 设置布局资源id
*/
protected abstract int getLayoutResId();
/**
* 初始化 view
*
* @param savedInstanceState
* @param intent
*/
protected abstract void onInitView(Bundle savedInstanceState, Intent intent);

/**
* 点击监听,当通过{@link #findView(int, boolean)} 方法, 当设置为true , 设置点击监听时, 则会通过此
* 方法进行监听回调。
*
* @param v 监听的 view 控件
* @param id 控件的 ID
*/
protected void onClick(View v, int id) {
}
protected void onInitViewModel() {
}

/**
* 为防止多次重复点击
*
* @return false
*/
public synchronized boolean isFastClick() {
long time = System.currentTimeMillis();
if (time - lastClickTime < 500) {
return true;
}
lastClickTime = time;
return false;
}

private long dialogCreateTime;
/**
* 显示加载 dialog
*
* @param msg
*/
public void showLoadingDialog(String msg) {
FragmentManager fragmentManager = getFragmentManager();
if (fragmentManager == null) {
return;
}
if (dialog == null || (dialog.getDialog() != null && !dialog.getDialog().isShowing())) {
dialogCreateTime = System.currentTimeMillis();
dialog = new LoadingDialog();
dialog.setLoadingInformation(msg);
dialog.show(fragmentManager, "loading_dialog");
}
}

/**
* 显示加载 dialog
*/
public void showLoadingDialog(int msgResId) {
showLoadingDialog(getString(msgResId));
}

/**
* 取消加载dialog
*/
public void dismissLoadingDialog() {
dismissLoadingDialog(null);
}

/**
* 取消加载dialog. 因为延迟, 所以要延时完成之后, 再在 runnable 中执行逻辑.
* <p>
* 延迟关闭时间是因为接口有时返回太快。
*/
public void dismissLoadingDialog(final Runnable runnable) {
if (dialog != null && dialog.getDialog() != null && dialog.getDialog().isShowing()) {
// 由于可能请求接口太快,则导致加载页面一闪问题, 所有再次做判断,
// 如果时间太快(小于 500ms), 则会延时 1s,再做关闭。
if (System.currentTimeMillis() - dialogCreateTime < 10) {
handler.postDelayed(new Runnable() {
@Override
public void run() {
if (runnable != null) {
runnable.run();
}
if (dialog != null) {
dialog.dismissAllowingStateLoss();
dialog = null;
}
}
}, 1000);

} else {
dialog.dismiss();
dialog = null;
if (runnable != null) {
runnable.run();
}
}
}
}

public void onCreateView() {
}

}

创建加载dialog

  • 创建common_dialog_loading.xml
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
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:wheel="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
wheel:cardBackgroundColor="#FFFFFF"
wheel:cardCornerRadius="7dp"
wheel:cardElevation="4dp"
wheel:cardUseCompatPadding="true">

<LinearLayout
android:layout_width="110dp"
android:layout_height="110dp"
android:gravity="center"
android:orientation="vertical">

<ProgressBar
android:layout_width="25dp"
android:layout_height="25dp" />

<TextView
android:id="@+id/common_dialog_tv_information"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#333333"
tools:text="Loading..." />
</LinearLayout>
</androidx.cardview.widget.CardView>

创建LoadingDialog

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
/**
* 简易可设置提示信息的加载对话框
*/
public class LoadingDialog extends DialogFragment {
private TextView contentTv;
private String loadingInfo;

@Override
public void onStart() {
super.onStart();
//透明化背景
Window window = getDialog().getWindow();
//背景色
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
}

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View contentView = inflater.inflate(R.layout.common_dialog_loading, container, false);
contentTv = contentView.findViewById(R.id.common_dialog_tv_information);
if(TextUtils.isEmpty(loadingInfo)){
contentTv.setVisibility(View.GONE);
}else {
contentTv.setText(loadingInfo);
}
setCancelable(false);

Dialog dialog = getDialog();
if(dialog != null){
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
}
return contentView;
}

/**
* 设置加载时提示
*
* @param info
*/
public void setLoadingInformation(String info) {
loadingInfo = info;
if(!TextUtils.isEmpty(loadingInfo) && contentTv != null){
contentTv.setText(info);
contentTv.setVisibility(View.VISIBLE);
}
}

}
其他文章
目录导航 置顶
  1. 1. 写在前面
  2. 2. BaseActivity
  3. 3. BaseFragment
  4. 4. 创建加载dialog
  5. 5. 创建LoadingDialog
请输入关键词进行搜索