java 模拟省略号分页

2/22/2017来源:ASP.NET技巧人气:1094

java 模拟省略号分页

本文实现java模拟省略号分页效果如下图 [1],2,3,…,19,20, 1,[2],3,4,…,19,20, 1,2,[3],4,5,…,19,20, 1,2,3,[4],5,6,…,19,20, 1,…,3,4,[5],6,7,…,19,20, 1,…,4,5,[6],7,8,…,19,20, 1,…,5,6,[7],8,9,…,19,20, 1,…,6,7,[8],9,10,…,19,20, 1,…,7,8,[9],10,11,…,19,20, 1,…,8,9,[10],11,12,…,19,20, 1,…,9,10,[11],12,13,…,19,20, 1,…,10,11,[12],13,14,…,19,20, 1,…,11,12,[13],14,15,…,19,20, 1,…,12,13,[14],15,16,…,19,20, 1,…,13,14,[15],16,17,…,19,20, 1,…,14,15,[16],17,18,…,20, 1,…,15,16,[17],18,19,20, 1,…,16,17,[18],19,20, 1,…,17,18,[19],20, 1,…,18,19,[20],


部分代码

代码块语法遵循标准markdown代码,例如:

/** * 根据当前页 和总页数 获取带有省略号的分页List * [1]23...89 * 1[2]34...89 * 12[3]45...89 * 123[4]56...89 * 1...34[5]67...9 * 1...45[6]789 * @param current 当前页 * @param total 总页数 * @return */ public static List<String> getPageList(int current,int total){ List<String> l = new ArrayList<String>(); List<String> list = getListByCurrent(current); if(current==4){ l.add("1"); }else if(current>4){ l.add("1"); l.add("..."); } for (String string : list) { if(Integer.valueOf(string)>0&&Integer.valueOf(string)<=total){ l.add(string); } } if(total-current>4){ l.add("..."); l.add(total-1+""); l.add(total+""); }else if(total-current==4){ l.add("..."); l.add(total+""); }else if(total-current==3){ l.add(total+""); } return l; } public static List<String> getListByCurrent(int current){ List<String> list = new ArrayList<String>(); list.add(String.valueOf(current-2)); list.add(String.valueOf(current-1)); list.add(String.valueOf(current)); list.add(String.valueOf(current+1)); list.add(String.valueOf(current+2)); return list; }