PullToRefresh使用详解-实现异步加载的下拉刷新列表_第1页
PullToRefresh使用详解-实现异步加载的下拉刷新列表_第2页
PullToRefresh使用详解-实现异步加载的下拉刷新列表_第3页
PullToRefresh使用详解-实现异步加载的下拉刷新列表_第4页
PullToRefresh使用详解-实现异步加载的下拉刷新列表_第5页
已阅读5页,还剩6页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

PullToRefresh 使用详解 二 使用详解 二 实现异步加载的下拉刷新列实现异步加载的下拉刷新列 表表 前言 根据前几篇的阶段性成果 下拉刷新 异步加载 将其集成 就成了这篇文章 这篇文 章代码量比较大 对于异步加载的部分 除了更改了 getView 里绑定部分的代码 其它的 都没有动 所以异步刷新里的代码我就不往里贴了 只贴 MainActivity java 的代码 主要看 看主程序是如何实现异步加载图片和下拉刷新的 效果图 效果图 初始化后 正在加载图片 加载出一部分 下拉刷新 新生成的 ITEM 加载完成新生成 ITEM 的图片 一 一 MainActivity java 其它的代码就不讲了 我只说说这个主页面是如何动作的 先看看整体代码 java view plaincopyprint 1 package com example try simpleadapter new 2 3 完成与服务器通信的下拉刷新 4 author harvic 5 6 import java io BufferedReader 7 import java io InputStreamReader 8 import java util ArrayList 9 import java util List 10 11 import org apache http HttpEntity 12 import org apache http HttpResponse 13 import org apache http NameValuePair 14 import org apache http client entity UrlEncodedFormEntity 15 import org apache http client methods HttpPost 16 import org apache http impl client DefaultHttpClient 17 import org apache http message BasicNameValuePair 18 import org apache http protocol HTTP 19 import org json JSONArray 20 21 22 import com handmark pulltorefresh library PullToRefreshBase 23 import com handmark pulltorefresh library PullToRefreshListView 24 import com handmark pulltorefresh library PullToRefreshBase Mode 25 import com handmark pulltorefresh library PullToRefreshBase OnRefreshListe ner 26 27 import android os AsyncTask 28 import android os Bundle 29 import android text format DateUtils 30 import android util Log 31 import android widget ListView 32 import android app ListActivity 33 34 public class MainActivity extends ListActivity 35 36 private String serverIP http 222 195 151 19 37 private List mData 38 private PullToRefreshListView mPullRefreshListView 39 ImageAndTextListAdapter adapter null 40 Override 41 public void onCreate Bundle savedInstanceState 42 super onCreate savedInstanceState 43 setContentView R layout activity main 44 45 mPullRefreshListView PullToRefreshListView findViewById R id pull refresh list 46 47 设定下拉监听函数 48 mPullRefreshListView setOnRefreshListener new OnRefreshListener 49 Override 50 public void onRefresh PullToRefreshBase refreshView 51 String label DateUtils formatDateTime getApplicationContext Sy stem currentTimeMillis 52 DateUtils FORMAT SHOW TIME DateUtils FORMAT SHO W DATE DateUtils FORMAT ABBREV ALL 53 54 Update the LastUpdatedLabel 55 refreshView getLoadingLayoutProxy setLastUpdatedLabel label 56 57 Log d msg this this 58 Do work to refresh the list here 59 new GetDataTask execute 60 61 62 63 64 mPullRefreshListView setMode Mode PULL FROM END 设置底部下 拉刷新模式 65 传参生成适配器 66 mData getData 67 ListView actualListView mPullRefreshListView getRefreshableView 68 adapter new ImageAndTextListAdapter this mData actualListView 69 70 设置适配器 71 actualListView setAdapter adapter 72 73 74 private List getData 75 创建默认的 httpClient 实例 76 DefaultHttpClient httpclient new DefaultHttpClient 77 HttpResponse response null 78 HttpEntity entity null 79 80 StringBuilder builder new StringBuilder 81 JSONArray jsonArray null 82 83 List list new ArrayList 84 85 try 86 创建 httpost 访问本地服务器网址 87 HttpPost httpost new HttpPost serverIP try an server index p hp 88 89 构造 POST 方法的 name value 参数对 90 List vps new ArrayList 91 将参数传入 post 方法中 92 vps add new BasicNameValuePair action insert 93 vps add new BasicNameValuePair name 进去了 94 95 httpost setEntity new UrlEncodedFormEntity vps HTTP UTF 8 96 response httpclient execute httpost 执行 97 98 if response getEntity null 99 如果服务器端 JSON 没写对 这句是会出异常 是执行不过去 的 100 BufferedReader reader new BufferedReader new InputStrea mReader response getEntity getContent 101 String s reader readLine 102 for s null s reader readLine 103 builder append s 104 105 Log i msg builder toString builder toString 106 107 jsonArray new JSONArray builder toString 108 for int i 0 i jsonArray length i 109 if jsonArray getJSONObject i getInt id 1 110 String name jsonArray getJSONObject i getString nam e 111 String info jsonArray getJSONObject i getString info 112 String PicName jsonArray getJSONObject i getString p hoto 113 String picURL serverIP try an server PicName jp g 114 115 ImageAndText item new ImageAndText picURL name inf o 116 list add item 117 118 119 120 catch Exception e 121 e printStackTrace 122 finally 123 try 124 if entity null 125 126 httpclient getConnectionManager shutdown 关闭连接 127 这两种释放连接的方法都可以 128 129 catch Exception e 130 TODO Auto generated catch block 131 e printStackTrace 132 133 134 135 return list 136 137 138 139 140 141 private class GetDataTask extends AsyncTask 142 143 后台处理部分 144 Override 145 protected ImageAndText doInBackground Void params 146 Simulates a background job 147 ImageAndText item null 148 try 149 item new ImageAndText serverIP try an server xizang jpg sss ssss 150 catch Exception e 151 TODO handle exception 152 setTitle map 出错了 153 154 155 return item 156 157 158 这里是对刷新的响应 可以利用 addFirst 和 addLast 函数将新加 的内容加到 LISTView 中 159 根据 AsyncTask 的原理 onPostExecute 里的 result 的值就是 doInBackground 的返回值 160 Override 161 protected void onPostExecute ImageAndText result 162 在头部增加新添内容 163 164 try 165 mData add result 166 167 通知程序数据集已经改变 如果不做通知 那么将不会刷新 mListItems 的集合 168 adapter notifyDataSetChanged 169 adapter loadImage 170 Call onRefreshComplete when the list has been refreshed 171 mPullRefreshListView onRefreshComplete 172 catch Exception e 173 TODO handle exception 174 setTitle e getMessage 175 176 177 super onPostExecute result 178 179 180 181 182 分开讲解 分开讲解 1 先看 先看 OnCreate 函数函数 java view plaincopyprint 1 mPullRefreshListView PullToRefreshListView findViewById R id pull refresh list 2 3 设定下拉监听函数 4 mPullRefreshListView setOnRefreshListener new OnRefreshListener 5 Override 6 public void onRefresh PullToRefreshBase refreshView 7 String label DateUtils formatDateTime getApplicationContext System c urrentTimeMillis 8 DateUtils FORMAT SHOW TIME DateUtils FORMAT SHOW DAT E DateUtils FORMAT ABBREV ALL 9 10 Update the LastUpdatedLabel 11 refreshView getLoadingLayoutProxy setLastUpdatedLabel label 12 13 Log d msg this this 14 Do work to refresh the list here 15 new GetDataTask execute 16 17 18 这段代码先初始化了 mPullRefreshListView 然后设置了下拉监听的函数 在下拉时执行 GetDataTask 函数 2 看看 看看 GetDataTask 函数做了那些改动 函数做了那些改动 java view plaincopyprint 1 private class GetDataTask extends AsyncTask 2 后台处理部分 3 Override 4 protected ImageAndText doInBackground Void params 5 Simulates a background job 6 ImageAndText item null 7 try 8 item new ImageAndText serverIP try an server xizang jpg sss ssss 9 catch Exception e 10 TODO handle exception 11 setTitle map 出错了 12 13 14 return item 15 16 17 这里是对刷新的响应 可以利用 addFirst 和 addLast 函数将新加的内 容加到 LISTView 中 18 根据 AsyncTask 的原理 onPostExecute 里的 result 的值就是 doInBackground 的返回值 19 Override 20 protected void onPostExecute ImageAndText result 21 在头部增加新添内容 22 23 try 24 mData add result 25 26 通知程序数据集已经改变 如果不做通知 那么将不会刷新 mListItems 的集合 27 adapter notifyDataSetChanged 28 adapter loadImage 29 Call onRefreshComplete when the list has been refreshed 30 mPullRefreshListView onRefreshComplete 31 catch Exception e 32 TODO handle exception 33 setTitle e getMessage 34 35 36 super onPostExecute result 37 38 在 doInBackground 中新增了一个 ITEM 项 然后在 onPostExecute 中将这个项加 入到 mData 数据列表中 然后利用 adapter notifyDa

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论