欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android实现简单的分页效果

程序员文章站 2024-02-27 15:15:27
本文实例为大家分享了android分页效果的具体代码,供大家参考,具体内容如下 1.实现分页最主要的就是封装分页代码,然后在按钮里实现相关的操作 /** *...

本文实例为大家分享了android分页效果的具体代码,供大家参考,具体内容如下

1.实现分页最主要的就是封装分页代码,然后在按钮里实现相关的操作

/**
 * 分页工具
 * 
 * @project app_page
 * @package com.android.dividepage
 * @author chenlin
 * @version 1.0
 * @date 2012年6月2日
 * @note todo
 * @param <t>
 */
public class pagehelper<t> {

 private list<t> alldata; // 所有数据
 private int perpage = 10; // 每页条目
 private int currentpage = 1;// 当前页
 private int pagenum = 1; // 页码
 private list<t> childdata;// 子数据
 private int allnum;// 总共条目

 public pagehelper(list<t> datas, int perpage) {
 this.alldata = datas;
 if (perpage > 0)
  this.perpage = perpage;
 // 如果数据大于10条
 if (alldata != null && alldata.size() > perpage) {
  childdata = alldata.sublist(0, perpage - 1);
 }
 allnum = alldata.size();
 // 如果总数能除断perpage,页数就是余数,否则+1
 pagenum = allnum % perpage == 0 ? (allnum / perpage) : (allnum / perpage + 1);
 }

 public int getcount() {
 return this.allnum;
 }

 public int getcurrentpage() {
 return this.currentpage;
 }

 public int getpagenum() {
 return this.pagenum;
 }

 public int getperpage() {
 return this.perpage;
 }

 public void gotopage(int n) { // 页面跳转
 currentpage = n > pagenum ? pagenum : (n < 1 ? 1 : n);
 }

 public boolean hasnextpage() {// 是否有下一页
 return currentpage < pagenum;
 }

 public boolean hasprepage() {// 是否有前一页
 return currentpage > 1;
 }

 public void headpage() {// 第一页
 currentpage = 1;
 }

 public void lastpage() {// 最后一页
 currentpage = pagenum;
 }

 public void nextpage() {// 下一页
 currentpage = hasnextpage() ? currentpage + 1 : pagenum;
 }

 public void prepage() {// 前一页
 currentpage = hasprepage() ? currentpage - 1 : 1;
 }

 public void setperpage(int perpage) {// 设置上一页面
 this.perpage = perpage;
 }

 /**
 * 获得当前数据
 * @return
 */
 public list<t> currentlist() {
 if (currentpage == 1) {
  childdata = alldata.sublist(0, perpage);
 } else if (currentpage == pagenum) {
  childdata = alldata.sublist(perpage * (pagenum - 1), allnum);
 } else {
  childdata = alldata.sublist(perpage * (currentpage - 1), perpage * currentpage);
 }
 return childdata;
 }

 public void setcurrentpage(int currentpage) {
 this.currentpage = currentpage;
 }

}

2、主页里我根据tab标签把按钮设置进去,然后在click里方法读取,最后根据tag标签就可判断是哪个按钮点击了

/**
 * 分页主页
 * @project app_page
 * @package com.android.dividepage
 * @author chenlin
 * @version 1.0
 * @date 2012年6月2日
 * @note todo
 */
public class mainactivity extends activity implements onclicklistener {

 private listview mlistview;
 //分页按钮
 private button mbtnprepage, mbtnnextpage, mbtnpreitem, mbtnnextitem;
 //显示分页信息
 private textview mtvpageno;

 //数据实现
 private pagehelper<string> mpagedaoimpl;
 private dataadapter madapter;
 private list<string> mdatas;

 //被选着的索引
 private int selectindex = 0;

 private static final int prepage = 0;
 private static final int nextpage = 1;
 private static final int preitem = 2;
 private static final int nextitem = 3;

 @override
 protected void oncreate(bundle savedinstancestate) {
 super.oncreate(savedinstancestate);
 setcontentview(r.layout.activity_main);
 initviews();
 initdatas();
 }

 private void initdatas() {
 //从资源文件里读数据
 mdatas = arrays.aslist(getresources().getstringarray(r.array.channellist));
 //每次读8条数据
 mpagedaoimpl = new pagehelper<string>(mdatas, 8);
 madapter = new dataadapter(this, mpagedaoimpl.currentlist());
 mlistview.setadapter(madapter);
 //设置当前页码与总页码
 mtvpageno.settext(mpagedaoimpl.getcurrentpage() + " / " + mpagedaoimpl.getpagenum());
 }

 private void initviews() {
 mlistview = (listview) findviewbyid(r.id.page_list);
 mbtnprepage = (button) findviewbyid(r.id.pre_page);
 mbtnprepage.settag(prepage);
 mbtnprepage.setonclicklistener(this);
 mbtnnextpage = (button) findviewbyid(r.id.next_page);
 mbtnnextpage.settag(nextpage);
 mbtnnextpage.setonclicklistener(this);
 mbtnpreitem = (button) findviewbyid(r.id.pre_item);
 mbtnpreitem.settag(preitem);
 mbtnpreitem.setonclicklistener(this);
 mbtnnextitem = (button) findviewbyid(r.id.next_item);
 mbtnnextitem.settag(nextitem);
 mbtnnextitem.setonclicklistener(this);

 mtvpageno = (textview) findviewbyid(r.id.pagenum);
 }

 @override
 public void onclick(view v) {
 final int flag = (integer) v.gettag();
 switch (flag) {
 case prepage:// 首页
  headpage();
  break;
 case nextpage:// 尾页
  lastpage();
  break;
 case preitem:// 上一条
  prepage();
  break;
 case nextitem:// 下一条
  nextpage();
  break;
 }
 }

 private void prepage() {
 if (selectindex == 0) {
  if (mpagedaoimpl.getcurrentpage() >= 1) {
  mpagedaoimpl.prepage();
  } 
  madapter.setdata(mpagedaoimpl.currentlist());
  mlistview.setselection(madapter.getcount() - 1);
  mtvpageno.settext(mpagedaoimpl.getcurrentpage() + " / " + mpagedaoimpl.getpagenum());
 } else {
  return;
 }
 }

 private void nextpage() {
 if (mpagedaoimpl.getcurrentpage() <= mpagedaoimpl.getpagenum()) {
  mpagedaoimpl.nextpage();
 } 
 madapter.setdata(mpagedaoimpl.currentlist());
 mlistview.setselection(0);
 mtvpageno.settext(mpagedaoimpl.getcurrentpage() + " / " + mpagedaoimpl.getpagenum());
 }



 private void lastpage() {
 if (mpagedaoimpl.getcurrentpage() != mpagedaoimpl.getpagenum()) {
  mpagedaoimpl.lastpage();
 } 
 madapter.setdata(mpagedaoimpl.currentlist());
 mtvpageno.settext(mpagedaoimpl.getcurrentpage() + " / " + mpagedaoimpl.getpagenum());
 }

 private void headpage() {
 if (mpagedaoimpl.getcurrentpage() != 1) {
  mpagedaoimpl.headpage();
 } 
 madapter.setdata(mpagedaoimpl.currentlist());
 mtvpageno.settext(mpagedaoimpl.getcurrentpage() + " / " + mpagedaoimpl.getpagenum());
 }

}

3、适配器代码,太简单了,我就不说了

public class dataadapter extends baseadapter {

 private context mcontext;
 private list<string> mdatas;

 public dataadapter(context context, list<string> datas) {
 this.mcontext = context;
 this.mdatas = datas;
 }

 @override
 public int getcount() {
 return mdatas == null ? 0 : mdatas.size();
 }

 @override
 public object getitem(int position) {
 return mdatas == null ? null : mdatas.get(position);
 }

 @override
 public long getitemid(int position) {
 return position;
 }

 @override
 public view getview(int position, view convertview, viewgroup parent) {
 viewholder mholder;
 if (convertview == null) {
  convertview = layoutinflater.from(mcontext).inflate(r.layout.channel_item, parent, false);
  mholder = new viewholder();
  mholder.nameview = (textview) convertview.findviewbyid(r.id.channel_name);
  convertview.settag(mholder);
 }
 mholder = (viewholder) convertview.gettag();
 mholder.nameview.settext(mdatas.get(position));
 return convertview;
 }

 static class viewholder {
 public textview nameview;
 }

 public void setdata(list<string> datas) {
 mdatas = datas;
 notifydatasetchanged();
 }
}


4、代码下载:http://xiazai.jb51.net/201611/yuanma/androidapppage(jb51.net).rar

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。