使用PullToRefresh实现下拉刷新和上拉加载

PullToRefresh是一套实现很是好的下拉刷新库,它支持:html

1.ListViewjava

2.ExpandableListViewandroid

3.GridViewgit

4.WebViewgithub

等多种经常使用的须要刷新的View类型,并且使用起来也十分方便。eclipse

(下载地址:https://github.com/chrisbanes/Android-PullToRefresh)ide

 

下载完成,将它导入到eclipse中,做为一个library导入到你的工程中就行了。布局

 

1、废话少说,下拉刷新go。this

 1.在你的布局文件中加上你想用的View就行了,好比这儿我想用一个支持下拉 刷新的ExpandableListViewspa

 

[html]  view plain copy 在CODE上查看代码片 派生到个人代码片
 
  1. <com.handmark.pulltorefresh.library.PullToRefreshExpandableListView  
  2.     android:id="@+id/expand_list"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent" />  


2. 在你的Activity代码中进行简单的设置:

 

 

[java]  view plain copy 在CODE上查看代码片 派生到个人代码片
 
  1. mExpandList = (PullToRefreshExpandableListView) rootView.findViewById(R.id.expand_list);  
  2. mExpandList.getRefreshableView().setGroupIndicator(null);  
  3. mExpandList.getRefreshableView().setDivider(null);  
  4. mExpandList.getRefreshableView().setSelector(android.R.color.transparent);  
  5. mExpandList.getRefreshableView().setOnGroupClickListener(this);  
  6. mExpandList.setOnRefreshListener(this);  

 

第一行是找到这个View,最后一行是为它加上刷新的监听器,中间的几行是我对ExpandableListView进行一些设置。

这样其实就已经能够下拉刷新了,但刷新时须要运行的代码写在哪呢,还有为何下拉不会收起来呢,且往下看。

 

3.下拉刷新时执行的方法onRefresh()

 

[java]  view plain copy 在CODE上查看代码片 派生到个人代码片
 
  1. @Override  
  2. public void onRefresh(PullToRefreshBase<ExpandableListView> refreshView) {  
  3.     if (!isRefreshing) {  
  4.         isRefreshing = true;  
  5.         updateList(true);  
  6.     } else {  
  7.         mExpandList.onRefreshComplete();  
  8.     }  
  9. }  


通常来讲咱们会开另外一个线程去获取数据,因此这儿会加上一个判断,若是已经在获取数据了,就onRefreshComplete(),就是将下拉收起;不然就去开新线程取数据,取完记得也要onRefreshComplete()哦!

 

 

2、上拉加载

若是你不想再费时间去本身写一个上拉加载,不妨试一下PullToRefresh自带的上拉效果哦!

PullToRefresh自己支持下拉刷新和上拉刷新,因此咱们只须要将上拉刷新改为上拉加载就好了。

 

1.设置Mode

 

[java]  view plain copy 在CODE上查看代码片 派生到个人代码片
 
  1. // set mode to BOTH  
  2. mExpandList.setMode(Mode.BOTH);  
  3. mExpandList.getLoadingLayoutProxy(false, true).setPullLabel(getString(R.string.pull_to_load));  
  4. mExpandList.getLoadingLayoutProxy(false, true).setRefreshingLabel(getString(R.string.loading));  
  5. mExpandList.getLoadingLayoutProxy(false, true).setReleaseLabel(getString(R.string.release_to_load));  

Mode设置为Mode.BOTH后,下拉和上拉都会执行onRefresh()中的方法了。

由于界面上边,咱们要显示“下拉刷新”,下边咱们要显示“上拉加载”,因此后三行就是改变下边部分的文字,getLoadingLayoutProxy(false, true)方法你们能够本身感觉一下。

 

 

2.怎么区分下拉/上拉

网上有的同窗是用onScrollListener来判断,这样并不严谨,我依靠是header仍是footer处于可见状态来区分下拉和上拉,若是是下拉,那header必定是可见的;反之,footer必定是可见的。

可是PullToRefreshExpandableListView并无提供这样的接口,那咱们就来小改一下咱们引入的工程吧,很简单:

找到包“com.handmark.pulltorefresh.library”下的PullToRefreshAdapterViewBase.java这个类,加入两个新接口:

 

[java]  view plain copy 在CODE上查看代码片 派生到个人代码片
 
  1. public boolean isHeaderShown() {  
  2.     return getHeaderLayout().isShown();  
  3. }  
  4.   
  5. public boolean isFooterShown() {  
  6.     return getFooterLayout().isShown();  
  7. }  


这样就好了哦,从新编译一下这个工程,和你本身的工程。

 


在onRefresh()中这样来用:

 

[java]  view plain copy 在CODE上查看代码片 派生到个人代码片
 
  1. @Override  
  2. public void onRefresh(PullToRefreshBase<ExpandableListView> refreshView) {  
  3.     if (!isRefreshing) {  
  4.         isRefreshing = true;  
  5.         if (mExpandList.isHeaderShown()) {  
  6.             Utils.LOGD("pull-to-refresh");  
  7.             refreshOnlineStatus(true);  
  8.         } else if (mExpandList.isFooterShown()) {  
  9.             Utils.LOGD("pull-to-load-more");  
  10.             loadNextPage();  
  11.         }  
  12.     } else {  
  13.         mExpandList.onRefreshComplete();  
  14.     }  
  15. }  

 


很简单吧,这样咱们就YD地使用PullToRefresh实现了下拉刷新和上拉加载,LOL,但愿多多少少能帮到你们。

 

=================================================================

更新于2014-07-01

近来发现:

1.实现上拉监听,只须要实现OnRefreshListener2就能够了,同时别忘记setMode(Mode.BOTH) 哦!

2.PullToRefreshListView在使用上有一个BUG,在你的xml layout中,不能一开始将它的visiablity设置为GONE,不然,在代码中设置visiablity为VISIABLE也没有做用。

 

 

 

 

最后放上一张效果图

相关文章
相关标签/搜索