1.简介

       无疑,在Android开发中,ListView是使用非常频繁的控件之一,ListView提供一个列表的容易,允许我们以列表的形式将数据展示到界面上,但是Google给我们提供的原生ListView的控件,虽然在功能上很强大,但是在用户体验和动态效果上,还是比较差劲的。为了改善用户体验,市面上纷纷出现了各种各样的自定义的ListView,他们功能强大,界面美观,使我们该需要学习的地方。其中,使用最频繁的功能无疑就是ListView的下拉刷新和上拉加载数据了,几乎在没一款内容型的App中都可以找到这种控件的身影,尤其是需要联网获取数据的模块,使用的就更为频繁了,so,我们很有必要了解下这种效果是怎么实现的。

2.开源组件PullToRefreshList介绍      

      既然Android和Java都是开源的,一些常见的功能或者效果就不难被找到。PullToRefresh就是一个典型的例子,PullToRefresh是老外写的一个开源ListView组件,这个项目在ListView的基础上扩展了ListView的功能,增强了Listview的用户体验,功能十分强大,而且很容易被集成到当前的项目中,你只需要调用简单的API,即可省去很多不必要的麻烦,非常棒。以上是项目在Github的链接,有兴趣的可以戳进去down下来,使用一下。这里不是我们的重点,不想废话了。
PullToRefresh的Github项目地址:
[html]  view plain copy print ?  
 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="wrap_content"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <LinearLayout  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:layout_gravity="center_horizontal"  
  11.         android:layout_margin="10dip"  
  12.         android:gravity="center_vertical"  
  13.         android:orientation="horizontal" >  
  14.   
  15.         <ProgressBar  
  16.             android:layout_width="wrap_content"  
  17.             android:layout_height="wrap_content"  
  18.             android:layout_gravity="center"  
  19.             android:indeterminateDrawable="@drawable/common_progressbar" />  
  20.   
  21.         <TextView  
  22.             android:layout_width="wrap_content"  
  23.             android:layout_height="wrap_content"  
  24.             android:layout_marginLeft="10dip"  
  25.             android:text="加载更多..."  
  26.             android:textColor="#FF0000"  
  27.             android:textSize="18sp" />  
  28.     </LinearLayout>  
  29.   
  30. </LinearLayout>  
此外,两个布局都用到一个ProgressBar的背景,其XML如下:
[java]  view plain copy print ?  
 
  1. public interface OnRefreshListener {  
  2.   
  3.     /** 
  4.      * 下拉刷新 
  5.      */  
  6.     void onDownPullRefresh();  
  7.   
  8.     /** 
  9.      * 上拉加载更多 
  10.      */  
  11.     void onLoadingMore();  
  12. }  
这个回调的接口定义了两个方法,分别是“下拉刷新”和“上拉加载”,然后还必须在ListView中暴露一个接口与外面的类链接,最好的方法就暴露公共方法,例如: