/
MultipleStatusView.java
536 lines (480 loc) · 16.8 KB
/
MultipleStatusView.java
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
package com.classic.common;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.support.v4.app.Fragment;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import java.util.ArrayList;
/**
* 类描述: 一个方便在多种状态切换的view
* <p>
* 创建人: 续写经典
* 创建时间: 2016/1/15 10:20.
*/
@SuppressWarnings("unused")
public class MultipleStatusView extends RelativeLayout {
private static final String TAG = "MultipleStatusView";
private static final RelativeLayout.LayoutParams DEFAULT_LAYOUT_PARAMS =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
public static final int STATUS_CONTENT = 0x00;
public static final int STATUS_LOADING = 0x01;
public static final int STATUS_EMPTY = 0x02;
public static final int STATUS_ERROR = 0x03;
public static final int STATUS_NO_NETWORK = 0x04;
private static final int NULL_RESOURCE_ID = -1;
private View mEmptyView;
private View mErrorView;
private View mLoadingView;
private View mNoNetworkView;
private View mContentView;
private int mEmptyViewResId;
private int mErrorViewResId;
private int mLoadingViewResId;
private int mNoNetworkViewResId;
private int mContentViewResId;
private int mViewStatus = -1;
private final LayoutInflater mInflater;
private OnClickListener mOnRetryClickListener;
private OnViewStatusChangeListener mViewStatusListener;
private final ArrayList<Integer> mOtherIds = new ArrayList<>();
public MultipleStatusView(Context context) {
this(context, null);
}
public MultipleStatusView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public MultipleStatusView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MultipleStatusView, defStyleAttr, 0);
mEmptyViewResId = a.getResourceId(R.styleable.MultipleStatusView_emptyView, R.layout.empty_view);
mErrorViewResId = a.getResourceId(R.styleable.MultipleStatusView_errorView, R.layout.error_view);
mLoadingViewResId = a.getResourceId(R.styleable.MultipleStatusView_loadingView, R.layout.loading_view);
mNoNetworkViewResId = a.getResourceId(R.styleable.MultipleStatusView_noNetworkView, R.layout.no_network_view);
mContentViewResId = a.getResourceId(R.styleable.MultipleStatusView_contentView, NULL_RESOURCE_ID);
a.recycle();
mInflater = LayoutInflater.from(getContext());
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
showContent();
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
clear(mEmptyView, mLoadingView, mErrorView, mNoNetworkView);
if (!mOtherIds.isEmpty()) {
mOtherIds.clear();
}
if (null != mOnRetryClickListener) {
mOnRetryClickListener = null;
}
if (null != mViewStatusListener) {
mViewStatusListener = null;
}
}
/**
* 获取当前状态
*
* @return 视图状态
*/
public int getViewStatus() {
return mViewStatus;
}
/**
* 设置重试点击事件
*
* @param onRetryClickListener 重试点击事件
*/
public void setOnRetryClickListener(OnClickListener onRetryClickListener) {
this.mOnRetryClickListener = onRetryClickListener;
}
/**
* 显示空视图
*/
public final void showEmpty() {
showEmpty(mEmptyViewResId, DEFAULT_LAYOUT_PARAMS);
}
/**
* 显示空视图
*
* @param hintResId 自定义提示文本内容
* @param formatArgs 占位符参数
*/
public final void showEmpty(int hintResId, Object... formatArgs) {
showEmpty();
setStatusHintContent(mEmptyView, hintResId, formatArgs);
}
/**
* 显示空视图
*
* @param hint 自定义提示文本内容
*/
public final void showEmpty(String hint) {
showEmpty();
setStatusHintContent(mEmptyView, hint);
}
/**
* 显示空视图
*
* @param layoutId 自定义布局文件
* @param layoutParams 布局参数
*/
public final void showEmpty(int layoutId, ViewGroup.LayoutParams layoutParams) {
showEmpty(null == mEmptyView ? inflateView(layoutId) : mEmptyView, layoutParams);
}
/**
* 显示空视图
*
* @param view 自定义视图
* @param layoutParams 布局参数
*/
public final void showEmpty(View view, ViewGroup.LayoutParams layoutParams) {
checkNull(view, "Empty view is null.");
checkNull(layoutParams, "Layout params is null.");
changeViewStatus(STATUS_EMPTY);
if (null == mEmptyView) {
mEmptyView = view;
View emptyRetryView = mEmptyView.findViewById(R.id.empty_retry_view);
if (null != mOnRetryClickListener && null != emptyRetryView) {
emptyRetryView.setOnClickListener(mOnRetryClickListener);
}
mOtherIds.add(mEmptyView.getId());
addView(mEmptyView, 0, layoutParams);
}
showViewById(mEmptyView.getId());
}
/**
* 显示错误视图
*/
public final void showError() {
showError(mErrorViewResId, DEFAULT_LAYOUT_PARAMS);
}
/**
* 显示错误视图
*
* @param hintResId 自定义提示文本内容
* @param formatArgs 占位符参数
*/
public final void showError(int hintResId, Object... formatArgs) {
showError();
setStatusHintContent(mErrorView, hintResId, formatArgs);
}
/**
* 显示错误视图
*
* @param hint 自定义提示文本内容
*/
public final void showError(String hint) {
showError();
setStatusHintContent(mErrorView, hint);
}
/**
* 显示错误视图
*
* @param layoutId 自定义布局文件
* @param layoutParams 布局参数
*/
public final void showError(int layoutId, ViewGroup.LayoutParams layoutParams) {
showError(null == mErrorView ? inflateView(layoutId) : mErrorView, layoutParams);
}
/**
* 显示错误视图
*
* @param view 自定义视图
* @param layoutParams 布局参数
*/
public final void showError(View view, ViewGroup.LayoutParams layoutParams) {
checkNull(view, "Error view is null.");
checkNull(layoutParams, "Layout params is null.");
changeViewStatus(STATUS_ERROR);
if (null == mErrorView) {
mErrorView = view;
View errorRetryView = mErrorView.findViewById(R.id.error_retry_view);
if (null != mOnRetryClickListener && null != errorRetryView) {
errorRetryView.setOnClickListener(mOnRetryClickListener);
}
mOtherIds.add(mErrorView.getId());
addView(mErrorView, 0, layoutParams);
}
showViewById(mErrorView.getId());
}
/**
* 显示加载中视图
*/
public final void showLoading() {
showLoading(mLoadingViewResId, DEFAULT_LAYOUT_PARAMS);
}
/**
* 显示加载中视图
*
* @param hintResId 自定义提示文本内容
* @param formatArgs 占位符参数
*/
public final void showLoading(int hintResId, Object... formatArgs) {
showLoading();
setStatusHintContent(mLoadingView, hintResId, formatArgs);
}
/**
* 显示加载中视图
*
* @param hint 自定义提示文本内容
*/
public final void showLoading(String hint) {
showLoading();
setStatusHintContent(mLoadingView, hint);
}
/**
* 显示加载中视图
*
* @param layoutId 自定义布局文件
* @param layoutParams 布局参数
*/
public final void showLoading(int layoutId, ViewGroup.LayoutParams layoutParams) {
showLoading(null == mLoadingView ? inflateView(layoutId) : mLoadingView, layoutParams);
}
/**
* 显示加载中视图
*
* @param view 自定义视图
* @param layoutParams 布局参数
*/
public final void showLoading(View view, ViewGroup.LayoutParams layoutParams) {
checkNull(view, "Loading view is null.");
checkNull(layoutParams, "Layout params is null.");
changeViewStatus(STATUS_LOADING);
if (null == mLoadingView) {
mLoadingView = view;
mOtherIds.add(mLoadingView.getId());
addView(mLoadingView, 0, layoutParams);
}
showViewById(mLoadingView.getId());
}
/**
* 显示无网络视图
*/
public final void showNoNetwork() {
showNoNetwork(mNoNetworkViewResId, DEFAULT_LAYOUT_PARAMS);
}
/**
* 显示无网络视图
*
* @param hintResId 自定义提示文本内容
* @param formatArgs 占位符参数
*/
public final void showNoNetwork(int hintResId, Object... formatArgs) {
showNoNetwork();
setStatusHintContent(mNoNetworkView, hintResId, formatArgs);
}
/**
* 显示无网络视图
*
* @param hint 自定义提示文本内容
*/
public final void showNoNetwork(String hint) {
showNoNetwork();
setStatusHintContent(mNoNetworkView, hint);
}
/**
* 显示无网络视图
*
* @param layoutId 自定义布局文件
* @param layoutParams 布局参数
*/
public final void showNoNetwork(int layoutId, ViewGroup.LayoutParams layoutParams) {
showNoNetwork(null == mNoNetworkView ? inflateView(layoutId) : mNoNetworkView, layoutParams);
}
/**
* 显示无网络视图
*
* @param view 自定义视图
* @param layoutParams 布局参数
*/
public final void showNoNetwork(View view, ViewGroup.LayoutParams layoutParams) {
checkNull(view, "No network view is null.");
checkNull(layoutParams, "Layout params is null.");
changeViewStatus(STATUS_NO_NETWORK);
if (null == mNoNetworkView) {
mNoNetworkView = view;
View noNetworkRetryView = mNoNetworkView.findViewById(R.id.no_network_retry_view);
if (null != mOnRetryClickListener && null != noNetworkRetryView) {
noNetworkRetryView.setOnClickListener(mOnRetryClickListener);
}
mOtherIds.add(mNoNetworkView.getId());
addView(mNoNetworkView, 0, layoutParams);
}
showViewById(mNoNetworkView.getId());
}
/**
* 显示内容视图
*/
public final void showContent() {
changeViewStatus(STATUS_CONTENT);
if (null == mContentView && mContentViewResId != NULL_RESOURCE_ID) {
mContentView = mInflater.inflate(mContentViewResId, null);
addView(mContentView, 0, DEFAULT_LAYOUT_PARAMS);
}
showContentView();
}
/**
* 显示内容视图
*
* @param layoutId 自定义布局文件
* @param layoutParams 布局参数
*/
public final void showContent(int layoutId, ViewGroup.LayoutParams layoutParams) {
showContent(inflateView(layoutId), layoutParams);
}
/**
* 显示内容视图
*
* @param view 自定义视图
* @param layoutParams 布局参数
*/
public final void showContent(View view, ViewGroup.LayoutParams layoutParams) {
checkNull(view, "Content view is null.");
checkNull(layoutParams, "Layout params is null.");
changeViewStatus(STATUS_CONTENT);
clear(mContentView);
mContentView = view;
addView(mContentView, 0, layoutParams);
showViewById(mContentView.getId());
}
private void setStatusHintContent(View view, int resId, Object... formatArgs) {
checkNull(view, "Target view is null.");
setStatusHintContent(view, view.getContext().getString(resId, formatArgs));
}
private void setStatusHintContent(View view, String hint) {
checkNull(view, "Target view is null.");
TextView hintView = view.findViewById(R.id.status_hint_content);
if (null != hintView) {
hintView.setText(hint);
} else {
throw new NullPointerException("Not find the view ID `status_hint_content`");
}
}
private View inflateView(int layoutId) {
return mInflater.inflate(layoutId, null);
}
private void showViewById(int viewId) {
final int childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
View view = getChildAt(i);
view.setVisibility(view.getId() == viewId ? View.VISIBLE : View.GONE);
}
}
private void showContentView() {
final int childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
View view = getChildAt(i);
view.setVisibility(mOtherIds.contains(view.getId()) ? View.GONE : View.VISIBLE);
}
}
private void checkNull(Object object, String hint) {
if (null == object) {
throw new NullPointerException(hint);
}
}
private void clear(View... views) {
if (null == views) {
return;
}
try {
for (View view : views) {
if (null != view) {
removeView(view);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 视图状态改变接口
*/
public interface OnViewStatusChangeListener {
/**
* 视图状态改变时回调
*
* @param oldViewStatus 之前的视图状态
* @param newViewStatus 新的视图状态
*/
void onChange(int oldViewStatus, int newViewStatus);
}
/**
* 设置视图状态改变监听事件
*
* @param onViewStatusChangeListener 视图状态改变监听事件
*/
public void setOnViewStatusChangeListener(OnViewStatusChangeListener onViewStatusChangeListener) {
this.mViewStatusListener = onViewStatusChangeListener;
}
/**
* 改变视图状态
*
* @param newViewStatus 新的视图状态
*/
private void changeViewStatus(int newViewStatus) {
if (mViewStatus == newViewStatus) {
return;
}
if (null != mViewStatusListener) {
mViewStatusListener.onChange(mViewStatus, newViewStatus);
}
mViewStatus = newViewStatus;
}
private void setContentViewResId(int contentViewResId) {
this.mContentViewResId = contentViewResId;
this.mContentView = mInflater.inflate(mContentViewResId, null);
addView(mContentView, 0, DEFAULT_LAYOUT_PARAMS);
}
private void setContentView(ViewGroup contentView) {
this.mContentView = contentView;
addView(mContentView, 0, DEFAULT_LAYOUT_PARAMS);
}
public static MultipleStatusView attach(Fragment fragment, int rootAnchor) {
if (null == fragment || fragment.getView() == null) {
throw new IllegalArgumentException("fragment is null or fragment.getView is null");
}
if (-1 != rootAnchor) {
ViewGroup contentAnchor = fragment.getView().findViewById(rootAnchor);
if (null != contentAnchor) {
attach(contentAnchor);
}
}
ViewGroup contentParent = (ViewGroup) fragment.getView().getParent();
return attach(contentParent);
}
public static MultipleStatusView attach(Activity activity, int rootAnchor) {
if (-1 != rootAnchor) {
ViewGroup contentAnchor = activity.findViewById(rootAnchor);
if (null != contentAnchor) {
attach(contentAnchor);
}
}
ViewGroup defaultAnchor = activity.findViewById(android.R.id.content);
return attach(defaultAnchor);
}
public static MultipleStatusView attach(ViewGroup rootAnchor) {
if (null == rootAnchor) {
throw new IllegalArgumentException("root Anchor View can't be null");
}
ViewGroup parent = (ViewGroup) rootAnchor.getParent();
int anchorIndex = parent.indexOfChild(rootAnchor);
if (-1 != anchorIndex) {
parent.removeView(rootAnchor);
MultipleStatusView statusView = new MultipleStatusView(rootAnchor.getContext());
statusView.setContentView(rootAnchor);
ViewGroup.LayoutParams p = rootAnchor.getLayoutParams();
parent.addView(statusView, anchorIndex, p);
return statusView;
}
return null;
}
}