当前位置: 移动技术网 > IT编程>移动开发>Android > android使用PullToRefresh实现下拉刷新和上拉加载

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

2019年07月24日  | 移动技术网IT编程  | 我要评论

鬼月,8l9864,寄秋小说全集

pulltorefresh是一套实现非常好的下拉刷新库,它支持:

1.listview

2.expandablelistview

3.gridview

4.webview

等多种常用的需要刷新的view类型,而且使用起来也十分方便。

下载完成,将它导入到eclipse中,作为一个library导入到你的工程中就好了。

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

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

<com.handmark.pulltorefresh.library.pulltorefreshexpandablelistview 
  android:id="@+id/expand_list" 
  android:layout_width="match_parent" 
  android:layout_height="match_parent" /> 

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

mexpandlist = (pulltorefreshexpandablelistview) rootview.findviewbyid(r.id.expand_list); 
mexpandlist.getrefreshableview().setgroupindicator(null); 
mexpandlist.getrefreshableview().setdivider(null); 
mexpandlist.getrefreshableview().setselector(android.r.color.transparent); 
mexpandlist.getrefreshableview().setongroupclicklistener(this); 
mexpandlist.setonrefreshlistener(this); 

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

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

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

@override 
public void onrefresh(pulltorefreshbase<expandablelistview> refreshview) { 
  if (!isrefreshing) { 
    isrefreshing = true; 
    updatelist(true); 
  } else { 
    mexpandlist.onrefreshcomplete(); 
  } 
} 

一般来说我们会开另一个线程去获取数据,所以这儿会加上一个判断,如果已经在获取数据了,就onrefreshcomplete(),就是将下拉收起;否则就去开新线程取数据,取完记得也要onrefreshcomplete()哦!

二、上拉加载

如果你不想再费时间去自己写一个上拉加载,不妨试一下pulltorefresh自带的上拉效果哦!

pulltorefresh本身支持下拉刷新和上拉刷新,所以我们只需要将上拉刷新改成上拉加载就行了。

1.设置mode

// set mode to both 
mexpandlist.setmode(mode.both); 
mexpandlist.getloadinglayoutproxy(false, true).setpulllabel(getstring(r.string.pull_to_load)); 
mexpandlist.getloadinglayoutproxy(false, true).setrefreshinglabel(getstring(r.string.loading)); 
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这个类,加入两个新接口:

public boolean isheadershown() { 
  return getheaderlayout().isshown(); 
} 
 
public boolean isfootershown() { 
  return getfooterlayout().isshown(); 
} 

这样就行了哦,重新编译一下这个工程,和你自己的工程。

在onrefresh()中这样来用:

@override 
public void onrefresh(pulltorefreshbase<expandablelistview> refreshview) { 
  if (!isrefreshing) { 
    isrefreshing = true; 
    if (mexpandlist.isheadershown()) { 
      utils.logd("pull-to-refresh"); 
      refreshonlinestatus(true); 
    } else if (mexpandlist.isfootershown()) { 
      utils.logd("pull-to-load-more"); 
      loadnextpage(); 
    } 
  } else { 
    mexpandlist.onrefreshcomplete(); 
  } 
} 

很简单吧,这样我们就yd地使用pulltorefresh实现了下拉刷新和上拉加载,lol,希望多多少少能帮到大家。

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

更新于2014-07-01

近来发现:

1.实现上拉监听,只需要实现onrefreshlistener2就可以了,同时别忘记setmode(mode.both) 哦!

2.pulltorefreshlistview在使用上有一个bug,在你的xml layout中,不能一开始将它的visiablity设置为gone,否则,在代码中设置visiablity为visiable也没有作用。

最后放上一张效果图

demo:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网