仿谷歌,百度分页计算分析 代码兑现
仿谷歌,百度分页计算分析
经过研究百度或谷歌分页有这样的规律:
点击第一页时
点击第二页(点2 出11)-----(点3 出12)
一直往下 (点4 出13)
直到点击第11页时(点11 出20)
点击12页(出现21 去掉 1)
是否找到规律!
百度的最多现实的页数是20页 ,那好我们以最多现实6页来进行分析!
下面进行分析:
分析:以下是以显示6个数页 为参考
1 2 3 当前页是1 1 2 3
1 2 3 4 当前页是2 起始值1 未页 4
1 2 3 4 5 当前页是3 起始值1 未页 5
1 2 3 4 5 6 当前页是4 起始值1 未页 6
从上图得知 当前页小于等于4时 起始页为1 末页=当前页+2
startindex=1;
endindex=nowpage+2;
2 3 4 5 6 7 当前页是5 起始值是2 未页 7
3 4 5 6 7 8 当前页是6 起始值是3 未页 8
4 5 6 7 8 9 当前页是7 起始值是4 未页 9
5 6 7 8 9 10 当前页是8 起始值是5 未页 10
从上图得知 当前页大于4时 (包括4)
1.当前页与尾页的差少于2
endindex=nowpage+2;
startindex=nowapge-3;
1.如果总页数在6页以上(包括6),起始页固定为尾页数过来第5个
分析: 索引值endindex=lastpage
startindex=lastpage -5;
2.总页数小于6,起始页固定为1 末页 lastpage
startindex=1;
endindex=lastpage;
2.正常情况,起始页为当前页-2
endindex=nowpage+2;
startindex=nowapge-3;
对于末页,即显示的最后一个页码
1.如果当前页小于4
1.如果总页数比6要大,末页为6
nowpage<4;
lastpage>6
startindex=1;
endindex=6;
2.比6小,末页为尾页
nowpage<4;
lastpage<6
startindex=1;
endindex=lastpage;
2.当前页大于4
1.当前页与尾页差小于2,末页为尾页
startindex=nowpage-3;
if(nowpage+2>lastpage){ endindex=lastpage}else{endindex= nowpage+2;}
2.否则末页为当前页+2
计算分析完了,是否可以用java实现啦?
代码如下:
package cn.****.util; import java.util.List; public class Pagination<T> { // 分页信息 private int nowpage;// 当前页 private int countrecord;// 总记录 private int countpage;// 总页数 public static final int PAGESIZE = 5;// 每页显示的记录数 private int startpage;// 页面中的起始页 private int endpage;// 页面中的结束页 private final int SHOWPAGE = 6;// 页面中显示的总页数 baidu,google显示的总页数是20 // 在测试我们才用6来测试 private List<T> allentities; private String url; /** 根据当前页及总记录数来构造分页对象 */ 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的值 */ /** 总页数数是否小于4 */ if (this.countpage < (this.SHOWPAGE / 2 + 1)) { this.startpage = 1; // 页面中起始页就是1 this.endpage = this.countpage;// 页面中的最终页就是总页数 } else { /** else中是总页数大于4的情况 */ /** 首先当前页的值是否小于等于4 */ if (this.nowpage <= (this.SHOWPAGE / 2 + 1)) { this.startpage = 1; this.endpage = this.nowpage + 2; /** 判断页面的最终页是否大于总页数 */ if (this.endpage >= this.countpage) { this.endpage = this.countpage; } } else { this.startpage = this.nowpage - 3; this.endpage = this.nowpage + 2; if (this.endpage >= this.countpage) { this.endpage = this.countpage; if (this.countpage < this.SHOWPAGE) { this.startpage = 1; } else { this.startpage = this.endpage - 5; } } } } } 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; } public List<T> getAllentities() { return allentities; } public void setAllentities(List<T> allentities) { this.allentities = allentities; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } }