public class PageSplit {
int page = 1;// 請求顯第page頁
int count = 1; // 記錄總數
int currentPage = 1;// 實際顯示currentPage頁
int pageCount = 1;// 總頁面數
int rows = 1; // 每頁顯示的行數
List list = null;
/**
* 構造方法一
*
* @param list準備進行分頁的List對象
*/
public PageSplit(List list) {
super();
this.list = list;
}
/**
* 構造方法二
*
* @param rows
* 每頁顯示的行數
* @param list
* 準備進行分頁的List對象
*/
public PageSplit(int rows, List list) {
super();
this.rows = rows;
this.list = list;
}
/**
* 返回第page頁的所有內容,結果仍為List型
*
* @param page返回第page頁的內容
* @return
*/
public List currentList(int page) {
this.page = page;
currentPage = page;
count = list.size();
pageCount = (count + rows - 1) / rows; // 根據你的紀錄總數,顯示行數,來計算分多少頁
/* 如果請求頁小於1,則讓當前頁面數等於1 */
if (currentPage < 1) {
currentPage = 1;
}
/* 如果請求頁大於總頁數,則讓當前頁面數等於總頁數 */
if (currentPage > pageCount) {
currentPage = pageCount;
}
List newList;
/* 不是滿頁的情況 */
if ((((currentPage - 1) * rows) + rows) > count) {
return newList = list.subList((currentPage - 1) * rows, count);
}
/* 恰巧每頁都滿的情況 */
else {
return newList = list.subList((currentPage - 1) * rows,
((currentPage - 1) * rows) + rows);
}
}
public int getPage() {
return page;
}
public void setPage(int page) {
this.page = page;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getPageCount() {
return pageCount;
}
public void setPageCount(int pageCount) {
this.pageCount = pageCount;
}
public int getRows() {
return rows;
}
public void setRows(int rows) {
this.rows = rows;
}
}