`
handrenliang
  • 浏览: 33692 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

如何对一个ListView进行分页显示

 
阅读更多
1. 创建一个自定义的Adapter,例如public class NewAdapter extends
   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());
   }
1
1
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics