分页公用类PaginationSupport

最近看了 robbin等大侠写的分页代码,深有感触 

robbin.iteye.com/blog/14657

虽然自己也写过分页,但是在代码的分离方面作的还是不够,尝试按照他们的方法改了下

java 代码
  1. package cn.feigme.pagination;   
  2.   
  3. import java.util.List;   
  4.   
  5. /**  
  6.  * @author feigme  
  7.  */  
  8. public class PaginationSupport {   
  9.        
  10.     //默認頁面顯示紀錄的數目   
  11.     public final static int PAGESIZE = 10;   
  12.        
  13.     //頁面實際顯示的紀錄數目   
  14.     private int pageSize = PAGESIZE;   
  15.        
  16.     //頁面上的索引數    
  17.     private int pageIndexCount = 5;   
  18.        
  19.     //總頁數   
  20.     private int pageCount ;   
  21.        
  22.     //紀錄實例集合   
  23.     private List items;   
  24.        
  25.     //總紀錄數   
  26.     private int totalCount;   
  27.        
  28.     //保存當前索引的紀錄數   
  29.     private int[] indexs = new int[0];   
  30.        
  31.     //紀錄起始位   
  32.     private int startIndex = 1;   
  33.        
  34.     /*********************構造函數*************************/  
  35.     //兩參數的構造函數   
  36.     public PaginationSupport(List items,int totalCount){   
  37.         setPageSize(PAGESIZE);   
  38.         setTotalCount(totalCount);   
  39.         setItems(items);   
  40.         setStartIndex(1);   
  41.     }   
  42.        
  43.     //三參數的構造函數   
  44.     public PaginationSupport(List items,int totalCount,int startIndex){   
  45.         setPageSize(PAGESIZE);   
  46.         setTotalCount(totalCount);   
  47.         setItems(items);   
  48.         setStartIndex(startIndex);   
  49.     }   
  50.        
  51.     //四條件的構造函數   
  52.     public PaginationSupport(List items,int totalCount,int startIndex,int pageSize){   
  53.         setPageSize(pageSize);   
  54.         setTotalCount(totalCount);   
  55.         setItems(items);   
  56.         setStartIndex(startIndex);   
  57.     }   
  58.        
  59.     //五條件的構造函數   
  60.     public PaginationSupport(List items,int totalCount,int startIndex,int pageSize,int pageIndexCount){   
  61.         setPageIndexCount(pageIndexCount);   
  62.         setPageSize(pageSize);   
  63.         setTotalCount(totalCount);   
  64.         setItems(items);   
  65.         setStartIndex(startIndex);         
  66.     }   
  67.   
  68.     /***********************get set方法**************************/  
  69.     public int[] getIndexs() {return indexs;}   
  70.     public void setIndexs(int[] indexs) {this.indexs = indexs;}   
  71.   
  72.     public List getItems() {return items;}   
  73.     public void setItems(List items) {this.items = items;}   
  74.   
  75.     public int getPageSize() {return pageSize;}   
  76.     public void setPageSize(int pageSize) {this.pageSize = pageSize;}   
  77.   
  78.     public int getStartIndex() {return startIndex;}   
  79.     public void setStartIndex(int startIndex) {   
  80.         if(startIndex >= this.pageCount) this.startIndex = this.pageCount;   
  81.         else if(startIndex<=0this.startIndex = 1;   
  82.         else this.startIndex = startIndex;   
  83.         if(getPageIndexCount()>this.pageCount){   
  84.             setPageIndexCount(this.pageCount);   
  85.         }   
  86.            
  87.         indexs = new int[getPageIndexCount()];   
  88.         int istart = this.startIndex-getPageIndexCount()/2+(getPageIndexCount()%2>0?0:1);;   
  89.         int iend = this.startIndex+getPageIndexCount()/2;   
  90.         if(istart<=0){   
  91.             istart =1;   
  92.             iend = getPageIndexCount();   
  93.         }   
  94.         if(iend>this.pageCount){   
  95.             iend = this.pageCount;   
  96.             istart = this.pageCount - getPageIndexCount()+1;   
  97.         }   
  98.         for (int i = 0; i < iend-istart+1; i++) {      
  99.              indexs[i]= istart+i;   
  100.         }    
  101.     }   
  102.   
  103.     public int getTotalCount() {return totalCount;}   
  104.     public void setTotalCount(int totalCount) {   
  105.         if(totalCount>0){   
  106.             this.totalCount = totalCount;   
  107.             this.pageCount = totalCount/pageSize + (totalCount%pageSize>0?1:0);   
  108.         }else{   
  109.             this.totalCount = 0;   
  110.         }   
  111.     }   
  112.   
  113.     public int getPageCount() {return pageCount;}   
  114.     public void setPageCount(int pageCount) {this.pageCount = pageCount ;}   
  115.   
  116.     public int getPageIndexCount() {return pageIndexCount;}   
  117.     public void setPageIndexCount(int pageIndexCount) {this.pageIndexCount = pageIndexCount;}   
  118.        
  119.     //下一頁   
  120.     public int getNextIndex() {      
  121.         int nextIndex = getStartIndex() + 1;    
  122.         if (nextIndex > pageCount)      
  123.             return pageCount;      
  124.         else     
  125.             return nextIndex;      
  126.     }      
  127.      
  128.     //上一頁   
  129.     public int getPreviousIndex() {      
  130.         int previousIndex = getStartIndex() - 1;      
  131.         if (previousIndex <= 0)      
  132.             return 1;      
  133.         else     
  134.             return previousIndex;      
  135.     }   
  136.        
  137.     //第一頁   
  138.     public int getFirstIndex(){   
  139.         return 1;   
  140.     }   
  141.        
  142.     //最後一頁   
  143.     public int getLastIndex(){   
  144.         return getPageCount();   
  145.     }   
  146.   
  147. }   

做了个简单的测试

java 代码
  1. public static void main(String[] args) {   
  2.   
  3.         List items = new ArrayList();   
  4.         for (int i = 0; i < 10; i++) {   
  5.             items.add("------------->" + i);   
  6.         }   
  7.   
  8.         int totalCount = 105;   
  9.         int pageSize = 10;   
  10.         int startIndex = 11;   
  11.         int pageIndexCount = 6;   
  12.   
  13.         PaginationSupport ps = new PaginationSupport(items, totalCount,   
  14.                 startIndex, pageSize, pageIndexCount);   
  15.   
  16.         for (int i = 0; i < ps.getItems().size(); i++) {   
  17.             System.out.println(ps.getItems().get(i));   
  18.         }   
  19.         System.out.println("共有" + ps.getTotalCount() + "條紀錄");   
  20.         System.out.println("當前第" + ps.getStartIndex() + "頁");   
  21.         System.out.println("共" + ps.getPageCount() + "頁");   
  22.         System.out.println("第一頁" + ps.getFirstIndex());   
  23.         System.out.println("上一頁" + ps.getPreviousIndex());   
  24.   
  25.         for (int i = 0; i < ps.getIndexs().length; i++) {   
  26.             System.out.println(ps.getIndexs()[i]);   
  27.         }   
  28.   
  29.         System.out.println("下一頁" + ps.getNextIndex());   
  30.         System.out.println("最後一頁" + ps.getLastIndex());   
  31.   
  32.     }  
写的比较匆忙,只把公用类给出来~
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值