加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 09:19:09 所属栏目:百科 来源:网络整理
导读:前言:上篇我们讲了怎么初步使用PullToRefresh,但上篇只是几个简单的字符串,在真正的项目中,不可能只是这么简单的,而是复杂的XML的累积,这篇我在前一篇和以前讲的simpleAdapter的基础上,进一步实现复杂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代码

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

    完整代码

    [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.52php.cn/article/p-eqxnrlgc-bae.html,谢谢!

    (编辑:李大同)

    【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读