ListView 分页加载更新实例分享

时间:2022-02-22 07:08:46

listview是android中最常用的控件之一。

在实际运用中往往会遇到一次性加载全部数据过多,需要分页加载增加程序运行效率!
本demo是将更新的监听放在listview的foot中,通过底部按钮点击触发或者通过滑动事件监听,当滑动到列表最底部的时候触发。
有图有真像:
列表初始状态
ListView 分页加载更新实例分享 
触发加载
ListView 分页加载更新实例分享 
加载后
ListView 分页加载更新实例分享 
部分代码:
mainactivity

复制代码 代码如下:


private void initactivity() {
// todo auto-generated method stub
loadmoreview = getlayoutinflater().inflate(r.layout.loadmore, null);
loadmore_btn = (button) loadmoreview.findviewbyid(r.id.loadmore_btn);
loadmore_btn.setonclicklistener(new onclicklistener() {
@override
public void onclick(view v) {
// todo auto-generated method stub
doupdate();
}
});
listview = (listview) findviewbyid(r.id.listview);
listview.addfooterview(loadmoreview);
initadapter();
listview.setadapter(adapter);
listview.setonscrolllistener(new onscrolllistener() {
@override
public void onscrollstatechanged(abslistview view, int scrollstate) {
// todo auto-generated method stub
int itemslastindex = adapter.getcount() - 1;
int lastindex = itemslastindex + 1;
if (scrollstate == onscrolllistener.scroll_state_idle
&& visiblelast == lastindex) {
// 可在这添加滑到列表底的时候异步自动加载
// doupdate();
}
}
@override
public void onscroll(abslistview view, int firstvisibleitem,
int visibleitemcount, int totalitemcount) {
// todo auto-generated method stub
visiblecount = visibleitemcount;
visiblelast = firstvisibleitem + visibleitemcount - 1;
}
});
}
protected void doupdate() {
// todo auto-generated method stub
loadmore_btn.settext("loading ...");
handler.postdelayed(new runnable() {
@override
public void run() {
// todo auto-generated method stub
loaddata();
adapter.notifydatasetchanged();// 通知adapter数据变化
listview.setselection(visiblelast - visiblecount + 1);
loadmore_btn.settext("load more");
}
}, 2000);
}
private void loaddata() {
// todo auto-generated method stub
int count = adapter.getcount();
for (int i = count; i < count + 10; i++) {
adapter.additem(string.valueof(i + 1));
}
}
private void initadapter() {
// todo auto-generated method stub
list<string> items = new arraylist<string>();
for (int i = 0; i < 10; i++) {
items.add(string.valueof(i + 1));
}
adapter = new listadapter(this, items);
}
@override
public boolean oncreateoptionsmenu(menu menu) {
// inflate the menu; this adds items to the action bar if it is present.
getmenuinflater().inflate(r.menu.activity_main, menu);
return true;
}


listadapter

复制代码 代码如下:


public listadapter(context context, list<string> items) {
this.items = items;
this.inflater = (layoutinflater) context
.getsystemservice(context.layout_inflater_service);
}
@override
public int getcount() {
// todo auto-generated method stub
return items.size();
}
@override
public object getitem(int position) {
// todo auto-generated method stub
return items.get(position);
}
@override
public long getitemid(int position) {
// todo auto-generated method stub
return position;
}
@override
public view getview(int position, view convertview, viewgroup parent) {
// todo auto-generated method stub
if (convertview == null) {
convertview = inflater.inflate(r.layout.list_item, null);
}
textview textview = (textview) convertview
.findviewbyid(r.id.list_item_text);
textview.settext(items.get(position));
return convertview;
}
/**
* 添加列表项
* @param item
*/
public void additem(string item){
items.add(item);
}


提醒一下:要下载android-support-v4.jar 这个第三方的jar包哦! 这里就不提供下载了!