分页功能实现

  • 页面列出条目的时候需要分页,以前总是在方法里进行分页的处理,比较乱也不好重复使用,现在将其抽象出来.实现的效果和百度相仿.
    private int nowpage;// 当前页
private int countrecord;// 总记录
private int countpage;// 总页数

public static final int PAGESIZE = 16;// 每页显示的记录数

private int startpage;// 页面中的起始页
private int endpage;// 页面中的结束页

private final int SHOWPAGE = 6;// 页面中显示的总页数 baidu显示的是10



/** 根据当前页及总记录数来构造分页对象 */
public Pagination(int nowpage, int countrecord) {
	this.nowpage = nowpage;
	this.countrecord = countrecord;

	/** 计算总页数 */
	this.countpage = this.countrecord % this.PAGESIZE == 0 ? this.countrecord
			/ this.PAGESIZE
			: this.countrecord / this.PAGESIZE + 1;
	/** 计算startpage与endpage的值 */

	/** 总页数数是否小于6 */
	if (this.countpage < this.SHOWPAGE ) {
		this.startpage = 1; // 页面中起始页就是1
		this.endpage = this.countpage;// 页面中的最终页就是总页数
	} else {
		/** else中是总页数大于4的情况 */

		/** 首先当前页的值是否小于等于6/2+1 */
		if (this.nowpage <= (this.SHOWPAGE / 2)) {
			this.startpage = 1;
			this.endpage = this.SHOWPAGE;
		} else {
			this.startpage = this.nowpage - this.SHOWPAGE / 2;
			this.endpage = this.nowpage + this.SHOWPAGE / 2-1;

			if (this.endpage >= this.countpage) {//当页数接近末尾时,总页数可能小于这个值,因此要重新判断
				this.endpage = this.countpage;
				this.startpage = this.countpage-SHOWPAGE+1;

			}
		}

	}

}

public int getNowpage() {
	return nowpage;
}

public void setNowpage(int nowpage) {
	this.nowpage = nowpage;
}

public int getCountrecord() {
	return countrecord;
}

public void setCountrecord(int countrecord) {
	this.countrecord = countrecord;
}

public int getCountpage() {
	return countpage;
}

public void setCountpage(int countpage) {
	this.countpage = countpage;
}

public int getStartpage() {
	return startpage;
}

public void setStartpage(int startpage) {
	this.startpage = startpage;
}

public int getEndpage() {
	return endpage;
}

public void setEndpage(int endpage) {
	this.endpage = endpage;
}
posted @ 2016-02-07 12:18  It'sRobin  阅读(153)  评论(0编辑  收藏  举报