最近看了 robbin等大侠写的分页代码,深有感触
虽然自己也写过分页,但是在代码的分离方面作的还是不够,尝试按照他们的方法改了下
java 代码
- package cn.feigme.pagination;
- import java.util.List;
- /**
- * @author feigme
- */
- public class PaginationSupport {
- //默認頁面顯示紀錄的數目
- public final static int PAGESIZE = 10;
- //頁面實際顯示的紀錄數目
- private int pageSize = PAGESIZE;
- //頁面上的索引數
- private int pageIndexCount = 5;
- //總頁數
- private int pageCount ;
- //紀錄實例集合
- private List items;
- //總紀錄數
- private int totalCount;
- //保存當前索引的紀錄數
- private int[] indexs = new int[0];
- //紀錄起始位
- private int startIndex = 1;
- /*********************構造函數*************************/
- //兩參數的構造函數
- public PaginationSupport(List items,int totalCount){
- setPageSize(PAGESIZE);
- setTotalCount(totalCount);
- setItems(items);
- setStartIndex(1);
- }
- //三參數的構造函數
- public PaginationSupport(List items,int totalCount,int startIndex){
- setPageSize(PAGESIZE);
- setTotalCount(totalCount);
- setItems(items);
- setStartIndex(startIndex);
- }
- //四條件的構造函數
- public PaginationSupport(List items,int totalCount,int startIndex,int pageSize){
- setPageSize(pageSize);
- setTotalCount(totalCount);
- setItems(items);
- setStartIndex(startIndex);
- }
- //五條件的構造函數
- public PaginationSupport(List items,int totalCount,int startIndex,int pageSize,int pageIndexCount){
- setPageIndexCount(pageIndexCount);
- setPageSize(pageSize);
- setTotalCount(totalCount);
- setItems(items);
- setStartIndex(startIndex);
- }
- /***********************get set方法**************************/
- public int[] getIndexs() {return indexs;}
- public void setIndexs(int[] indexs) {this.indexs = indexs;}
- public List getItems() {return items;}
- public void setItems(List items) {this.items = items;}
- public int getPageSize() {return pageSize;}
- public void setPageSize(int pageSize) {this.pageSize = pageSize;}
- public int getStartIndex() {return startIndex;}
- public void setStartIndex(int startIndex) {
- if(startIndex >= this.pageCount) this.startIndex = this.pageCount;
- else if(startIndex<=0) this.startIndex = 1;
- else this.startIndex = startIndex;
- if(getPageIndexCount()>this.pageCount){
- setPageIndexCount(this.pageCount);
- }
- indexs = new int[getPageIndexCount()];
- int istart = this.startIndex-getPageIndexCount()/2+(getPageIndexCount()%2>0?0:1);;
- int iend = this.startIndex+getPageIndexCount()/2;
- if(istart<=0){
- istart =1;
- iend = getPageIndexCount();
- }
- if(iend>this.pageCount){
- iend = this.pageCount;
- istart = this.pageCount - getPageIndexCount()+1;
- }
- for (int i = 0; i < iend-istart+1; i++) {
- indexs[i]= istart+i;
- }
- }
- public int getTotalCount() {return totalCount;}
- public void setTotalCount(int totalCount) {
- if(totalCount>0){
- this.totalCount = totalCount;
- this.pageCount = totalCount/pageSize + (totalCount%pageSize>0?1:0);
- }else{
- this.totalCount = 0;
- }
- }
- public int getPageCount() {return pageCount;}
- public void setPageCount(int pageCount) {this.pageCount = pageCount ;}
- public int getPageIndexCount() {return pageIndexCount;}
- public void setPageIndexCount(int pageIndexCount) {this.pageIndexCount = pageIndexCount;}
- //下一頁
- public int getNextIndex() {
- int nextIndex = getStartIndex() + 1;
- if (nextIndex > pageCount)
- return pageCount;
- else
- return nextIndex;
- }
- //上一頁
- public int getPreviousIndex() {
- int previousIndex = getStartIndex() - 1;
- if (previousIndex <= 0)
- return 1;
- else
- return previousIndex;
- }
- //第一頁
- public int getFirstIndex(){
- return 1;
- }
- //最後一頁
- public int getLastIndex(){
- return getPageCount();
- }
- }
做了个简单的测试
java 代码
- public static void main(String[] args) {
- List items = new ArrayList();
- for (int i = 0; i < 10; i++) {
- items.add("------------->" + i);
- }
- int totalCount = 105;
- int pageSize = 10;
- int startIndex = 11;
- int pageIndexCount = 6;
- PaginationSupport ps = new PaginationSupport(items, totalCount,
- startIndex, pageSize, pageIndexCount);
- for (int i = 0; i < ps.getItems().size(); i++) {
- System.out.println(ps.getItems().get(i));
- }
- System.out.println("共有" + ps.getTotalCount() + "條紀錄");
- System.out.println("當前第" + ps.getStartIndex() + "頁");
- System.out.println("共" + ps.getPageCount() + "頁");
- System.out.println("第一頁" + ps.getFirstIndex());
- System.out.println("上一頁" + ps.getPreviousIndex());
- for (int i = 0; i < ps.getIndexs().length; i++) {
- System.out.println(ps.getIndexs()[i]);
- }
- System.out.println("下一頁" + ps.getNextIndex());
- System.out.println("最後一頁" + ps.getLastIndex());
- }