1、PullToRefresh使用详解实现异步加载的下拉刷新列表资料PullToRefresh使用详解(二)-实现异步加载的下拉刷新列表 前言:根据前几篇的阶段性成果(下拉刷新、异步加载),将其集成,就成了这篇文章,这篇文章代码量比较大,对于异步加载的部分,除了更改了getView()里绑定部分的代码,其它的都没有动,所以异步刷新里的代码我就不往里贴了,只贴MainActivity.java的代码,主要看看主程序是如何实现异步加载图片和下拉刷新的。效果图:初始化后,正在加载图片 加载出一部分 下拉刷新 新生成的ITEM 加载完成新生成ITEM的图片一、MainActivity.java其它的代码就
2、不讲了,我只说说这个主页面是如何动作的,先看看整体代码。java view plaincopyprint?1. packagecom.example.try_simpleadapter_new;2. /*3. *完成与服务器通信的下拉刷新4. *authorharvic5. */6. importjava.io.BufferedReader;7. importjava.io.InputStreamReader;8. importjava.util.ArrayList;9. importjava.util.List;10. 11. importorg.apache.http.HttpEntity
3、;12. importorg.apache.http.HttpResponse;13. importorg.apache.http.NameValuePair;14. importorg.apache.http.client.entity.UrlEncodedFormEntity;15. importorg.apache.http.client.methods.HttpPost;16. importorg.apache.http.impl.client.DefaultHttpClient;17. importorg.apache.http.message.BasicNameValuePair;
4、18. importorg.apache.http.protocol.HTTP;19. importorg.json.JSONArray;20. 21. 22. importcom.handmark.pulltorefresh.library.PullToRefreshBase;23. importcom.handmark.pulltorefresh.library.PullToRefreshListView;24. importcom.handmark.pulltorefresh.library.PullToRefreshBase.Mode;25. importcom.handmark.pu
5、lltorefresh.library.PullToRefreshBase.OnRefreshListener;26. 27. importandroid.os.AsyncTask;28. importandroid.os.Bundle;29. importandroid.text.format.DateUtils;30. importandroid.util.Log;31. importandroid.widget.ListView;32. importandroid.app.ListActivity;33. 34. publicclassMainActivityextendsListAct
6、ivity35. 36. privateStringserverIP=http:/222.195.151.19;37. privateListmData;38. privatePullToRefreshListViewmPullRefreshListView;39. ImageAndTextListAdapteradapter=null;40. Override41. publicvoidonCreate(BundlesavedInstanceState)42. super.onCreate(savedInstanceState);43. setContentView(R.layout.act
7、ivity_main);44. 45. mPullRefreshListView=(PullToRefreshListView)findViewById(R.id.pull_refresh_list);46. 47. /设定下拉监听函数 48. mPullRefreshListView.setOnRefreshListener(newOnRefreshListener()49. Override50. publicvoidonRefresh(PullToRefreshBaserefreshView)51. Stringlabel=DateUtils.formatDateTime(getAppl
8、icationContext(),System.currentTimeMillis(),52. DateUtils.FORMAT_SHOW_TIME|DateUtils.FORMAT_SHOW_DATE|DateUtils.FORMAT_ABBREV_ALL);53. 54. /UpdatetheLastUpdatedLabel 55. refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);56. 57. Log.d(msg,this=+this);58. /Doworktorefreshthelisthere. 59.
9、newGetDataTask().execute();60. 61. 62. );63. 64. mPullRefreshListView.setMode(Mode.PULL_FROM_END);/设置底部下拉刷新模式 65. /传参生成适配器 66. mData=getData();67. ListViewactualListView=mPullRefreshListView.getRefreshableView();68. adapter=newImageAndTextListAdapter(this,mData,actualListView);69. 70. /设置适配器 71. act
10、ualListView.setAdapter(adapter);72. 73. 74. privateListgetData()75. /创建默认的httpClient实例. 76. DefaultHttpClienthttpclient=newDefaultHttpClient();77. HttpResponseresponse=null;78. HttpEntityentity=null;79. 80. StringBuilderbuilder=newStringBuilder();81. JSONArrayjsonArray=null;82. 83. Listlist=newArray
11、List();84. 85. try86. /创建httpost.访问本地服务器网址 87. HttpPosthttpost=newHttpPost(serverIP+/try_an_server/index.php);88. 89. /构造POST方法的name:value参数对 90. Listvps=newArrayList();91. /将参数传入post方法中 92. vps.add(newBasicNameValuePair(action,insert);93. vps.add(newBasicNameValuePair(name,进去了);94. 95. httpost.setE
12、ntity(newUrlEncodedFormEntity(vps,HTTP.UTF_8);96. response=httpclient.execute(httpost);/执行 97. 98. if(response.getEntity()!=null)99. /如果服务器端JSON没写对,这句是会出异常,是执行不过去的 100. BufferedReaderreader=newBufferedReader(newInputStreamReader(response.getEntity().getContent();101. Strings=reader.readLine();102. f
13、or(;s!=null;s=reader.readLine()103. builder.append(s);104. 105. Log.i(msg,builder.toString=+builder.toString();106. 107. jsonArray=newJSONArray(builder.toString();108. for(inti=0;ijsonArray.length();i+)109. if(jsonArray.getJSONObject(i).getInt(id)=1)110. Stringname=jsonArray.getJSONObject(i).getStri
14、ng(name);111. Stringinfo=jsonArray.getJSONObject(i).getString(info);112. StringPicName=jsonArray.getJSONObject(i).getString(photo);113. StringpicURL=serverIP+/try_an_server/+PicName+.jpg;114. 115. ImageAndTextitem=newImageAndText(picURL,name,info);116. list.add(item);117. 118. 119. 120. catch(Except
15、ione)121. e.printStackTrace();122. finally123. try124. if(entity!=null)125. 126. httpclient.getConnectionManager().shutdown();/关闭连接 127. /这两种释放连接的方法都可以 128. 129. catch(Exceptione)130. /TODOAuto-generatedcatchblock 131. e.printStackTrace();132. 133. 134. 135. returnlist;136. 137. 138. 139. 140. 141.
16、privateclassGetDataTaskextendsAsyncTask142. 143. /后台处理部分 144. Override145. protectedImageAndTextdoInBackground(Void.params)146. /Simulatesabackgroundjob. 147. ImageAndTextitem=null;148. try149. item=newImageAndText(serverIP+/try_an_server/xizang.jpg,sss,ssss);150. catch(Exceptione)151. /TODO:handlee
17、xception 152. setTitle(map出错了);153. 154. 155. returnitem;156. 157. 158. /这里是对刷新的响应,可以利用addFirst()和addLast()函数将新加的内容加到LISTView中 159. /根据AsyncTask的原理,onPostExecute里的result的值就是doInBackground()的返回值 160. Override161. protectedvoidonPostExecute(ImageAndTextresult)162. /在头部增加新添内容 163. 164. try165. mData.ad
18、d(result);166. 167. /通知程序数据集已经改变,如果不做通知,那么将不会刷新mListItems的集合 168. adapter.notifyDataSetChanged();169. adapter.loadImage();170. /CallonRefreshCompletewhenthelisthasbeenrefreshed. 171. mPullRefreshListView.onRefreshComplete();172. catch(Exceptione)173. /TODO:handleexception 174. setTitle(e.getMessage(
19、);175. 176. 177. super.onPostExecute(result);178. 179. 180. 181. 182. package com.example.try_simpleadapter_new;/* * 完成与服务器通信的下拉刷新 * author harvic */import java.io.BufferedReader;import java.io.InputStreamReader;import java.util.ArrayList;import java.util.List;import org.apache.http.HttpEntity;impor
20、t org.apache.http.HttpResponse;import org.apache.http.NameValuePair;import org.apache.http.client.entity.UrlEncodedFormEntity;import org.apache.http.client.methods.HttpPost;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.message.BasicNameValuePair;import org.apache.http.p
21、rotocol.HTTP;import org.json.JSONArray;import com.handmark.pulltorefresh.library.PullToRefreshBase;import com.handmark.pulltorefresh.library.PullToRefreshListView;import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;import android.os.AsyncTask;import android.os.Bundle;import android.text.format.D
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1