本文实例讲述了android中listview下拉刷新的实现方法。分享给大家供大家参考,具体如下:
listview中的下拉刷新是非常常见的,也是经常使用的,看到有很多同学想要,那我就整理一下,供大家参考。那我就不解释,直接上代码了。
这里需要自己重写一下listview,重写代码如下:
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
|
package net.loonggg.listview; import java.util.date; import android.content.context; import android.util.attributeset; import android.view.layoutinflater; import android.view.motionevent; import android.view.view; import android.view.viewgroup; import android.view.animation.linearinterpolator; import android.view.animation.rotateanimation; import android.widget.abslistview; import android.widget.abslistview.onscrolllistener; import android.widget.imageview; import android.widget.linearlayout; import android.widget.listview; import android.widget.progressbar; import android.widget.textview; public class mylistview extends listview implements onscrolllistener { private final static int release_to_refresh = 0 ; // 下拉过程的状态值 private final static int pull_to_refresh = 1 ; // 从下拉返回到不刷新的状态值 private final static int refreshing = 2 ; // 正在刷新的状态值 private final static int done = 3 ; private final static int loading = 4 ; // 实际的padding的距离与界面上偏移距离的比例 private final static int ratio = 3 ; private layoutinflater inflater; // listview头部下拉刷新的布局 private linearlayout headerview; private textview lvheadertipstv; private textview lvheaderlastupdatedtv; private imageview lvheaderarrowiv; private progressbar lvheaderprogressbar; // 定义头部下拉刷新的布局的高度 private int headercontentheight; private rotateanimation animation; private rotateanimation reverseanimation; private int starty; private int state; private boolean isback; // 用于保证starty的值在一个完整的touch事件中只被记录一次 private boolean isrecored; private onrefreshlistener refreshlistener; private boolean isrefreshable; public mylistview(context context) { super (context); init(context); } public mylistview(context context, attributeset attrs) { super (context, attrs); init(context); } private void init(context context) { setcachecolorhint(context.getresources().getcolor(r.color.transparent)); inflater = layoutinflater.from(context); headerview = (linearlayout) inflater.inflate(r.layout.lv_header, null ); lvheadertipstv = (textview) headerview .findviewbyid(r.id.lvheadertipstv); lvheaderlastupdatedtv = (textview) headerview .findviewbyid(r.id.lvheaderlastupdatedtv); lvheaderarrowiv = (imageview) headerview .findviewbyid(r.id.lvheaderarrowiv); // 设置下拉刷新图标的最小高度和宽度 lvheaderarrowiv.setminimumwidth( 70 ); lvheaderarrowiv.setminimumheight( 50 ); lvheaderprogressbar = (progressbar) headerview .findviewbyid(r.id.lvheaderprogressbar); measureview(headerview); headercontentheight = headerview.getmeasuredheight(); // 设置内边距,正好距离顶部为一个负的整个布局的高度,正好把头部隐藏 headerview.setpadding( 0 , - 1 * headercontentheight, 0 , 0 ); // 重绘一下 headerview.invalidate(); // 将下拉刷新的布局加入listview的顶部 addheaderview(headerview, null , false ); // 设置滚动监听事件 setonscrolllistener( this ); // 设置旋转动画事件 animation = new rotateanimation( 0 , - 180 , rotateanimation.relative_to_self, 0 .5f, rotateanimation.relative_to_self, 0 .5f); animation.setinterpolator( new linearinterpolator()); animation.setduration( 250 ); animation.setfillafter( true ); reverseanimation = new rotateanimation(- 180 , 0 , rotateanimation.relative_to_self, 0 .5f, rotateanimation.relative_to_self, 0 .5f); reverseanimation.setinterpolator( new linearinterpolator()); reverseanimation.setduration( 200 ); reverseanimation.setfillafter( true ); // 一开始的状态就是下拉刷新完的状态,所以为done state = done; // 是否正在刷新 isrefreshable = false ; } @override public void onscrollstatechanged(abslistview view, int scrollstate) { } @override public void onscroll(abslistview view, int firstvisibleitem, int visibleitemcount, int totalitemcount) { if (firstvisibleitem == 0 ) { isrefreshable = true ; } else { isrefreshable = false ; } } @override public boolean ontouchevent(motionevent ev) { if (isrefreshable) { switch (ev.getaction()) { case motionevent.action_down: if (!isrecored) { isrecored = true ; starty = ( int ) ev.gety(); // 手指按下时记录当前位置 } break ; case motionevent.action_up: if (state != refreshing && state != loading) { if (state == pull_to_refresh) { state = done; changeheaderviewbystate(); } if (state == release_to_refresh) { state = refreshing; changeheaderviewbystate(); onlvrefresh(); } } isrecored = false ; isback = false ; break ; case motionevent.action_move: int tempy = ( int ) ev.gety(); if (!isrecored) { isrecored = true ; starty = tempy; } if (state != refreshing && isrecored && state != loading) { // 保证在设置padding的过程中,当前的位置一直是在head,否则如果当列表超出屏幕的话,当在上推的时候,列表会同时进行滚动 // 可以松手去刷新了 if (state == release_to_refresh) { setselection( 0 ); // 往上推了,推到了屏幕足够掩盖head的程度,但是还没有推到全部掩盖的地步 if (((tempy - starty) / ratio < headercontentheight) // 由松开刷新状态转变到下拉刷新状态 && (tempy - starty) > 0 ) { state = pull_to_refresh; changeheaderviewbystate(); } // 一下子推到顶了 else if (tempy - starty <= 0 ) { // 由松开刷新状态转变到done状态 state = done; changeheaderviewbystate(); } } // 还没有到达显示松开刷新的时候,done或者是pull_to_refresh状态 if (state == pull_to_refresh) { setselection( 0 ); // 下拉到可以进入release_to_refresh的状态 if ((tempy - starty) / ratio >= headercontentheight) { // 由done或者下拉刷新状态转变到松开刷新 state = release_to_refresh; isback = true ; changeheaderviewbystate(); } // 上推到顶了 else if (tempy - starty <= 0 ) { // 由done或者下拉刷新状态转变到done状态 state = done; changeheaderviewbystate(); } } // done状态下 if (state == done) { if (tempy - starty > 0 ) { state = pull_to_refresh; changeheaderviewbystate(); } } // 更新headview的size if (state == pull_to_refresh) { headerview.setpadding( 0 , - 1 * headercontentheight + (tempy - starty) / ratio, 0 , 0 ); } // 更新headview的paddingtop if (state == release_to_refresh) { headerview.setpadding( 0 , (tempy - starty) / ratio - headercontentheight, 0 , 0 ); } } break ; default : break ; } } return super .ontouchevent(ev); } // 当状态改变时候,调用该方法,以更新界面 private void changeheaderviewbystate() { switch (state) { case release_to_refresh: lvheaderarrowiv.setvisibility(view.visible); lvheaderprogressbar.setvisibility(view.gone); lvheadertipstv.setvisibility(view.visible); lvheaderlastupdatedtv.setvisibility(view.visible); lvheaderarrowiv.clearanimation(); // 清除动画 lvheaderarrowiv.startanimation(animation); // 开始动画效果 lvheadertipstv.settext( "松开刷新" ); break ; case pull_to_refresh: lvheaderprogressbar.setvisibility(view.gone); lvheadertipstv.setvisibility(view.visible); lvheaderlastupdatedtv.setvisibility(view.visible); lvheaderarrowiv.clearanimation(); lvheaderarrowiv.setvisibility(view.visible); // 是由release_to_refresh状态转变来的 if (isback) { isback = false ; lvheaderarrowiv.clearanimation(); lvheaderarrowiv.startanimation(reverseanimation); lvheadertipstv.settext( "下拉刷新" ); } else { lvheadertipstv.settext( "下拉刷新" ); } break ; case refreshing: headerview.setpadding( 0 , 0 , 0 , 0 ); lvheaderprogressbar.setvisibility(view.visible); lvheaderarrowiv.clearanimation(); lvheaderarrowiv.setvisibility(view.gone); lvheadertipstv.settext( "正在刷新..." ); lvheaderlastupdatedtv.setvisibility(view.visible); break ; case done: headerview.setpadding( 0 , - 1 * headercontentheight, 0 , 0 ); lvheaderprogressbar.setvisibility(view.gone); lvheaderarrowiv.clearanimation(); lvheaderarrowiv.setimageresource(r.drawable.arrow); lvheadertipstv.settext( "下拉刷新" ); lvheaderlastupdatedtv.setvisibility(view.visible); break ; } } // 此方法直接照搬自网络上的一个下拉刷新的demo,此处是“估计”headview的width以及height private void measureview(view child) { viewgroup.layoutparams params = child.getlayoutparams(); if (params == null ) { params = new viewgroup.layoutparams( viewgroup.layoutparams.fill_parent, viewgroup.layoutparams.wrap_content); } int childwidthspec = viewgroup.getchildmeasurespec( 0 , 0 + 0 , params.width); int lpheight = params.height; int childheightspec; if (lpheight > 0 ) { childheightspec = measurespec.makemeasurespec(lpheight, measurespec.exactly); } else { childheightspec = measurespec.makemeasurespec( 0 , measurespec.unspecified); } child.measure(childwidthspec, childheightspec); } public void setonrefreshlistener(onrefreshlistener refreshlistener) { this .refreshlistener = refreshlistener; isrefreshable = true ; } public interface onrefreshlistener { public void onrefresh(); } public void onrefreshcomplete() { state = done; lvheaderlastupdatedtv.settext( "最近更新:" + new date().tolocalestring()); changeheaderviewbystate(); } private void onlvrefresh() { if (refreshlistener != null ) { refreshlistener.onrefresh(); } } public void setadapter(lvadapter adapter) { lvheaderlastupdatedtv.settext( "最近更新:" + new date().tolocalestring()); super .setadapter(adapter); } } |
重写完listview之后,在布局文件中是这么使用的,头部下拉刷新的布局文件lv_header.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
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
|
<?xml version= "1.0" encoding= "utf-8" ?> <!-- listview的头部 --> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:background= "#000000" > <!-- 内容 --> <relativelayout android:id= "@+id/head_contentlayout" android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:paddingleft= "30dp" > <!-- 箭头图像、进度条 --> <framelayout android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_alignparentleft= "true" android:layout_centervertical= "true" > <!-- 箭头 --> <imageview android:id= "@+id/lvheaderarrowiv" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_gravity= "center" android:src= "@drawable/arrow" /> <!-- 进度条 --> <progressbar android:id= "@+id/lvheaderprogressbar" style= "?android:attr/progressbarstylesmall" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_gravity= "center" android:visibility= "gone" /> </framelayout> <!-- 提示、最近更新 --> <linearlayout android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_centerhorizontal= "true" android:gravity= "center_horizontal" android:orientation= "vertical" > <!-- 提示 --> <textview android:id= "@+id/lvheadertipstv" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "下拉刷新" android:textcolor= "@color/white" android:textsize= "20sp" /> <!-- 最近更新 --> <textview android:id= "@+id/lvheaderlastupdatedtv" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "上次更新" android:textcolor= "@color/gold" android:textsize= "10sp" /> </linearlayout> </relativelayout> </linearlayout> |
在main.xml中进行设置,代码如下:
1
2
3
4
5
6
7
8
9
10
11
|
<?xml version= "1.0" encoding= "utf-8" ?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "fill_parent" android:layout_height= "fill_parent" android:background= "#000000" android:orientation= "vertical" > <net.loonggg.listview.mylistview android:id= "@+id/lv" android:layout_width= "fill_parent" android:layout_height= "fill_parent" /> </linearlayout> |
然后就是在mainactivity中实现,代码如下:
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
|
package net.loonggg.listview; import java.util.arraylist; import java.util.list; import net.loonggg.listview.mylistview.onrefreshlistener; import android.app.activity; import android.os.asynctask; import android.os.bundle; import android.view.view; public class mainactivity extends activity { private list<string> list; private mylistview lv; private lvadapter adapter; @override protected void oncreate(bundle savedinstancestate) { super .oncreate(savedinstancestate); setcontentview(r.layout.activity_main); lv = (mylistview) findviewbyid(r.id.lv); list = new arraylist<string>(); list.add( "loonggg" ); list.add( "我们都是开发者" ); list.add( "我们都是开发者" ); list.add( "我们都是开发者" ); list.add( "我们都是开发者" ); list.add( "我们都是开发者" ); list.add( "我们都是开发者" ); list.add( "我们都是开发者" ); list.add( "我们都是开发者" ); list.add( "我们都是开发者" ); list.add( "我们都是开发者" ); list.add( "我们都是开发者" ); list.add( "我们都是开发者" ); list.add( "我们都是开发者" ); list.add( "我们都是开发者" ); list.add( "我们都是开发者" ); list.add( "我们都是开发者" ); adapter = new lvadapter(list, this ); lv.setadapter(adapter); lv.setonrefreshlistener( new onrefreshlistener() { @override public void onrefresh() { new asynctask< void , void , void >() { protected void doinbackground( void ... params) { try { thread.sleep( 1000 ); } catch (exception e) { e.printstacktrace(); } list.add( "刷新后添加的内容" ); return null ; } @override protected void onpostexecute( void result) { adapter.notifydatasetchanged(); lv.onrefreshcomplete(); } }.execute( null , null , null ); } }); } } |
这里还需要为listview设置一下adapter,自定义的adapter如下:
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
|
package net.loonggg.listview; import java.util.list; import android.content.context; import android.view.view; import android.view.viewgroup; import android.widget.baseadapter; import android.widget.textview; public class lvadapter extends baseadapter { private list<string> list; private context context; public lvadapter(list<string> list, context context) { this .list = list; this .context = context; } @override public int getcount() { return list.size(); } @override public object getitem( int position) { return list.get(position); } @override public long getitemid( int position) { return position; } @override public view getview( int position, view convertview, viewgroup parent) { textview tv = new textview(context.getapplicationcontext()); tv.settext(list.get(position)); return tv; } } |
到这里就完了,代码中的解释非常详细,具体的我就不多说了,也不解释了,自己看看并研究吧!
希望本文所述对大家android程序设计有所帮助。