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

Android下拉刷新ListView——RTPullListView(demo)

程序员文章站 2023-12-12 19:03:22
下拉刷新在越来越多的app中使用,已经形成一种默认的用户习惯,遇到列表显示的内容时,用户已经开始习惯性的拉拉。在交互习惯上已经形成定性。之前在我的文章《ios学习笔记34—...

下拉刷新在越来越多的app中使用,已经形成一种默认的用户习惯,遇到列表显示的内容时,用户已经开始习惯性的拉拉。在交互习惯上已经形成定性。之前在我的文章《ios学习笔记34—egotableviewpullrefresh实现下拉刷新》中介绍过如何在ios上实现下拉刷新的功能。今天主要介绍下在android上实现下拉刷新的demo,下拉控件参考自github上开源项目pulltorefresh,并做简单修改。最终效果如下:

Android下拉刷新ListView——RTPullListView(demo)                       Android下拉刷新ListView——RTPullListView(demo)

工程结构如下:

Android下拉刷新ListView——RTPullListView(demo)

使用过程中可以对下拉显示效果进行自定义,如果要修改顶部下拉显示,则只需修改pulllist_head.xml即可,修改底部获取更多则修改list_footview.xml即可。

接下来是在activity中使用:

复制代码 代码如下:

/**
* pulllistview
* @author ryan
*
*/
public class rtpulllistviewactivity extends activity {
private static final int internet_failure = -1;
private static final int load_success = 1;
private static final int load_more_success = 3;
private static final int no_more_info = 4;
private static final int load_new_info = 5;

private rtpulllistview pulllistview;
private progressbar moreprogressbar;

private list<string> datalist;
private arrayadapter<string> adapter;

@override
public void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.main);
pulllistview = (rtpulllistview) this.findviewbyid(r.id.pulllistview);
datalist = new arraylist<string>();
for (int i = 0; i < 5; i++) {
datalist.add("item data "+i);
}
adapter = new arrayadapter<string>(this, android.r.layout.simple_list_item_1, datalist);
pulllistview.setadapter(adapter);

//添加listview底部获取更多按钮(可自定义)
layoutinflater inflater = layoutinflater.from(this);
view view = inflater.inflate(r.layout.list_footview, null);
relativelayout footerview =(relativelayout) view.findviewbyid(r.id.list_footview);
moreprogressbar = (progressbar) view.findviewbyid(r.id.footer_progress);
pulllistview.addfooterview(footerview);

//下拉刷新监听器
pulllistview.setonrefreshlistener(new onrefreshlistener() {

@override
public void onrefresh() {
new thread(new runnable() {

@override
public void run() {
try {
//模拟数据刷新
thread.sleep(2000);
datalist.clear();
for (int i = 0; i < 5; i++) {
datalist.add("item data "+i);
}
myhandler.sendemptymessage(load_new_info);
} catch (interruptedexception e) {
e.printstacktrace();
}
}
}).start();
}
});

//获取更多监听器
footerview.setonclicklistener(new onclicklistener() {

@override
public void onclick(view v) {
moreprogressbar.setvisibility(view.visible);

new thread(new runnable() {

@override
public void run() {
try {
//模拟数据刷新
thread.sleep(2000);
for (int i = 0; i < 5; i++) {
datalist.add("new item data "+i);
}
myhandler.sendemptymessage(load_more_success);
} catch (interruptedexception e) {
e.printstacktrace();
}
}
}).start();
}
});
}
//结果处理
private handler myhandler = new handler(){
@override
public void handlemessage(message msg) {
super.handlemessage(msg);
switch (msg.what) {
case load_more_success:
moreprogressbar.setvisibility(view.gone);
adapter.notifydatasetchanged();
pulllistview.setselectionfoot();
break;
case load_new_info:
adapter.notifydatasetchanged();
pulllistview.onrefreshcomplete();
break;
default:
break;
}
}

};
}

具体使用及修改请参考demo源码:

上一篇:

下一篇: