1. 创建一个自定义的Adapter,例如public class NewAdapter extends
BaseAdapter,重写相应的方法。
2. 创建一个分页对象Page,如:
3. 在NewAdapter类中添加Page对象字段和两个List对象newsList和tempList,构造函
数如下:
BaseAdapter,重写相应的方法。
2. 创建一个分页对象Page,如:
public class Page implements Serializable{
private int currentPage; // 当前页
private int totalPage; // 总页数
private int startIndex; // 开始索引,由currentPage和eachPageCount计算出来
private int eachPageCount; // 每页显示的记录数
public Page(int totalPage,int eachPageCount){this(totalPage,1,eachPageCount);}
public Page(int totalPage,int currentPage,int eachPageCount){
this.totalPage = totalPage;
this.currentPage = currentPage;
this.eachPageCount = eachPageCount;
// 下一页的开始索引是上一页的最后一项
this.startIndex = (currentPage - 1)*(eachPageCount-1);
}
//getter和setter省略
public int getStartIndex(){
return (currentPage - 1)*(eachPageCount - 1);
}
public int getEndIndex(){
return startIndex + eachPageCount;
}
}3. 在NewAdapter类中添加Page对象字段和两个List对象newsList和tempList,构造函
数如下:
public NewAdapter(Context context,List newsList,int startIndex){
this.context = context;
tempList = newsList;
// 每页显示4条记录
page = new Page(newsList.size/3,4);
this.newsList = newsList.subList(
page.getStartIndex),page.getEndIndex());
}
本文介绍如何通过创建自定义Adapter实现ListView等组件的数据分页显示。首先定义了一个Page类来管理分页逻辑,然后在Adapter中使用Page对象来控制每次加载的数据范围。
191

被折叠的 条评论
为什么被折叠?



