欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android ListView下拉刷新上拉自动加载更多DEMO示例

程序员文章站 2024-03-05 10:57:42
代码下载地址已经更新。因为代码很久没更新,已经很落伍了,建议大家使用recyclerview实现。 参考项目: https://github.com/bingoog...

代码下载地址已经更新。因为代码很久没更新,已经很落伍了,建议大家使用recyclerview实现。

参考项目:

https://github.com/bingoogolapple/bgarefreshlayout-android

https://github.com/baoyongzhang/android-pullrefreshlayout

下拉刷新,android中非常普遍的功能。为了方便便重写的listview来实现下拉刷新,同时添加了上拉自动加载更多的功能。设计最初是参考开源中国的android客户端源码。先看示例图。

Android ListView下拉刷新上拉自动加载更多DEMO示例Android ListView下拉刷新上拉自动加载更多DEMO示例

图1 图2

Android ListView下拉刷新上拉自动加载更多DEMO示例Android ListView下拉刷新上拉自动加载更多DEMO示例

图3 图4

Android ListView下拉刷新上拉自动加载更多DEMO示例Android ListView下拉刷新上拉自动加载更多DEMO示例

图5 图6

下拉刷新的时动画效果: 图1 ==》 图2 ==》 图3 ==》 图4 ==》 图1。

上拉自动加载更多的效果:图5

图6是示例demo的截图

重写后的listview动画效果来源于添加的头部(header)和尾部(footer),listview提供了addheaderview和addfooterview方法来添加header和footer。大家也可以通过修改头部、尾部的xml文件来定义自己的动画效果。

实现原理

1.下拉刷新

通过ontouchevent判断手势,来改变listview的header。header的状态共4种,自己定义为:

none(对应图1):初始状态

pull(对应图2):下拉状态,此时松开会还原到状态none,并不进行刷新

release(对应图3):同样是下拉状态,但此刻松开会执行刷新,进入状态refreshing

refreshing(对应图4):正在执行刷新,刷新结束后进入状态none。

header在四种状态切换时不仅改变内部组件,同时改变自身的大小。改变内部组件的体现比如,箭头的朝上或者朝下,文字提示的变化,等待圆圈的显示与否。大小的改变其实就是高度的改变,none时header高度为0,release时header的高度由你下拉的程度决定。

2.加载更多

在listview滑动停止后,判断listview的最后一个item是否已经显示,如果显示说明listview已经滑动到了最底部,这时便触发加载更多的方法,方法结束根据结果改变footer。

3.回调方法

在类中定义了两个接口onrefreshlistener和onloadlistener,用来定义和提供加载数据的方法,具体实现则交给它们的实现类去做。

package com.example.autolistview.widget; 
import com.example.autolistview.r; 
import com.example.autolistview.utils.utils; 
import android.content.context; 
import android.util.attributeset; 
import android.view.layoutinflater; 
import android.view.motionevent; 
import android.view.view; 
import android.view.viewgroup; 
import android.view.animation.linearinterpolator; 
import android.view.animation.rotateanimation; 
import android.widget.abslistview; 
import android.widget.imageview; 
import android.widget.progressbar; 
import android.widget.textview; 
import android.widget.abslistview.onscrolllistener; 
import android.widget.listview; 
/** 
* @author sunnycoffee 
* @create 2013-10-24 
* @version 1.0 
* @desc 自定义listview 下拉刷新,上拉加载更多 
*/ 
public class autolistview extends listview implements onscrolllistener { 
// 区分当前操作是刷新还是加载 
public static final int refresh = 0; 
public static final int load = 1; 
// 区分pull和release的距离的大小 
private static final int space = 20; 
// 定义header的四种状态和当前状态 
private static final int none = 0; 
private static final int pull = 1; 
private static final int release = 2; 
private static final int refreshing = 3; 
private int state; 
private layoutinflater inflater; 
private view header; 
private view footer; 
private textview tip; 
private textview lastupdate; 
private imageview arrow; 
private progressbar refreshing; 
private textview nodata; 
private textview loadfull; 
private textview more; 
private progressbar loading; 
private rotateanimation animation; 
private rotateanimation reverseanimation; 
private int starty; 
private int firstvisibleitem; 
private int scrollstate; 
private int headercontentinitialheight; 
private int headercontentheight; 
// 只有在listview第一个item显示的时候(listview滑到了顶部)才进行下拉刷新, 否则此时的下拉只是滑动listview 
private boolean isrecorded; 
private boolean isloading;// 判断是否正在加载 
private boolean loadenable = true;// 开启或者关闭加载更多功能 
private boolean isloadfull; 
private int pagesize = 10; 
private onrefreshlistener onrefreshlistener; 
private onloadlistener onloadlistener; 
public autolistview(context context) { 
super(context); 
initview(context); 
} 
public autolistview(context context, attributeset attrs) { 
super(context, attrs); 
initview(context); 
} 
public autolistview(context context, attributeset attrs, int defstyle) { 
super(context, attrs, defstyle); 
initview(context); 
} 
// 下拉刷新监听 
public void setonrefreshlistener(onrefreshlistener onrefreshlistener) { 
this.onrefreshlistener = onrefreshlistener; 
} 
// 加载更多监听 
public void setonloadlistener(onloadlistener onloadlistener) { 
this.loadenable = true; 
this.onloadlistener = onloadlistener; 
} 
public boolean isloadenable() { 
return loadenable; 
} 
// 这里的开启或者关闭加载更多,并不支持动态调整 
public void setloadenable(boolean loadenable) { 
this.loadenable = loadenable; 
this.removefooterview(footer); 
} 
public int getpagesize() { 
return pagesize; 
} 
public void setpagesize(int pagesize) { 
this.pagesize = pagesize; 
} 
// 初始化组件 
private void initview(context context) { 
// 设置箭头特效 
animation = new rotateanimation(0, -180, 
rotateanimation.relative_to_self, 0.5f, 
rotateanimation.relative_to_self, 0.5f); 
animation.setinterpolator(new linearinterpolator()); 
animation.setduration(100); 
animation.setfillafter(true); 
reverseanimation = new rotateanimation(-180, 0, 
rotateanimation.relative_to_self, 0.5f, 
rotateanimation.relative_to_self, 0.5f); 
reverseanimation.setinterpolator(new linearinterpolator()); 
reverseanimation.setduration(100); 
reverseanimation.setfillafter(true); 
inflater = layoutinflater.from(context); 
footer = inflater.inflate(r.layout.listview_footer, null); 
loadfull = (textview) footer.findviewbyid(r.id.loadfull); 
nodata = (textview) footer.findviewbyid(r.id.nodata); 
more = (textview) footer.findviewbyid(r.id.more); 
loading = (progressbar) footer.findviewbyid(r.id.loading); 
header = inflater.inflate(r.layout.pull_to_refresh_header, null); 
arrow = (imageview) header.findviewbyid(r.id.arrow); 
tip = (textview) header.findviewbyid(r.id.tip); 
lastupdate = (textview) header.findviewbyid(r.id.lastupdate); 
refreshing = (progressbar) header.findviewbyid(r.id.refreshing); 
// 为listview添加头部和尾部,并进行初始化 
headercontentinitialheight = header.getpaddingtop(); 
measureview(header); 
headercontentheight = header.getmeasuredheight(); 
toppadding(-headercontentheight); 
this.addheaderview(header); 
this.addfooterview(footer); 
this.setonscrolllistener(this); 
} 
public void onrefresh() { 
if (onrefreshlistener != null) { 
onrefreshlistener.onrefresh(); 
} 
} 
public void onload() { 
if (onloadlistener != null) { 
onloadlistener.onload(); 
} 
} 
public void onrefreshcomplete(string updatetime) { 
lastupdate.settext(this.getcontext().getstring(r.string.lastupdatetime, 
lastupdate)); 
state = none; 
refreshheaderviewbystate(); 
} 
// 用于下拉刷新结束后的回调 
public void onrefreshcomplete() { 
string currenttime = utils.getcurrenttime(); 
onrefreshcomplete(currenttime); 
} 
// 用于加载更多结束后的回调 
public void onloadcomplete() { 
isloading = false; 
} 
@override 
public void onscroll(abslistview view, int firstvisibleitem, 
int visibleitemcount, int totalitemcount) { 
this.firstvisibleitem = firstvisibleitem; 
} 
@override 
public void onscrollstatechanged(abslistview view, int scrollstate) { 
this.scrollstate = scrollstate; 
ifneedload(view, scrollstate); 
} 
// 根据listview滑动的状态判断是否需要加载更多 
private void ifneedload(abslistview view, int scrollstate) { 
if (!loadenable) { 
return; 
} 
try { 
if (scrollstate == onscrolllistener.scroll_state_idle 
&& !isloading 
&& view.getlastvisibleposition() == view 
.getpositionforview(footer) && !isloadfull) { 
onload(); 
isloading = true; 
} 
} catch (exception e) { 
} 
} 
/** 
* 监听触摸事件,解读手势 
*/ 
@override 
public boolean ontouchevent(motionevent ev) { 
switch (ev.getaction()) { 
case motionevent.action_down: 
if (firstvisibleitem == 0) { 
isrecorded = true; 
starty = (int) ev.gety(); 
} 
break; 
case motionevent.action_cancel: 
case motionevent.action_up: 
if (state == pull) { 
state = none; 
refreshheaderviewbystate(); 
} else if (state == release) { 
state = refreshing; 
refreshheaderviewbystate(); 
onrefresh(); 
} 
isrecorded = false; 
break; 
case motionevent.action_move: 
whenmove(ev); 
break; 
} 
return super.ontouchevent(ev); 
} 
// 解读手势,刷新header状态 
private void whenmove(motionevent ev) { 
if (!isrecorded) { 
return; 
} 
int tmpy = (int) ev.gety(); 
int space = tmpy - starty; 
int toppadding = space - headercontentheight; 
switch (state) { 
case none: 
if (space > 0) { 
state = pull; 
refreshheaderviewbystate(); 
} 
break; 
case pull: 
toppadding(toppadding); 
if (scrollstate == scroll_state_touch_scroll 
&& space > headercontentheight + space) { 
state = release; 
refreshheaderviewbystate(); 
} 
break; 
case release: 
toppadding(toppadding); 
if (space > 0 && space < headercontentheight + space) { 
state = pull; 
refreshheaderviewbystate(); 
} else if (space <= 0) { 
state = none; 
refreshheaderviewbystate(); 
} 
break; 
} 
} 
// 调整header的大小。其实调整的只是距离顶部的高度。 
private void toppadding(int toppadding) { 
header.setpadding(header.getpaddingleft(), toppadding, 
header.getpaddingright(), header.getpaddingbottom()); 
header.invalidate(); 
} 
/** 
* 这个方法是根据结果的大小来决定footer显示的。 
* <p> 
* 这里假定每次请求的条数为10。如果请求到了10条。则认为还有数据。如过结果不足10条,则认为数据已经全部加载,这时footer显示已经全部加载 
* </p> 
* 
* @param resultsize 
*/ 
public void setresultsize(int resultsize) { 
if (resultsize == 0) { 
isloadfull = true; 
loadfull.setvisibility(view.gone); 
loading.setvisibility(view.gone); 
more.setvisibility(view.gone); 
nodata.setvisibility(view.visible); 
} else if (resultsize > 0 && resultsize < pagesize) { 
isloadfull = true; 
loadfull.setvisibility(view.visible); 
loading.setvisibility(view.gone); 
more.setvisibility(view.gone); 
nodata.setvisibility(view.gone); 
} else if (resultsize == pagesize) { 
isloadfull = false; 
loadfull.setvisibility(view.gone); 
loading.setvisibility(view.visible); 
more.setvisibility(view.visible); 
nodata.setvisibility(view.gone); 
} 
} 
// 根据当前状态,调整header 
private void refreshheaderviewbystate() { 
switch (state) { 
case none: 
toppadding(-headercontentheight); 
tip.settext(r.string.pull_to_refresh); 
refreshing.setvisibility(view.gone); 
arrow.clearanimation(); 
arrow.setimageresource(r.drawable.pull_to_refresh_arrow); 
break; 
case pull: 
arrow.setvisibility(view.visible); 
tip.setvisibility(view.visible); 
lastupdate.setvisibility(view.visible); 
refreshing.setvisibility(view.gone); 
tip.settext(r.string.pull_to_refresh); 
arrow.clearanimation(); 
arrow.setanimation(reverseanimation); 
break; 
case release: 
arrow.setvisibility(view.visible); 
tip.setvisibility(view.visible); 
lastupdate.setvisibility(view.visible); 
refreshing.setvisibility(view.gone); 
tip.settext(r.string.pull_to_refresh); 
tip.settext(r.string.release_to_refresh); 
arrow.clearanimation(); 
arrow.setanimation(animation); 
break; 
case refreshing: 
toppadding(headercontentinitialheight); 
refreshing.setvisibility(view.visible); 
arrow.clearanimation(); 
arrow.setvisibility(view.gone); 
tip.setvisibility(view.gone); 
lastupdate.setvisibility(view.gone); 
break; 
} 
} 
// 用来计算header大小的。比较隐晦。 
private void measureview(view child) { 
viewgroup.layoutparams p = child.getlayoutparams(); 
if (p == null) { 
p = new viewgroup.layoutparams(viewgroup.layoutparams.match_parent, 
viewgroup.layoutparams.wrap_content); 
} 
int childwidthspec = viewgroup.getchildmeasurespec(0, 0 + 0, p.width); 
int lpheight = p.height; 
int childheightspec; 
if (lpheight > 0) { 
childheightspec = measurespec.makemeasurespec(lpheight, 
measurespec.exactly); 
} else { 
childheightspec = measurespec.makemeasurespec(0, 
measurespec.unspecified); 
} 
child.measure(childwidthspec, childheightspec); 
} 
/* 
* 定义下拉刷新接口 
*/ 
public interface onrefreshlistener { 
public void onrefresh(); 
} 
/* 
* 定义加载更多接口 
*/ 
public interface onloadlistener { 
public void onload(); 
} 
}

这个类的逻辑复杂的地方就是header四种状态的判断和切换。

以下是几种状态的切换情况

none ==》 pull
none 《== pull ==》 release
pull 《== release ==》 refreshing
refreshing ==》 none

代码中对于 release ==》pull 状态的切换处理的不太理想,好像是纵坐标的记录方式有问题,如果有谁解决,希望能够留言告知。

为了减少篇幅,其他代码就不在贴了。

注意:定义header的xml最外层必须使用线性布局,不然的话会出错。

以上所述是小编给大家介绍的android listview下拉刷新上拉自动加载更多demo示例,希望对大家有所帮助