DefaultView.java
17.7 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
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
package com.wd.common.widget;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import com.wd.foundation.wdkit.constant.DefaultViewConstant;
import com.wd.common.viewclick.BaseClickListener;
import com.wd.fastcoding.base.R;
import com.wd.foundation.wdkitcore.tools.ResUtils;
/**
* 缺省页
*
* @author baozhaoxin
* @version [V1.0.0, 2023/3/31]
* @since V1.0.0
*/
public class DefaultView extends LinearLayout {
/**
* 根view
*/
private View rootView;
/**
* View 距离top高度
*/
private View topView, btmView;
/**
* 根布局
*/
private LinearLayout defaultRootLayout;
/**
* 内容布局
*/
private LinearLayout defaultLayout;
/**
* 图标
*/
private ImageView defaultImg;
private ConstraintLayout topMenuView;
/**
* 文案
*/
private TextView defaultTv;
/**
* 重试按钮
*/
private TextView defaultBtn;
/**
* 上下文环境
*/
private Context mContext;
/**
* 1.页面接口报错,用于列表类及其他页面(带重试按钮):Loading failed, please try again!
* 2.内容获取失败,用于内容详情页(带重试按钮):An error occurred. Please try again later.
* 3.暂无网络(带重试按钮):网络出小差了,请检查网络后重试
* 4.暂无内容:No content
* 5.暂无收藏内容:You haven't collected anything yet. Go to the home page.
* 6.暂无消息:No records
* 7.暂无浏览历史:No records
* 8.暂无搜索结果:No content found
*/
private int mType = -1;//DefaultViewConstant.TYPE_GET_CONTENT_ERROR;
/**
* 重试按钮点击监听
*/
private RetryClickListener retryClickListener;
/**
* 缺省页面布局,true:用带NestedScrollView布局;false:不带带NestedScrollView布局
*/
private boolean nestedScrollView = false;
public DefaultView(Context context) {
this(context, null);
}
public DefaultView(Context context, boolean nestedScrollView) {
super(context);
this.nestedScrollView = nestedScrollView;
initView(context);
}
public DefaultView(Context context, @Nullable @org.jetbrains.annotations.Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public DefaultView(Context context, @Nullable @org.jetbrains.annotations.Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
//获取定义的一些属性
TypedArray styleAttrs = context.getTheme().obtainStyledAttributes(attrs, R.styleable.DefaultView, defStyleAttr, 0);
//数一数有多少个属性呢
int indexCount = styleAttrs.getIndexCount();
// 循环遍历的方式,找到我们所定义的一些属性
for (int i = 0; i < indexCount; i++) {
//属性的索引值
int attrIndex = styleAttrs.getIndex(i);
//根据索引值给java代码中的成员变量赋值
if (attrIndex == R.styleable.DefaultView_default_view_type) {
mType = styleAttrs.getInteger(attrIndex, -1);
}
}
//资源文件中的属性回收
styleAttrs.recycle();
initView(context);
}
/**
* 初始化view
*
* @param context
*/
private void initView(Context context) {
mContext = context;
if (rootView == null) {
if (nestedScrollView) {
rootView = LayoutInflater.from(context).inflate(R.layout.default_layout_nested_sc, this);
} else {
rootView = LayoutInflater.from(context).inflate(R.layout.default_layout, this);
}
}
if (rootView != null) {
defaultRootLayout = rootView.findViewById(R.id.default_root_layout);
topView = rootView.findViewById(R.id.top_View);
btmView = rootView.findViewById(R.id.btmspaceview);
defaultLayout = rootView.findViewById(R.id.default_content_layout);
defaultImg = rootView.findViewById(R.id.default_img);
topMenuView = rootView.findViewById(R.id.layout_follow_more_creator);
defaultTv = rootView.findViewById(R.id.default_tv);
defaultBtn = rootView.findViewById(R.id.default_btn);
defaultBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (retryClickListener != null) {
retryClickListener.onRetryClick();
}
}
});
}
// 显示默认缺省页面
show(mType);
//defaultRootLayout.setVisibility(GONE);
}
public void show(int type) {
show(type, false, false);
}
/**
* 展示布局,同时设置顶部底部比例
*/
public void showWithWeight(int type, int topWeight, int btmWeight) {
setTopBtmWeight(topWeight, btmWeight);
show(type, false, false);
}
/**
* 展示布局,设置top和btm距离,适合容器是wrap_parent
*/
public void setTopBtmHeight(int type, int topWeight, int btmWeight) {
setTopBtmHeight(topWeight, btmWeight);
show(type, false, false);
}
/**
* 显示缺省页
*
* @param type 1.页面接口报错,用于列表类及其他页面(带重试按钮):Loading failed, please try again!
* 2.内容获取失败,用于内容详情页(带重试按钮):An error occurred. Please try again later.
* 3.暂无网络(带重试按钮):网络出小差了,请检查网络后重试
* 4.暂无内容:No content
* 5.暂无收藏内容:You haven't collected anything yet. Go to the home page.
* 6.暂无消息:No records
* 7.暂无浏览历史:No records
* 8.暂无搜索结果:No content found
* 9.确认提交
* 10.直播结束
* 11.没有找到相关内容(2023/11/6 新增 )DefaultViewConstant.TYPE_NO_RELEVANT_CONTENT_FOUND
* @param hasTry true带重试按钮、false不带重试按钮
* @param isDark true黑模式、false正常模式
*/
public void show(int type, boolean hasTry, boolean isDark) {
if (type == -1) {
return;
}
mType = type;
if (isDark) {
//黑色样式
setDarkMode();
}
//点击重试按钮
defaultBtn.setVisibility(hasTry ? VISIBLE : GONE);
// topMenuView.setVisibility(GONE);
/*if (type == DefaultViewConstant.TYPE_GET_CONTENT_FAILED) {
//内容获取失败,用于内容详情页
defaultImg.setBackgroundResource(R.mipmap.icon_default_get_content_failed);
defaultTv.setText(ResUtils.getString(R.string.default_get_content_failed));
} else if (type == DefaultViewConstant.TYPE_GET_CONTENT_ERROR) {
//内容获取失败,用于内容详情页
defaultImg.setBackgroundResource(R.mipmap.icon_default_get_content_failed);
defaultTv.setText(ResUtils.getString(R.string.default_get_content_failed));
defaultBtn.setVisibility(VISIBLE);
} else if (type == DefaultViewConstant.TYPE_NO_NETWORK) {
//暂无网络
defaultImg.setBackgroundResource(R.mipmap.icon_default_no_network);
defaultTv.setText(ResUtils.getString(R.string.default_no_network));
defaultBtn.setVisibility(VISIBLE);
} else if (type == DefaultViewConstant.TYPE_NO_COLLECTED) {
//暂无收藏
defaultImg.setBackgroundResource(R.mipmap.icon_default_no_collected);
defaultTv.setText(ResUtils.getString(R.string.no_collection));
} else if (type == DefaultViewConstant.TYPE_NO_RECORDS_MESSAGE) {
//暂无消息
defaultImg.setBackgroundResource(R.mipmap.icon_default_no_records_message);
defaultTv.setText(ResUtils.getString(R.string.default_no_records));
} else if (type == DefaultViewConstant.TYPE_NO_RECORDS_BROWSE) {
//暂无浏览历史
defaultImg.setBackgroundResource(R.mipmap.icon_default_noworkorreservation);
defaultTv.setText(ResUtils.getString(R.string.no_browsing_history));
} else if (type == DefaultViewConstant.TYPE_NO_CONTENT_FOUND) {
//没有找到相关内容(搜索)
defaultImg.setBackgroundResource(R.mipmap.icon_default_no_search_result);
defaultTv.setText(ResUtils.getString(R.string.default_no_content_found));
} else if (type == DefaultViewConstant.TYPE_LIVE_END) {
//直播结束
defaultImg.setBackgroundResource(R.mipmap.icon_default_live_end);
defaultTv.setText(ResUtils.getString(R.string.tips_end_of_live_broadcast));
} else if (type == DefaultViewConstant.TYPE_NO_COMMENT) {
//暂无评论
defaultImg.setBackgroundResource(R.mipmap.icon_default_nocomment);
defaultTv.setText(ResUtils.getString(R.string.nocomment));
} else if (type == DefaultViewConstant.TYPE_NO_WORKS || type == DefaultViewConstant.TYPE_NO_RESERVATION
|| type == DefaultViewConstant.TYPE_NO_CONTENT) {
//暂无预约、暂无作品、暂无内容
defaultImg.setBackgroundResource(R.mipmap.icon_default_noworkorreservation);
if (type == DefaultViewConstant.TYPE_NO_WORKS) {
defaultTv.setText(ResUtils.getString(R.string.noworks));
} else if (type == DefaultViewConstant.TYPE_NO_RESERVATION) {
defaultTv.setText(ResUtils.getString(R.string.noreservation));
} else if (type == DefaultViewConstant.TYPE_NO_CONTENT) {
defaultTv.setText(ResUtils.getString(R.string.default_no_content));
}
}else if (type == DefaultViewConstant.TYPE_NO_CONTENT_FOR_DETAIL){
//内容找不到了
defaultImg.setBackgroundResource(R.mipmap.icon_default_get_content_failed);
defaultTv.setText(ResUtils.getString(R.string.res_content_no_found));
}else if (type == DefaultViewConstant.TYPE_NO_CONTENT_FOR_MY_LEAVE_WORD){
//我的留言为空,暂无留言
defaultImg.setBackgroundResource(R.mipmap.ic_no_works_yet);
defaultTv.setText(ResUtils.getString(R.string.no_leave_word_yet));
} else if (type == DefaultViewConstant.TYPE_CURRENT_LIMITING) {
//限流
defaultImg.setBackgroundResource(R.mipmap.icon_currentlimiting);
defaultTv.setText(ResUtils.getString(R.string.currentlimiting) + "(10s)");
} else if (type == DefaultViewConstant.TYPE_NO_PERMISSION) {
//该号主暂时无法访问
defaultImg.setBackgroundResource(R.mipmap.icon_no_permission);
defaultTv.setText(ResUtils.getString(R.string.res_user_creator_can_not_open));
} else if (type == DefaultViewConstant.TYPE_ANCHOR_LEAVING) {
//主播暂时离开,马上回来
defaultImg.setBackgroundResource(R.mipmap.icon_anchor_leaving);
defaultTv.setText(ResUtils.getString(R.string.anchor_leaving));
} else if (type == DefaultViewConstant.TYPE_NO_RELEVANT_CONTENT_FOUND) {
//没有找到相关内容
defaultImg.setBackgroundResource(R.mipmap.icon_no_relevant_content_found);
defaultTv.setText(ResUtils.getString(R.string.no_relevant_content_found));
defaultImg.getLayoutParams().height = (int) mContext.getResources().getDimension(R.dimen.rmrb_dp139);
defaultImg.getLayoutParams().width = (int) mContext.getResources().getDimension(R.dimen.rmrb_dp200);
} else if (type == DefaultViewConstant.TYPE_PERSONAL_CENTER_WORKS) {
// 号主页-暂无作品
defaultImg.setBackgroundResource(R.mipmap.ic_no_works_yet);
defaultTv.setText(ResUtils.getString(R.string.no_works_yet));
} else if (type == DefaultViewConstant.TYPE_PERSONAL_CENTER_COMMENT) {
// 号主页-暂无评论
defaultImg.setBackgroundResource(R.mipmap.icon_default_nocomment);
defaultTv.setText(ResUtils.getString(R.string.no_comment_yet));
} else if (type == DefaultViewConstant.TYPE_PERSONAL_CENTER_FOCUS) {
// 号主页-暂无关注
defaultImg.setBackgroundResource(R.mipmap.icon_default_noworkorreservation);
defaultTv.setText(ResUtils.getString(R.string.no_focus_yet));
} else if (type == DefaultViewConstant.TYPE_PERSONAL_CENTER_FOCUS_TOP_VIEW) {
defaultImg.setBackgroundResource(R.mipmap.icon_default_noworkorreservation);
defaultTv.setText(ResUtils.getString(R.string.no_focus_yet));
setTopMenuViewShow();
} else if (type == DefaultViewConstant.TYPE_GET_CONTENT_FAILED_VIDEO) {
// 内容获取失败,用于视频详情页(带重试按钮)
defaultImg.setBackgroundResource(R.mipmap.icon_default_get_content_failed);
defaultTv.setText(ResUtils.getString(R.string.default_get_content_failed));
defaultBtn.setTextColor(ResUtils.getColor(R.color.res_color_common_C5));
}else if (type == DefaultViewConstant.TYPE_DRAFTS_NO_CONTENT) {
// 草稿箱-暂无内容
defaultImg.setBackgroundResource(R.mipmap.ic_no_works_yet);
defaultTv.setText(ResUtils.getString(R.string.default_no_content));
} else {
//页面接口报错,用于列表类及其他页面(带重试按钮)
defaultImg.setBackgroundResource(R.mipmap.icon_default_get_content_failed);
defaultTv.setText(ResUtils.getString(R.string.default_get_content_failed));
}
defaultRootLayout.setVisibility(VISIBLE);*/
}
/**
* 隐藏缺省页
*/
public void hide() {
defaultRootLayout.setVisibility(GONE);
}
public int getmType() {
return mType;
}
public void setmType(int mType) {
this.mType = mType;
}
/**
* 设置暗模式
*/
public void setDarkMode() {
//黑色
defaultRootLayout.setBackgroundColor(0xff000000);
defaultTv.setTextColor(0xffB0B0B0);
defaultBtn.setTextColor(0xffCCCCCC);
defaultBtn.setBackground(ResUtils.getDrawable(R.drawable.bg_text_networkerror_reload_dark));
}
/**
* 设置topView 屏幕比重
*/
public void setTopViewWeight(int weight) {
LayoutParams params = (LayoutParams) topView.getLayoutParams();
params.weight = weight;
topView.setLayoutParams(params);
}
/**
* 在ColumnFragment上设置背景色
*/
public void setColumnFragmentBackgroundColor() {
defaultRootLayout.setBackgroundResource(R.drawable.shape_square_page_top_radius);
LayoutParams defaultViewLp = (LayoutParams) defaultRootLayout.getLayoutParams();
defaultViewLp.rightMargin = (int) mContext.getResources().getDimension(R.dimen.rmrb_dp6);
defaultViewLp.leftMargin = (int) mContext.getResources().getDimension(R.dimen.rmrb_dp6);
defaultRootLayout.setLayoutParams(defaultViewLp);
}
/**
* 设置重试按钮点击监听
*
* @param retryClickListener
*/
public void setRetryBtnClickListener(RetryClickListener retryClickListener) {
this.retryClickListener = retryClickListener;
}
public interface RetryClickListener {
void onRetryClick();
}
/**
* 设置顶部、底部比例
*/
public void setTopBtmWeight(int topWeight, int btmWeight) {
if (topView != null) {
//设置顶部比例
LayoutParams topParams = (LayoutParams) topView.getLayoutParams();
topParams.weight = topWeight;
topView.setLayoutParams(topParams);
}
if (btmView != null) {
//设置底部比例
LayoutParams btmParams = (LayoutParams) btmView.getLayoutParams();
btmParams.weight = btmWeight;
btmView.setLayoutParams(btmParams);
}
}
public void showOnlySpace(int type) {
if (btmView != null) {
btmView.setVisibility(GONE);
}
show(type);
}
/**
* 设置顶部、底部高度
*/
public void setTopBtmHeight(int topWeight, int btmWeight) {
if (topView != null) {
//设置顶部比例
LayoutParams topParams = (LayoutParams) topView.getLayoutParams();
topParams.height = topWeight;
topParams.weight = 0;
topView.setLayoutParams(topParams);
}
if (btmView != null) {
//设置底部比例
LayoutParams btmParams = (LayoutParams) btmView.getLayoutParams();
btmParams.height = btmWeight;
btmParams.weight = 0;
btmView.setLayoutParams(btmParams);
}
}
public void setTipTextStyle(int color, float textsize) {
if (defaultTv != null) {
defaultTv.setTextColor(color);
defaultTv.setTextSize(TypedValue.COMPLEX_UNIT_DIP, textsize);
}
}
public void setTopMenuViewShow() {
if (topMenuView != null) {
topMenuView.setVisibility(VISIBLE);
}
}
public void setTopMenuViewClickListen(BaseClickListener clickListen) {
if (topMenuView != null) {
topMenuView.setOnClickListener(clickListen);
}
}
}