分页显示数据时常常需要将页码列出来。
此处的Page类主要关注分页显示时候的页码的显示。
一. Page类
页码类如下:
import java.util.ArrayList; import java.util.List; public class Page { private boolean hasNext; private boolean hasPer; private int curPage; private int countPage; private int showPageNum;//显示出来的页码数量 private List<Integer> pageList; public void setShowPageNum(int showPageNum) { this.showPageNum = showPageNum; } public void setHasNext(boolean hasNext) { this.hasNext = hasNext; } public void setPageList(List<Integer> pageList) { this.pageList = pageList; } public List<Integer> getPageList() { pageList=new ArrayList(); if(curPage<showPageNum){ for(int i=1;i<=(countPage>showPageNum?showPageNum:countPage);i++){ pageList.add(i); } }else{ if(curPage-(showPageNum%2==0?showPageNum/2-1:showPageNum/2)>1 && curPage+showPageNum/2<=countPage){ for(int i=curPage-(showPageNum%2==0?showPageNum/2-1:showPageNum/2);i<=curPage+showPageNum/2;i++){ pageList.add(i); } }else if(curPage+showPageNum/2>countPage){ for(int i=countPage-showPageNum+1;i<=countPage;i++){ pageList.add(i); } } } return pageList; } public boolean isHasNext() { if(curPage+1>countPage){ hasNext=false; }else{ hasNext=true; } return hasNext; } public boolean isHasPer() { if(curPage-1<=0){ hasPer=false; }else{ hasPer=true; } return hasPer; } public void setHasPer(boolean hasPer) { this.hasPer = hasPer; } public int getCurPage() { return curPage; } public void setCurPage(int curPage) { this.curPage = curPage; } public int getCountPage() { return countPage; } public void setCountPage(int countPage) { this.countPage = countPage; }
二. 实验
以下实验需要显示设置总页数为12页。
设置当前页从1~12。以此查看查看得到的需要显示的页码列表。
1. 如果显示页码数为奇数
setShowPageNum(5) 显示5个页码
如下例:
public static void main(String[] args) { Page page=new Page(); page.setCountPage(12); page.setShowPageNum(5); for(int i=1;i<=page.getCountPage();i++){ page.setCurPage(i); System.out.print("当前页为第"+i+"页:\t"); System.out.println(page.getPageList()); } }
则输出:
当前页为第1页: [1, 2, 3, 4, 5]
当前页为第2页: [1, 2, 3, 4, 5]
当前页为第3页: [1, 2, 3, 4, 5]
当前页为第4页: [1, 2, 3, 4, 5]
当前页为第5页: [3, 4, 5, 6, 7]
当前页为第6页: [4, 5, 6, 7, 8]
当前页为第7页: [5, 6, 7, 8, 9]
当前页为第8页: [6, 7, 8, 9, 10]
当前页为第9页: [7, 8, 9, 10, 11]
当前页为第10页:[8, 9, 10, 11, 12]
当前页为第11页:[8, 9, 10, 11, 12]
当前页为第12页:[8, 9, 10, 11, 12]
2. 如果显示页码数为偶数
setShowPageNum(6) 显示6个页码
如下例:
public static void main(String[] args) { Page page=new Page(); page.setCountPage(12); page.setShowPageNum(6); for(int i=1;i<=page.getCountPage();i++){ page.setCurPage(i); System.out.print("当前页为第"+i+"页:\t"); System.out.println(page.getPageList()); } }
则输出:
当前页为第1页: [1, 2, 3, 4, 5, 6]
当前页为第2页: [1, 2, 3, 4, 5, 6]
当前页为第3页: [1, 2, 3, 4, 5, 6]
当前页为第4页: [1, 2, 3, 4, 5, 6]
当前页为第5页: [1, 2, 3, 4, 5, 6]
当前页为第6页: [4, 5, 6, 7, 8, 9]
当前页为第7页: [5, 6, 7, 8, 9, 10]
当前页为第8页: [6, 7, 8, 9, 10, 11]
当前页为第9页: [7, 8, 9, 10, 11, 12]
当前页为第10页:[7, 8, 9, 10, 11, 12]
当前页为第11页:[7, 8, 9, 10, 11, 12]
当前页为第12页:[7, 8, 9, 10, 11, 12]
3. 如果需要总页数小于需要显示页码数量
setCountPage(4) 总页数为4。
setShowPageNum(6) 显示4个页码。
如下例:
public static void main(String[] args) { Page page=new Page(); page.setCountPage(4); page.setShowPageNum(6); for(int i=1;i<=page.getCountPage();i++){ page.setCurPage(i); System.out.print("当前页为第"+i+"页:\t"); System.out.println(page.getPageList()); } }
则输出:
当前页为第1页: [1, 2, 3, 4]
当前页为第2页: [1, 2, 3, 4]
当前页为第3页: [1, 2, 3, 4]
当前页为第4页: [1, 2, 3, 4]