PullToRefresh使用详解(二)---重写BaseAdapter实现复杂XML下拉刷新

前言:上篇我们讲了怎么初步使用PullToRefresh,但上篇只是几个简单的字符串,在真正的项目中,不可能只是这么简单的,而是复杂的XML的累积,这篇我在前一篇和以前讲的simpleAdapter的基础上,进一步实现复杂XML的下拉刷新

相关文章

(这篇文章是建立在这三篇文章的基础上,其实是在利用了《List控件使用--SimpleAdapter使用详解(二)》的布局和重写BaseAdapter的代码,然后利用了《PullToRefresh使用详解(一)--构建下拉刷新的ListView》的下拉刷新功能。所以文章的部分代码省略没讲,强烈建议大家先看这三篇。)

1、《List控件使用--SimpleAdapter使用详解(一)

2、《List控件使用--SimpleAdapter使用详解(二)

3、《PullToRefresh使用详解(一)--构建下拉刷新的ListView


其它相关文章

4、《PullToRefresh使用详解(一)--构建下拉刷新的listView

5、《PullToRefresh使用详解(三)--实现异步加载的下拉刷新列表》

6、《PullToRefresh使用详解(四)--利用回调函数实现到底加载》

7、《PullToRefresh使用详解(五)--下拉刷新的ScrollView》

效果图:

正在刷新 刷新后


一、XML代码

1、activity_main.xml

PullToRefresh标准写法,与《PullToRefresh使用详解(一)--构建下拉刷新的ListView》布局一样。

[html] view plain copy
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical">
  6. <!--ThePullToRefreshListViewreplacesastandardListViewwidget.-->
  7. com.handmark.pulltorefresh.library.PullToRefreshListView
  8. android:id="@+id/pull_refresh_list"
  9. android:cacheColorHint="#00000000"
  10. android:divider="#19000000"
  11. android:dividerHeight="4dp"
  12. android:fadingEdge="none"
  13. android:fastScrollEnabled="false"
  14. android:footerDividersEnabled="false"
  15. android:headerDividersEnabled="false"
  16. android:smoothScrollbar="true"/>
  17. </LinearLayout>

2、数据项XML(item.xml)

与《List控件使用--SimpleAdapter使用详解(二)》布局一样,只是将名字改成了item.xml

    android:orientation="horizontal"android:layout_width="fill_parent"
  1. android:layout_height="fill_parent">
  2. ImageViewandroid:id="@+id/img"
  3. android:layout_width="wrap_content"
  4. android:layout_height="wrap_content"
  5. android:layout_margin="5px"/>
  6. LinearLayoutandroid:orientation="vertical"
  7. android:layout_width="wrap_content"
  8. android:layout_height="wrap_content"TextViewandroid:id="@+id/name"
  9. android:layout_height="wrap_content"
  10. android:textColor="#FFFFFF00"
  11. android:textSize="22px"TextViewandroid:id="@+id/info"
  12. android:textColor="#FF00FFFF"
  13. android:textSize="13px"

    二、JAVA代码

    先贴出全部代码,然后再慢慢讲。

    完整代码

    packagecom.example.try_pulltorefresh_map;
  1. //try_PullToRefresh_map
  2. /**
  3. *完成了从TXT文本中提取,并向下刷新
  4. *blog:http://blog.csdn.net/harvic880925/article/details/17708409
  5. *@authorharvic
  6. *@date2013-12-31
  7. *
  8. */
  9. importjava.io.BufferedReader;
  10. importjava.io.IOException;
  11. importjava.io.InputStream;
  12. importjava.io.InputStreamReader;
  13. importjava.io.UnsupportedEncodingException;
  14. importjava.util.ArrayList;
  15. importjava.util.HashMap;
  16. importorg.json.JSONArray;
  17. importcom.handmark.pulltorefresh.library.PullToRefreshBase;
  18. importcom.handmark.pulltorefresh.library.PullToRefreshListView;
  19. importcom.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
  20. importcom.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
  21. importandroid.os.AsyncTask;
  22. importandroid.os.Bundle;
  23. importandroid.app.ListActivity;
  24. importandroid.content.Context;
  25. importandroid.graphics.Bitmap;
  26. importandroid.graphics.BitmapFactory;
  27. importandroid.text.format.DateUtils;
  28. importandroid.view.LayoutInflater;
  29. importandroid.view.View;
  30. importandroid.view.ViewGroup;
  31. importandroid.widget.BaseAdapter;
  32. importandroid.widget.ImageView;
  33. importandroid.widget.ListView;
  34. importandroid.widget.TextView;
  35. publicclassMainActivityextendsListActivity{
  36. privateArrayList<HashMap<String,Object>>listItem=newArrayList<HashMap<String,Object>>();
  37. privatePullToRefreshListViewmpullRefreshListView;
  38. MyAdapteradapter=null;
  39. @Override
  40. protectedvoidonCreate(BundlesavedInstanceState){
  41. super.onCreate(savedInstanceState);
  42. setContentView(R.layout.activity_main);
  43. mpullRefreshListView=(PullToRefreshListView)findViewById(R.id.pull_refresh_list);
  44. //设定下拉监听函数
  45. mpullRefreshListView.setonRefreshListener(newOnRefreshListener<ListView>(){
  46. @Override
  47. voidonRefresh(PullToRefreshBase<ListView>refreshView){
  48. Stringlabel=DateUtils.formatDateTime(getApplicationContext(),System.currentTimeMillis(),
  49. DateUtils.FORMAT_SHOW_TIME|DateUtils.FORMAT_SHOW_DATE|DateUtils.FORMAT_ABBREV_ALL);
  50. //UpdatetheLastUpdatedLabel
  51. refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
  52. //Doworktorefreshthelisthere.
  53. newGetDataTask().execute();
  54. }
  55. });
  56. mpullRefreshListView.setMode(Mode.PULL_FROM_END);//设置底部下拉刷新模式
  57. listItem=getData();//获取LIST数据
  58. adapter=newMyAdapter(this);
  59. //设置适配器
  60. ListViewactualListView=mpullRefreshListView.getRefreshableView();
  61. actualListView.setAdapter(adapter);
  62. privateclassGetDataTaskextendsAsyncTask<Void,Void,HashMap<String,Object>>{
  63. //后台处理部分
  64. protectedHashMap<String,Object>doInBackground(Void...params){
  65. //Simulatesabackgroundjob.
  66. try{
  67. Thread.sleep(1000);
  68. }catch(InterruptedExceptione){
  69. HashMap<String,Object>map=newHashMap<String,Object>();
  70. try{
  71. map= map.put("name","林珊");
  72. map.put("info","上传了一张新照片油画");
  73. map.put("img","youhua");
  74. catch(Exceptione){
  75. //Todo:handleexception
  76. setTitle("map出错了");
  77. returnnull;
  78. }
  79. returnmap;
  80. //这里是对刷新的响应,可以利用addFirst()和addLast()函数将新加的内容加到LISTView中
  81. //根据AsyncTask的原理,onPostExecute里的result的值就是doInBackground()的返回值
  82. voidonPostExecute(HashMap<String,Object>result){
  83. //在头部增加新添内容
  84. listItem.add(result);
  85. //通知程序数据集已经改变,如果不做通知,那么将不会刷新mListItems的集合
  86. adapter.notifyDataSetChanged();
  87. //CallonRefreshCompletewhenthelisthasbeenrefreshed.
  88. mpullRefreshListView.onRefreshComplete();
  89. setTitle(e.getMessage());
  90. super.onPostExecute(result);
  91. ArrayList<HashMap<String,Object>>list= InputStreaminputStream;
  92. inputStream=this.getAssets().open("my_home_friends.txt");
  93. Stringjson=readTextFile(inputStream);
  94. JSONArrayarray=newJSONArray(json);
  95. for(inti=0;i<array.length();i++){
  96. "name"));
  97. "info"));
  98. "photo"));
  99. list.add(map);
  100. returnlist;
  101. }catch(Exceptione){
  102. //Todo:handleexception
  103. e.printstacktrace();
  104. finalclassViewHolder{
  105. publicImageViewimg;
  106. publicTextViewname;
  107. publicTextViewinfo;
  108. classMyAdapterextendsBaseAdapter{
  109. privateLayoutInflatermInflater;
  110. publicMyAdapter(Contextcontext){
  111. this.mInflater=LayoutInflater.from(context);
  112. intgetCount(){
  113. //TodoAuto-generatedmethodstub
  114. returnlistItem.size();
  115. publicObjectgetItem(intarg0){
  116. longgetItemId(return0;
  117. publicViewgetView(intposition,ViewconvertView,ViewGroupparent){
  118. ViewHolderholder=if(convertView==null){
  119. holder=newViewHolder();
  120. convertView=mInflater.inflate(R.layout.item,null);
  121. holder.img=(ImageView)convertView.findViewById(R.id.img);
  122. holder.name=(TextView)convertView.findViewById(R.id.name);
  123. holder.info=(TextView)convertView.findViewById(R.id.info);
  124. convertView.setTag(holder);
  125. else{
  126. holder=(ViewHolder)convertView.getTag();
  127. holder.img.setimageBitmap(getHome((String)listItem.get(position).get("img")));
  128. holder.name.setText((String)listItem.get(position).get("name"));
  129. holder.info.setText((String)listItem.get(position).get("info"));
  130. returnconvertView;
  131. *根据图片名称获取主页图片
  132. publicBitmapgetHome(Stringphoto){
  133. StringhomeName=photo+".jpg";
  134. InputStreamis= is=getAssets().open("home/"+homeName);
  135. Bitmapbitmap=BitmapFactory.decodeStream(is);
  136. is.close();
  137. returnbitmap;
  138. ////工具类
  139. /**
  140. *
  141. *@paraminputStream
  142. *@return
  143. */
  144. publicStringreadTextFile(InputStreaminputStream){
  145. StringreadedStr="";
  146. BufferedReaderbr;
  147. br=newBufferedReader(newInputStreamReader(inputStream,"UTF-8"));
  148. Stringtmp;
  149. while((tmp=br.readLine())!=null){
  150. readedStr+=tmp;
  151. br.close();
  152. inputStream.close();
  153. catch(UnsupportedEncodingExceptione){
  154. e.printstacktrace();
  155. catch(IOExceptione){
  156. returnreadedStr;
  157. }
讲解:

执行流程:
看起来代码挺长,其实只看OnCreate()函数就能知道,只是分成了几大块,先贴出OnCreate()函数

    }

1、首先初始化mpullRefreshListView,然后设定监听函数,监听函数没变,我只是更改了GetDataTask()函数里的部分代码,我相信大家也能看懂,难度不大;

2、设定下拉模式,绑定适配器,对应代码

    mpullRefreshListView.setMode(Mode.PULL_FROM_END); actualListView.setAdapter(adapter);
而这里的适配器是经过重写的,新生成一个类MyAdapterextends BaseAdapter,关于重写适配器的方法,参考《List控件使用--SimpleAdapter使用详解(二)》;

到这就结束了,由于这篇文章是几篇文章功能整合,很多东西相关的三篇文章都已经讲过了,也就没必要再讲,所以这里讲的比较粗略。

源码地址:http://download.csdn.net/detail/harvic880925/6790941(不要分,仅供分享

请大家尊重原创者版权,转载请标明出处:http://www.jb51.cc/article/p-eqxnrlgc-bae.html,谢谢!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念
xml文件介绍及使用
xml编程(一)-xml语法
XML文件结构和基本语法
第2章 包装类
XML入门的常见问题(二)
Java对象的强、软、弱和虚引用
JS解析XML文件和XML字符串详解
java中枚举的详细使用介绍
了解Xml格式
XML入门的常见问题(四)
深入SQLite多线程的使用总结详解
PlayFramework完整实现一个APP(一)
XML和YAML的使用方法
XML轻松学习总节篇