针对list集合进行分页展示

直接定义个工具类,代码如下:

  1 package com.jk51.modules.wechat.web.util;
  2 
  3 import java.util.Collections;
  4 import java.util.List;
  5 
  6 /**
  7  * 版权所有(C) 2018  9  * 作者: chen
 10  * 创建日期: 2018/6/20
 11  * 修改记录:
 12  */
 13 public class ListPageUtil<T> {
 14     private List<T> data;
 15 
 16     /** 上一页 */
 17     private int lastPage;
 18 
 19     /** 当前页 */
 20     private int currentPage;
 21 
 22     /** 下一页 */
 23     private int nextPage;
 24 //
 25     /** 每页条数 */
 26     private int pageSize;
 27 
 28     /** 总页数 */
 29     private int totalPage;
 30 
 31     /** 总数据条数 */
 32     private int totalCount;
 33 
 34     public ListPageUtil(List<T> data,int currentPage,int pageSize) {
 35         if (data == null || data.isEmpty()) {
 36             throw new IllegalArgumentException("data must be not empty!");
 37         }
 38 
 39         this.data = data;
 40         this.pageSize = pageSize;
 41         this.currentPage = currentPage;
 42         this.totalCount = data.size();
 43         this.totalPage = (totalCount + pageSize - 1) / pageSize;
 44         this.lastPage = currentPage-1>1? currentPage-1:1;
 45         this.nextPage = currentPage>=totalPage? totalPage: currentPage + 1;
 46 
 47     }
 48 
 49     /**
 50      * 得到分页后的数据
 51      * @return 分页后结果
 52      */
 53 //    public List<T> getPagedLst() {
 54 //        int fromIndex = (nowPage - 1) * pageSize;
 55 //        if (fromIndex >= data.size()) {
 56 //            return Collections.emptyList();//空数组
 57 //        }
 58 //        if(fromIndex<0){
 59 //            return Collections.emptyList();//空数组
 60 //        }
 61 //        int toIndex = nowPage * pageSize;
 62 //        if (toIndex >= data.size()) {
 63 //            toIndex = data.size();
 64 //        }
 65 //        return data.subList(fromIndex, toIndex);
 66 //    }
 67 
 68     public int getPageSize() {
 69         return pageSize;
 70     }
 71 
 72     public List<T> getData() {
 73         int fromIndex = (currentPage - 1) * pageSize;
 74         if (fromIndex >= data.size()) {
 75             return Collections.emptyList();//空数组
 76         }
 77         if(fromIndex<0){
 78             return Collections.emptyList();//空数组
 79         }
 80         int toIndex = currentPage * pageSize;
 81         if (toIndex >= data.size()) {
 82             toIndex = data.size();
 83         }
 84         return data.subList(fromIndex, toIndex);
 85     }
 86     public int getLastPage() {
 87         return lastPage;
 88     }
 89 
 90     public int getCurrentPage() {
 91         return currentPage;
 92     }
 93 
 94     public int getNextPage() {
 95         return nextPage;
 96     }
 97 
 98     public int getTotalPage() {
 99         return totalPage;
100     }
101 
102     public int getTotalCount() {
103         return totalCount;
104     }
105 }

 

posted @ 2018-06-20 15:56  不二尘  阅读(7447)  评论(0编辑  收藏  举报