在项目中,都会用到listview或gridview等列表控件。一般会用来展示从网络请求的数据 。如果请求的数据为空或者在请求的时候正好无没有网络了,我们的界面应该如何展示呢?数据为空的时候,listview可以使用setemptyview (view emptyview) 方法来我们需要的统一界面。数据加载失败呢?我们也可以统一进行处理。
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
|
//下面这个类是简单地封装用于无数据及加载错误的一个页面。
public class commonshowview {
private context mcontext; // 上下文
private viewgroup memptyorerrorview; // 页面加载无数据或加载错误时显示
private viewgroup mcontentview; // 加载成功时显示的内容
private viewgroup mparentview; // 父布局viewgroup
private layoutinflater minflater;
private textview no_net;
private button load_btn;
private boolean mviewsadded;
private viewgroup.layoutparams mlayoutparams = new viewgroup.layoutparams(layoutparams.match_parent, layoutparams.match_parent);
public final static int type_empty = 1 ; // 数据为空
public final static int type_error = 2 ; // 加载数据失败
public final static int type_content = 3 ; // 直接显示内容
private int mtype = type_empty; // 数据类型,默认是无数据
/**
* 构造方法,传入上下文及内容groupview
*/
public commonshowview(context context, viewgroup mcontentview) {
mcontext = context;
minflater = layoutinflater.from(mcontext);
this .mcontentview = mcontentview;
mparentview = (viewgroup) mcontentview.getparent();
initviews();
}
@suppresslint ( "inflateparams" )
private void initviews() {
memptyorerrorview = (viewgroup) minflater.inflate(r.layout.common_show, null );
no_net = (textview) memptyorerrorview.findviewbyid(r.id.no_net);
load_btn = (button) memptyorerrorview.findviewbyid(r.id.load_btn);
if (!mviewsadded) {
mviewsadded = true ;
mparentview.addview(memptyorerrorview, mlayoutparams);
}
load_btn.setonclicklistener( new onclicklistener() { // 检查网络,进行网络设置
@override
public void onclick(view v) {
intent intent = null ;
// 判断手机系统的版本 即api大于10 就是3.0或以上版本及魅族手机
if (android.os.build.version.sdk_int > 10 && !android.os.build.manufacturer.equals( "meizu" )) {
intent = new intent(android.provider.settings.action_wireless_settings);
}
else if (android.os.build.version.sdk_int > 17 && android.os.build.manufacturer.equals( "meizu" )) {
intent = new intent(android.provider.settings.action_settings);
}
else {
intent = new intent();
componentname component = new componentname( "com.android.settings" , "com.android.settings.wirelesssettings" );
intent.setcomponent(component);
intent.setaction( "android.intent.action.view" );
}
mcontext.startactivity(intent);
}
});
}
public viewgroup getemptyorerrorview() {
return memptyorerrorview;
}
/**
* 设置无数据或加载错误view
* @description:
*/
public void setemptyorerrorview(viewgroup emptyorerrorview) {
this .mparentview.removeview( this .memptyorerrorview);
this .mparentview.addview(emptyorerrorview, mlayoutparams);
this .memptyorerrorview = emptyorerrorview;
}
/**
* 设置无数据或加载错误view
* @description:
*/
public void setemptyorerrorview( int res) {
viewgroup vg = (viewgroup) minflater.inflate(res, null );
this .mparentview.removeview( this .memptyorerrorview);
this .mparentview.addview(vg, mlayoutparams);
this .memptyorerrorview = vg;
}
/**
* 获取内容view
* @description:
*/
public viewgroup getcontextview() {
return mcontentview;
}
/**
* 方法概述:获取内容view的父布局
*/
public viewgroup getrootview() {
return mparentview;
}
/**
* 方法概述:设置展示内容的视图
*/
public void setcontextview(viewgroup contentview) {
this .mtype = type_content;
showbytype(mtype);
}
/**
* 根据类型进行对应展示
* @description:
* @date 2016-2-19 下午3:02:20
*/
public void showbytype( int mtype) {
hideallviews();
if (mcontentview != null ) {
switch (mtype) {
case type_empty:
if (memptyorerrorview != null ) {
memptyorerrorview.setvisibility(view.visible);
no_net.settext( "这里空空也野哦~" );
load_btn.setvisibility(view.gone);
}
break ;
case type_error:
if (memptyorerrorview != null ) {
memptyorerrorview.setvisibility(view.visible);
no_net.settext( "网络加载失败哦~" );
load_btn.setvisibility(view.visible);
}
break ;
case type_content:
if (mcontentview != null ) {
mcontentview.setvisibility(view.visible);
}
break ;
default :
break ;
}
}
}
/**
* 方法概述:将所有的子view隐藏起来
*/
private void hideallviews() {
if (mparentview != null ) {
if (mparentview.getchildcount() > 0 ) {
for ( int i = 0 ; i < mparentview.getchildcount(); i++) {
view childview = mparentview.getchildat(i);
if (childview != null ) {
childview.setvisibility(view.gone);
}
}
}
}
}
}
|
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
|
//错误页面及空页面时布局
<?xml version= "1.0" encoding= "utf-8" ?>
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"
android:id= "@+id/rel_null"
android:layout_width= "match_parent"
android:layout_height= "match_parent" >
<textview
android:id= "@+id/no_net"
android:layout_width= "match_parent"
android:layout_height= "wrap_content"
android:layout_centerhorizontal= "true"
android:layout_centerinparent= "true"
android:gravity= "center"
android:text= "好像没网哦~"
android:textcolor= "#333333"
android:textsize= "16sp" />
<button
android:id= "@+id/load_btn"
android:layout_width= "112dp"
android:layout_height= "40dp"
android:layout_below= "@+id/no_net"
android:layout_centerhorizontal= "true"
android:layout_margintop= "15dp"
android:background= "#e12228"
android:gravity= "center"
android:text= "检查网络"
android:textcolor= "#ffffff"
android:textsize= "14sp" />
</relativelayout>
|
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
|
//在activity中使用
public class mainactivity extends activity {
private listview listview;
private commonshowview mshowview;
private list<string> datas;
@override
protected void oncreate(bundle savedinstancestate) {
super .oncreate(savedinstancestate);
setcontentview(r.layout.activity_main);
listview = (listview) findviewbyid(r.id.listview);
mshowview = new commonshowview( this , listview);
// ----------有数据的时情况下--------------
// datas = getdatas(true);
// listview.setadapter(new myadapter(this, datas));
// mshowview.setcontextview(listview);//显示数据,也可以不调用,直接显示
// --------无数据的情况下----------------
// datas = getdatas(false);//无数据
// mshowview.showbytype(commonshowview.type_empty);
// ---------数据加载失败的情况下---------------
mshowview.showbytype(commonshowview.type_error);
}
private list<string> getdatas( boolean flag) {
list<string> datas = new arraylist<>();
datas.add( "这是第一行的data" );
datas.add( "这是第2行的data" );
datas.add( "这是第三行的data" );
datas.add( "这是第4行的data" );
if (flag) {
return datas;
}
else {
return null ;
}
}
class myadapter extends baseadapter {
private list<string> datas;
private context mcontext;
public myadapter(context mcontext, list<string> datas) {
this .mcontext = mcontext;
this .datas = datas;
}
@override
public int getcount() {
// todo auto-generated method stub
return datas.size();
}
@override
public object getitem( int position) {
// todo auto-generated method stub
return datas.get(position);
}
@override
public long getitemid( int position) {
// todo auto-generated method stub
return position;
}
@override
public view getview( int position, view convertview, viewgroup parent) {
// 这里只举个小例子,没有考虑性能 优化
textview tv = new textview(mcontext);
tv.settextsize(16f);
tv.settext(datas.get(position));
return tv;
}
}
}
|
目前只是做了个超级简单的样式,具体的展示根据需求再改再封装。帖个图:
以上就是listview数据为空及加载错误处理的方法,希望对大家的学习有所帮助。