List集合分页显示

Posted 海洋svs

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了List集合分页显示相关的知识,希望对你有一定的参考价值。

package com.mshc.util;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/** 
 * @author svs: 
 * @version 创建时间:2016-12-16 下午02:28:23 
 * 类说明 
 */
public class ListPageUtil<T> {
    /**原集合*/
    private List<T> data;
 
    /** 上一页 */
    private int lastPage;

    /** 当前页 */
    private int nowPage;

    /** 下一页 */
    private int nextPage;
//    
    /** 每页条数 */
    private int pageSize;

    /** 总页数 */
    private int totalPage;

    /** 总数据条数 */
    private int totalCount;
    
    public ListPageUtil(List<T> data,int nowPage,int pageSize) {
        if (data == null || data.isEmpty()) {
            throw new IllegalArgumentException("data must be not empty!");
        }
 
        this.data = data;
        this.pageSize = pageSize;
        /*this.totalPage = data.size()/pageSize;
        if(data.size()%pageSize!=0){
            this.totalPage++;
        }*/
        
        this.nowPage = nowPage;
        this.totalCount = data.size();
        this.totalPage = (totalCount + pageSize - 1) / pageSize;
        this.lastPage = nowPage-1>1? nowPage-1:1;
        this.nextPage = nowPage>=totalPage? totalPage: nowPage + 1;
        
    }
 
    /**
     * 得到分页后的数据
     *
     * @param pageNum 页码
     * @return 分页后结果
     */
    public List<T> getPagedList() {
        int fromIndex = (nowPage - 1) * pageSize;
        if (fromIndex >= data.size()) {
            return Collections.emptyList();//空数组
        }
        if(fromIndex<0){
            return Collections.emptyList();//空数组
        }
        int toIndex = nowPage * pageSize;
        if (toIndex >= data.size()) {
            toIndex = data.size();
        }
        return data.subList(fromIndex, toIndex);
    }
 
    public int getPageSize() {
        return pageSize;
    }
 
    public List<T> getData() {
        return data;
    }
    public int getLastPage() {
        return lastPage;
    }

    public int getNowPage() {
        return nowPage;
    }

    public int getNextPage() {
        return nextPage;
    }

    public int getTotalPage() {
        return totalPage;
    }

    public int getTotalCount() {
        return totalCount;
    }

   public static void main(String[] args) {
        Integer[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
        List<Integer> list = Arrays.asList(array);
 
       // ListPageUtil<Integer> pager = new ListPageUtil<Integer>(list, 10);
        ListPageUtil<Integer> listPageUtil = new ListPageUtil<Integer>(list,4, 5);
        List<Integer> pagedList = listPageUtil.getPagedList();
        System.out.println(pagedList);
    }
}

 

以上是关于List集合分页显示的主要内容,如果未能解决你的问题,请参考以下文章

TP5报如下的错误 Indirect modification of overloaded element of thinkpaginatorCollection has no effect(代码片段

List集合分页工具类

分页离线条件查询 页面响应500 后端未报异常 list集合有数据

List集合分页

黑马day13 分页思路&amp;实现

FragmentStatePagerAdapter 视图分页器片段在活动重新创建后不显示