仿Baidu,Google查询分页技术实现分析之一

仿Baidu,Google查询分页技术实现分析

分页 (pagination) 一种自动分页机制,可以将移动 Web 窗体中的内容分割成一组组较小的页进行呈现,以适合于特定的设备。该机制还呈现可用于浏览到其他页的用户界面元素.在整个的web开发应用中分页是必会的知识点。应该熟练掌握与运用。
在实际开发的应用中,分页显的尤为重要,现将baidu,google查询分页显示的页面及分页效果分析如下:
1 、baidu搜索观察分页效果如下:
2 、Google搜索分页效果实现如下:
 
 
 
 
 
 
 
分析流程结构: 

 

页面显示的页码
当前页
末页
分析
1 2 3 4 5 6 7 8 9 10 下一页
1
10
10=1+9
 分析1:当总页数小于11时,
       页面索引起始值=1;
       页面索引最终值=总页数;
上一页 1 2 3 4 5 6 7 8 9 10 11 下一页
2
11
11=2+9
上一页 1 2 3 … … 9 10 11 12 下一页
3
12
12=3+9
上一页 1 2 3 … …10 11 12 13 下一页
4
13
13=4+9
上一页 1 2 3 … …11 12 13 14 下一页
5
14
14=5+9
上一页 1 2 3 … …12 13 14 15 下一页
6
15
15=6+9
上一页 1 2 3 … …13 14 15 16 下一页
7
16
16=7+9
上一页 1 2 3 … …16 17 18 20 下一页
11
20
20=11+9
注意观察
上一页 2 3 4 … …18 29 20 21 下一页
12
21
2=12-10 21=12+9
上一页 3 4 5 … …19 20 21 22 下一页
13
22
3=13-10 22=13+9
上一页 4 5 6 … … 20 21 22 23 下一页
14
23
4=14-10 23=13+9
分析2:当 总页数大于 11页面上显示的总共页码为20,其中以上观察可知:
      页面索引起始值=当前页-10;
      页面索引最终值=当前页+9;
注意:当页面的 索引最终值大于查询的总页数时,那么效果应该如下:
    页面索引最终值=查询的总页数;
    页面索引起始值=查询的总页数-19;
 
 
 

 

 
上面做了简单的分析与算法,那你是否能够用Java代码实现呢?
 
 

原创:仿Baidu,Google查询分页技术JAVA实现

http://student.csdn.net/space.php?uid=1394199&do=blog&id=56848
在上一节中我们简单观察与分析了仿baidu,google的查询分页技术,有很多学生纷纷的给我来了邮件,期待着使用Java技术的实现.在这里很感谢大家对我的关注,我会一如既往的在此平台之上发布自己的授课中一些知识点的总结.下将仿baidu,google查询分页技术的实现方式一种分享给大家,如有问题请及时发送邮件.希望同学们在这新的学期中有更大的收获。

在这里我简单说说我的实现思路,我将整个分页的技术全部封装在了一个Pagination的JavaBean中.具体代码如下:
 
Java代码 复制代码
  1. package cn.csdn.util;   
  2.   
  3. import java.util.List;   
  4.   
  5. /**  
  6.  *   
  7.  * @author redarmy_chen  
  8.  *  
  9.  * @param <T>  
  10.  */  
  11. public class Pagination<T> {   
  12.   
  13.     // 分页信息   
  14.     private int nowpage;// 当前页   
  15.     private int countrecord;// 总记录   
  16.     private int countpage;// 总页数   
  17.   
  18.     public static final int PAGESIZE = 5;// 每页显示的记录数   
  19.   
  20.     private int startpage;// 页面中的起始页   
  21.     private int endpage;// 页面中的结束页   
  22.   
  23.     private final int SHOWPAGE = 6;// 页面中显示的总页数 baidu,google显示的总页数是20   
  24.     // 在测试我们才用6来测试   
  25.   
  26.     private List<T> allentities;   
  27.   
  28.     private String url;   
  29.   
  30.     /** 根据当前页及总记录数来构造分页对象 */  
  31.     public Pagination(int nowpage, int countrecord) {   
  32.         this.nowpage = nowpage;   
  33.         this.countrecord = countrecord;   
  34.   
  35.         /** 计算总页数 */  
  36.         this.countpage = this.countrecord % this.PAGESIZE == 0 ? this.countrecord   
  37.                 / this.PAGESIZE   
  38.                 : this.countrecord / this.PAGESIZE + 1;   
  39.   
  40.         /** 计算startpage与endpage的值 */  
  41.   
  42.         /** 总页数数是否小于4 */  
  43.         if (this.countpage < (this.SHOWPAGE / 2 + 1)) {   
  44.             this.startpage = 1// 页面中起始页就是1   
  45.             this.endpage = this.countpage;// 页面中的最终页就是总页数   
  46.         } else {   
  47.             /** else中是总页数大于4的情况 */  
  48.   
  49.             /** 首先当前页的值是否小于等于4 */  
  50.             if (this.nowpage <= (this.SHOWPAGE / 2 + 1)) {   
  51.                 this.startpage = 1;   
  52.                 this.endpage = this.nowpage + 2;   
  53.                 /** 判断页面的最终页是否大于总页数 */  
  54.                 if (this.endpage >= this.countpage) {   
  55.                     this.endpage = this.countpage;   
  56.                 }   
  57.             } else {   
  58.                 this.startpage = this.nowpage - 3;   
  59.                 this.endpage = this.nowpage + 2;   
  60.   
  61.                 if (this.endpage >= this.countpage) {   
  62.                     this.endpage = this.countpage;   
  63.                     if (this.countpage < this.SHOWPAGE) {   
  64.                         this.startpage = 1;   
  65.                     } else {   
  66.                         this.startpage = this.endpage - 5;   
  67.                     }   
  68.   
  69.                 }   
  70.             }   
  71.   
  72.         }   
  73.   
  74.     }   
  75.   
  76.     public int getNowpage() {   
  77.         return nowpage;   
  78.     }   
  79.   
  80.     public void setNowpage(int nowpage) {   
  81.         this.nowpage = nowpage;   
  82.     }   
  83.   
  84.     public int getCountrecord() {   
  85.         return countrecord;   
  86.     }   
  87.   
  88.     public void setCountrecord(int countrecord) {   
  89.         this.countrecord = countrecord;   
  90.     }   
  91.   
  92.     public int getCountpage() {   
  93.         return countpage;   
  94.     }   
  95.   
  96.     public void setCountpage(int countpage) {   
  97.         this.countpage = countpage;   
  98.     }   
  99.   
  100.     public int getStartpage() {   
  101.         return startpage;   
  102.     }   
  103.   
  104.     public void setStartpage(int startpage) {   
  105.         this.startpage = startpage;   
  106.     }   
  107.   
  108.     public int getEndpage() {   
  109.         return endpage;   
  110.     }   
  111.   
  112.     public void setEndpage(int endpage) {   
  113.         this.endpage = endpage;   
  114.     }   
  115.   
  116.     public List<T> getAllentities() {   
  117.         return allentities;   
  118.     }   
  119.   
  120.     public void setAllentities(List<T> allentities) {   
  121.         this.allentities = allentities;   
  122.     }   
  123.   
  124.     public String getUrl() {   
  125.         return url;   
  126.     }   
  127.   
  128.     public void setUrl(String url) {   
  129.         this.url = url;   
  130.     }   
  131.   
  132. }  
package cn.csdn.util;

import java.util.List;

/**
 * 
 * @author redarmy_chen
 *
 * @param <T>
 */
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;
	}

}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值