效果图: 

                               正在刷新                                                                       刷新后

      

一、导入Library

下载源码后(https://github.com/chrisbanes/Android-PullToRefresh),里面有个Library工程,添加工程到Eclipse中;

另外extras文件夹还有两个工程:PullToRefreshListFragment和PullToRefreshViewPager,由于我们的这个用不到他们的库文件,所以不必导入了;

二、实战

1、新建工程,添加Libray库到工程中

新建工程(try_PullToRefresh)后,右键-》Properties-》Android-》Add  选择上面的Library,然后就是这个样子的

2、重写activity_main.xml

XML内容为:

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns: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. <!--     The PullToRefreshListView replaces a standard ListView widget. -->
  7. <com.handmark.pulltorefresh.library.PullToRefreshListView
  8. android:id="@+id/pull_refresh_list"
  9. android:layout_width="fill_parent"
  10. android:layout_height="fill_parent"
  11. android:cacheColorHint="#00000000"
  12. android:divider="#19000000"
  13. android:dividerHeight="4dp"
  14. android:fadingEdge="none"
  15. android:fastScrollEnabled="false"
  16. android:footerDividersEnabled="false"
  17. android:headerDividersEnabled="false"
  18. android:smoothScrollbar="true" />
  19. </LinearLayout>

其中中间那一大段<com.handmark.pull………………/>就是相当于ListView控件,用这段来代替原是ListView控件的代码

3、JAVA代码讲解

全部代码:

[java]  view plain copy
  1. package com.example.try_pulltorefresh;
  2. import java.util.Arrays;
  3. import java.util.LinkedList;
  4. import com.handmark.pulltorefresh.library.PullToRefreshBase;
  5. import com.handmark.pulltorefresh.library.PullToRefreshListView;
  6. import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
  7. import android.os.AsyncTask;
  8. import android.os.Bundle;
  9. import android.app.Activity;
  10. import android.text.format.DateUtils;
  11. import android.widget.ArrayAdapter;
  12. import android.widget.ListView;
  13. public class MainActivity extends Activity {
  14. private String[] mStrings = { "Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam", "Abondance", "Ackawi",
  15. "Acorn", "Adelost", "Affidelice au Chablis", "Afuega'l Pitu", "Airag", "Airedale", "Aisy Cendre",
  16. "Allgauer Emmentaler", "Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam", "Abondance", "Ackawi",
  17. "Acorn", "Adelost", "Affidelice au Chablis", "Afuega'l Pitu", "Airag", "Airedale", "Aisy Cendre",
  18. "Allgauer Emmentaler" };
  19. private LinkedList<String> mListItems;
  20. private PullToRefreshListView mPullRefreshListView;
  21. private ArrayAdapter<String> mAdapter;
  22. @Override
  23. protected void onCreate(Bundle savedInstanceState) {
  24. super.onCreate(savedInstanceState);
  25. setContentView(R.layout.activity_main);
  26. mPullRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_refresh_list);
  27. // Set a listener to be invoked when the list should be refreshed.
  28. mPullRefreshListView.setOnRefreshListener(new OnRefreshListener<ListView>() {
  29. @Override
  30. public void onRefresh(PullToRefreshBase<ListView> refreshView) {
  31. String label = DateUtils.formatDateTime(getApplicationContext(), System.currentTimeMillis(),
  32. DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);
  33. // Update the LastUpdatedLabel
  34. refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
  35. // Do work to refresh the list here.
  36. new GetDataTask().execute();
  37. }
  38. });
  39. mListItems = new LinkedList<String>();
  40. mListItems.addAll(Arrays.asList(mStrings));
  41. mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mListItems);
  42. //这两个绑定方法用其一
  43. // 方法一
  44. //       mPullRefreshListView.setAdapter(mAdapter);
  45. //方法二
  46. ListView actualListView = mPullRefreshListView.getRefreshableView();
  47. actualListView.setAdapter(mAdapter);
  48. }
  49. private class GetDataTask extends AsyncTask<Void, Void, String> {
  50. //后台处理部分
  51. @Override
  52. protected String doInBackground(Void... params) {
  53. // Simulates a background job.
  54. try {
  55. Thread.sleep(1000);
  56. } catch (InterruptedException e) {
  57. }
  58. String str="Added after refresh...I add";
  59. return str;
  60. }
  61. //这里是对刷新的响应,可以利用addFirst()和addLast()函数将新加的内容加到LISTView中
  62. //根据AsyncTask的原理,onPostExecute里的result的值就是doInBackground()的返回值
  63. @Override
  64. protected void onPostExecute(String result) {
  65. //在头部增加新添内容
  66. mListItems.addFirst(result);
  67. //通知程序数据集已经改变,如果不做通知,那么将不会刷新mListItems的集合
  68. mAdapter.notifyDataSetChanged();
  69. // Call onRefreshComplete when the list has been refreshed.
  70. mPullRefreshListView.onRefreshComplete();
  71. super.onPostExecute(result);
  72. }
  73. }
  74. }

代码讲解:
1、变量定义

[java]  view plain copy
  1. private LinkedList<String> mListItems;    //显示的列表对应原字符串
  2. private PullToRefreshListView mPullRefreshListView;  //PullToRefreshListView实例
  3. private ArrayAdapter<String> mAdapter;  //ListView的适配器

2、在OnCreate()中主要分为两步
(1)初始化mPullRefreshListView并设置监听器,以执行当需要刷新时,应该怎么办,至于真正执行刷新的类GetDataTask()我们后面再细讲,对应代码为:

[java]  view plain copy
  1. mPullRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_refresh_list);
  2. // Set a listener to be invoked when the list should be refreshed.
  3. mPullRefreshListView.setOnRefreshListener(new OnRefreshListener<ListView>() {
  4. @Override
  5. public void onRefresh(PullToRefreshBase<ListView> refreshView) {
  6. String label = DateUtils.formatDateTime(getApplicationContext(), System.currentTimeMillis(),
  7. DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);
  8. // Update the LastUpdatedLabel
  9. refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
  10. // Do work to refresh the list here.
  11. new GetDataTask().execute();
  12. }
  13. });

(2)设置适配器列表内容,并与ListView绑定以显示出来,对应代码为:

[java]  view plain copy
  1. //设置列表内容
  2. mListItems = new LinkedList<String>();
  3. mListItems.addAll(Arrays.asList(mStrings));
  4. mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mListItems);
  5. //这两个绑定方法用其一
  6. // 方法一
  7. //       mPullRefreshListView.setAdapter(mAdapter);
  8. //方法二
  9. ListView actualListView = mPullRefreshListView.getRefreshableView();
  10. actualListView.setAdapter(mAdapter);

3、执行刷新的类GetDataTask()
先贴出这段代码来:

[java]  view plain copy
  1. private class GetDataTask extends AsyncTask<Void, Void, String> {//定义返回值的类型
  2. // 后台处理部分
  3. @Override
  4. protected String doInBackground(Void... params) {
  5. // Simulates a background job.
  6. try {
  7. Thread.sleep(1000);
  8. } catch (InterruptedException e) {
  9. }
  10. String str="Added after refresh...I add";
  11. return str;
  12. }
  13. //这里是对刷新的响应,可以利用addFirst()和addLast()函数将新加的内容加到LISTView中
  14. //根据AsyncTask的原理,onPostExecute里的result的值就是doInBackground()的返回值
  15. @Override
  16. protected void onPostExecute(String result) {
  17. //在头部增加新添内容
  18. mListItems.addFirst(result);
  19. //通知程序数据集已经改变,如果不做通知,那么将不会刷新mListItems的集合
  20. mAdapter.notifyDataSetChanged();
  21. // Call onRefreshComplete when the list has been refreshed.
  22. mPullRefreshListView.onRefreshComplete();
  23. super.onPostExecute(result);//这句是必有的,AsyncTask规定的格式
  24. }
  25. }

(1)派生自AsyncTask

由于派生自AsyncTask,所以下面的那个函数doInBackground和onPostExecute就不难理解了,这两个函数是AsyncTask必须是重写的两个函数
(2)doInBackground函数

doInBackground执行要于后台执行的语句,返回的值可以是任意类型,但要提前在extends AsyncTask<Void, Void, String> 中定义,这个返回值会做为onPostExecute的参数result传到onPostExecute函数中;如果对于网络访问程序,doInBackground就执行访问网络的代码,然后讲返回值存在result中传给onPostExecute函数,以刷新列表;

(3)onPostExecute函数

onPostExecute()是对返回的值进行操作,并添加到ListView的列表中,有两种添加方式添加到头部----mListItems.addFirst(result);和添加在尾部----mListItems.addLast(result);

效果图: 

                               正在刷新                                                                       刷新后

      

一、导入Library

下载源码后(https://github.com/chrisbanes/Android-PullToRefresh),里面有个Library工程,添加工程到Eclipse中;

另外extras文件夹还有两个工程:PullToRefreshListFragment和PullToRefreshViewPager,由于我们的这个用不到他们的库文件,所以不必导入了;

二、实战

1、新建工程,添加Libray库到工程中

新建工程(try_PullToRefresh)后,右键-》Properties-》Android-》Add  选择上面的Library,然后就是这个样子的

2、重写activity_main.xml

XML内容为:

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns: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. <!--     The PullToRefreshListView replaces a standard ListView widget. -->
  7. <com.handmark.pulltorefresh.library.PullToRefreshListView
  8. android:id="@+id/pull_refresh_list"
  9. android:layout_width="fill_parent"
  10. android:layout_height="fill_parent"
  11. android:cacheColorHint="#00000000"
  12. android:divider="#19000000"
  13. android:dividerHeight="4dp"
  14. android:fadingEdge="none"
  15. android:fastScrollEnabled="false"
  16. android:footerDividersEnabled="false"
  17. android:headerDividersEnabled="false"
  18. android:smoothScrollbar="true" />
  19. </LinearLayout>

其中中间那一大段<com.handmark.pull………………/>就是相当于ListView控件,用这段来代替原是ListView控件的代码

3、JAVA代码讲解

全部代码:

[java]  view plain copy
  1. package com.example.try_pulltorefresh;
  2. import java.util.Arrays;
  3. import java.util.LinkedList;
  4. import com.handmark.pulltorefresh.library.PullToRefreshBase;
  5. import com.handmark.pulltorefresh.library.PullToRefreshListView;
  6. import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
  7. import android.os.AsyncTask;
  8. import android.os.Bundle;
  9. import android.app.Activity;
  10. import android.text.format.DateUtils;
  11. import android.widget.ArrayAdapter;
  12. import android.widget.ListView;
  13. public class MainActivity extends Activity {
  14. private String[] mStrings = { "Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam", "Abondance", "Ackawi",
  15. "Acorn", "Adelost", "Affidelice au Chablis", "Afuega'l Pitu", "Airag", "Airedale", "Aisy Cendre",
  16. "Allgauer Emmentaler", "Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam", "Abondance", "Ackawi",
  17. "Acorn", "Adelost", "Affidelice au Chablis", "Afuega'l Pitu", "Airag", "Airedale", "Aisy Cendre",
  18. "Allgauer Emmentaler" };
  19. private LinkedList<String> mListItems;
  20. private PullToRefreshListView mPullRefreshListView;
  21. private ArrayAdapter<String> mAdapter;
  22. @Override
  23. protected void onCreate(Bundle savedInstanceState) {
  24. super.onCreate(savedInstanceState);
  25. setContentView(R.layout.activity_main);
  26. mPullRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_refresh_list);
  27. // Set a listener to be invoked when the list should be refreshed.
  28. mPullRefreshListView.setOnRefreshListener(new OnRefreshListener<ListView>() {
  29. @Override
  30. public void onRefresh(PullToRefreshBase<ListView> refreshView) {
  31. String label = DateUtils.formatDateTime(getApplicationContext(), System.currentTimeMillis(),
  32. DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);
  33. // Update the LastUpdatedLabel
  34. refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
  35. // Do work to refresh the list here.
  36. new GetDataTask().execute();
  37. }
  38. });
  39. mListItems = new LinkedList<String>();
  40. mListItems.addAll(Arrays.asList(mStrings));
  41. mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mListItems);
  42. //这两个绑定方法用其一
  43. // 方法一
  44. //       mPullRefreshListView.setAdapter(mAdapter);
  45. //方法二
  46. ListView actualListView = mPullRefreshListView.getRefreshableView();
  47. actualListView.setAdapter(mAdapter);
  48. }
  49. private class GetDataTask extends AsyncTask<Void, Void, String> {
  50. //后台处理部分
  51. @Override
  52. protected String doInBackground(Void... params) {
  53. // Simulates a background job.
  54. try {
  55. Thread.sleep(1000);
  56. } catch (InterruptedException e) {
  57. }
  58. String str="Added after refresh...I add";
  59. return str;
  60. }
  61. //这里是对刷新的响应,可以利用addFirst()和addLast()函数将新加的内容加到LISTView中
  62. //根据AsyncTask的原理,onPostExecute里的result的值就是doInBackground()的返回值
  63. @Override
  64. protected void onPostExecute(String result) {
  65. //在头部增加新添内容
  66. mListItems.addFirst(result);
  67. //通知程序数据集已经改变,如果不做通知,那么将不会刷新mListItems的集合
  68. mAdapter.notifyDataSetChanged();
  69. // Call onRefreshComplete when the list has been refreshed.
  70. mPullRefreshListView.onRefreshComplete();
  71. super.onPostExecute(result);
  72. }
  73. }
  74. }

代码讲解:
1、变量定义

[java]  view plain copy
  1. private LinkedList<String> mListItems;    //显示的列表对应原字符串
  2. private PullToRefreshListView mPullRefreshListView;  //PullToRefreshListView实例
  3. private ArrayAdapter<String> mAdapter;  //ListView的适配器

2、在OnCreate()中主要分为两步
(1)初始化mPullRefreshListView并设置监听器,以执行当需要刷新时,应该怎么办,至于真正执行刷新的类GetDataTask()我们后面再细讲,对应代码为:

[java]  view plain copy
  1. mPullRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_refresh_list);
  2. // Set a listener to be invoked when the list should be refreshed.
  3. mPullRefreshListView.setOnRefreshListener(new OnRefreshListener<ListView>() {
  4. @Override
  5. public void onRefresh(PullToRefreshBase<ListView> refreshView) {
  6. String label = DateUtils.formatDateTime(getApplicationContext(), System.currentTimeMillis(),
  7. DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);
  8. // Update the LastUpdatedLabel
  9. refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
  10. // Do work to refresh the list here.
  11. new GetDataTask().execute();
  12. }
  13. });

(2)设置适配器列表内容,并与ListView绑定以显示出来,对应代码为:

[java]  view plain copy
  1. //设置列表内容
  2. mListItems = new LinkedList<String>();
  3. mListItems.addAll(Arrays.asList(mStrings));
  4. mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mListItems);
  5. //这两个绑定方法用其一
  6. // 方法一
  7. //       mPullRefreshListView.setAdapter(mAdapter);
  8. //方法二
  9. ListView actualListView = mPullRefreshListView.getRefreshableView();
  10. actualListView.setAdapter(mAdapter);

3、执行刷新的类GetDataTask()
先贴出这段代码来:

[java]  view plain copy
  1. private class GetDataTask extends AsyncTask<Void, Void, String> {//定义返回值的类型
  2. // 后台处理部分
  3. @Override
  4. protected String doInBackground(Void... params) {
  5. // Simulates a background job.
  6. try {
  7. Thread.sleep(1000);
  8. } catch (InterruptedException e) {
  9. }
  10. String str="Added after refresh...I add";
  11. return str;
  12. }
  13. //这里是对刷新的响应,可以利用addFirst()和addLast()函数将新加的内容加到LISTView中
  14. //根据AsyncTask的原理,onPostExecute里的result的值就是doInBackground()的返回值
  15. @Override
  16. protected void onPostExecute(String result) {
  17. //在头部增加新添内容
  18. mListItems.addFirst(result);
  19. //通知程序数据集已经改变,如果不做通知,那么将不会刷新mListItems的集合
  20. mAdapter.notifyDataSetChanged();
  21. // Call onRefreshComplete when the list has been refreshed.
  22. mPullRefreshListView.onRefreshComplete();
  23. super.onPostExecute(result);//这句是必有的,AsyncTask规定的格式
  24. }
  25. }

(1)派生自AsyncTask

由于派生自AsyncTask,所以下面的那个函数doInBackground和onPostExecute就不难理解了,这两个函数是AsyncTask必须是重写的两个函数
(2)doInBackground函数

doInBackground执行要于后台执行的语句,返回的值可以是任意类型,但要提前在extends AsyncTask<Void, Void, String> 中定义,这个返回值会做为onPostExecute的参数result传到onPostExecute函数中;如果对于网络访问程序,doInBackground就执行访问网络的代码,然后讲返回值存在result中传给onPostExecute函数,以刷新列表;

(3)onPostExecute函数

onPostExecute()是对返回的值进行操作,并添加到ListView的列表中,有两种添加方式添加到头部----mListItems.addFirst(result);和添加在尾部----mListItems.addLast(result);

PullToRefresh使用详解(一)--构建下拉刷新的listView相关推荐

  1. android listview下拉刷新动画,ListView下拉刷新实现方式详解和改造(上)

    我们知道页面的下拉刷新目前基本已经成为智能移动终端的标配刷新方式.Twitter设计出现有的下拉刷新(在2013年申请了专利). 下拉刷新1.jpg 这一优美而又简单的刷新方式,很快使得各大系统纷纷效 ...

  2. android 下拉刷新listview,实现Android下拉刷新的ListView

    ListView的下拉刷新及上拉加载更多数据是我们开发中通常要实现的功能,开源项目中有很多的上下拉加载刷新的ListView可直接拿来使用,这几天刚好学习了下刷新的实现方式,把学习的资料做个记录: 实 ...

  3. 详解element-ui设置下拉选择切换必填和非必填

    <template><div><el-form:model="ruleForm":rules="rules"ref="r ...

  4. this.$router.push如何刷新页面_小程序丨微信小程序如何实现页面下拉刷新

    微信小程序蕴含着众多功能,本期将简单介绍实现页面下拉刷新的方法,通过阅读本文,读者们可以自行动手操作,在实践中认识微信小程序. 首先,我们需在json配置中写出以下配置: "enablePu ...

  5. Android PullToRefresh(下拉刷新)的使用详解

    开源项地址:https://github.com/chrisbanes/Android-PullToRefresh 在Android-PullToRefresh-master文件夹下,我们会看到还有三 ...

  6. pullToRefresh下拉刷新上拉加载

    PullToRefresh 是一个第三方的工程. 之前的自定义下拉刷新控件貌似不太好用,于是网上找了这个. 参考:http://www.cnblogs.com/summers/p/4343964.ht ...

  7. Android ListView 实现下拉刷新上拉加载

    转载请注明出处:http://blog.csdn.net/allen315410/article/details/39965327 1.简介 无疑,在Android开发中,ListView是使用非常频 ...

  8. Android自定义控件之仿美团下拉刷新

    android美团下拉刷新控件自定义控件 目录(?)[+]   美团的下拉刷新分为三个状态:  第一个状态为下拉刷新状态(pull to refresh),在这个状态下是一个绿色的椭圆随着下拉的距离动 ...

  9. android官方自带下拉刷新功能

    最近想写下拉刷新功能,网上找的第三方框架最著名的pullToRefresh也早在2013年停止维护了,偶然间发现谷歌公司早已推出了自家的下拉刷新功能,位于v4包中,效果请看下图: 使用的就是andro ...

最新文章

  1. Bootstrap中文本的样式
  2. javascript:闭包的总结
  3. 【Unity】2.11 了解游戏有哪些分类对你开阔思路有好处
  4. python学全栈还是运维_Python全栈学习——Python基础及Web开发
  5. POJ1080 Human Gene Functions 动态规划 LCS的变形
  6. 在Gradle中为JPMS构建Java 6-8库
  7. 下面不是mysql特性_下面( )不是MySQL的特性。_学小易找答案
  8. mpython 直接访问_如何从python代码中直接访问Android的Service
  9. 从Graalvm性能测试结果看选择JVM和native-image的策略
  10. f2blog最新注入漏洞
  11. redis 系列19 客户端
  12. Android FrameWork——ActivityManager框架
  13. 正确理解springboot的常用注入方式
  14. PCA(主成分分析)的简单理解
  15. 求两个集合是否有交集 c语言_高中数学:集合与函数概念知识点汇总
  16. C#中生成随机数的方法和语句
  17. 传奇开服教程——legend/blue引擎替换和登陆器生成教程
  18. Linux自学之旅-安装篇(磁盘分区)
  19. 数据分析之--Mataplotlib入门
  20. 关于Dev C++等软件突然提示16位应用程序不兼容的问题

热门文章

  1. VMware虚拟化技术培训
  2. 2010年5月编程语言的排行指数-Objective-C闯进前十!
  3. 在windows部署hadoopSpark和IDEA
  4. 数据管理的三个阶段(人工管理,文件系统,数据库系统)
  5. Python3:字符串处理函数
  6. 注意力机制QKV的作用 简单易懂
  7. PowerShell教程
  8. CodeMix使用教程:构建自定义DevStyle主题
  9. 通过游戏编程学Python(2)— 脑筋急转弯
  10. LRN(Local Response Normalization)局部归一化分析