Android实现简单的分页效果
本文实例为大家分享了Android分页效果的具体代码,供大家参考,具体内容如下
1.实现分页最主要的就是封装分页代码,然后在按钮里实现相关的操作
/** *分页工具 * *@ProjectApp_Page *@Packagecom.android.dividepage *@authorchenlin *@version1.0 *@Date2012年6月2日 *@NoteTODO *@param<T> */ publicclassPageHelper<T>{ privateList<T>allData;//所有数据 privateintperPage=10;//每页条目 privateintcurrentPage=1;//当前页 privateintpageNum=1;//页码 privateList<T>childData;//子数据 privateintallNum;//总共条目 publicPageHelper(List<T>datas,intperPage){ 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); } publicintgetCount(){ returnthis.allNum; } publicintgetCurrentPage(){ returnthis.currentPage; } publicintgetPageNum(){ returnthis.pageNum; } publicintgetPerPage(){ returnthis.perPage; } publicvoidgotoPage(intn){//页面跳转 currentPage=n>pageNum?pageNum:(n<1?1:n); } publicbooleanhasNextPage(){//是否有下一页 returncurrentPage<pageNum; } publicbooleanhasPrePage(){//是否有前一页 returncurrentPage>1; } publicvoidheadPage(){//第一页 currentPage=1; } publicvoidlastPage(){//最后一页 currentPage=pageNum; } publicvoidnextPage(){//下一页 currentPage=hasNextPage()?currentPage+1:pageNum; } publicvoidprePage(){//前一页 currentPage=hasPrePage()?currentPage-1:1; } publicvoidsetPerPage(intperPage){//设置上一页面 this.perPage=perPage; } /** *获得当前数据 *@return */ publicList<T>currentList(){ if(currentPage==1){ childData=allData.subList(0,perPage); }elseif(currentPage==pageNum){ childData=allData.subList(perPage*(pageNum-1),allNum); }else{ childData=allData.subList(perPage*(currentPage-1),perPage*currentPage); } returnchildData; } publicvoidsetCurrentPage(intcurrentPage){ this.currentPage=currentPage; } }
2、主页里我根据tab标签把按钮设置进去,然后在click里方法读取,最后根据tag标签就可判断是哪个按钮点击了
/** *分页主页 *@ProjectApp_Page *@Packagecom.android.dividepage *@authorchenlin *@version1.0 *@Date2012年6月2日 *@NoteTODO */ publicclassMainActivityextendsActivityimplementsOnClickListener{ privateListViewmListView; //分页按钮 privateButtonmBtnPrePage,mBtnNextPage,mBtnPreItem,mBtnNextItem; //显示分页信息 privateTextViewmTvPageNo; //数据实现 privatePageHelper<String>mPageDaoImpl; privateDataAdaptermAdapter; privateList<String>mDatas; //被选着的索引 privateintselectIndex=0; privatestaticfinalintPREPAGE=0; privatestaticfinalintNEXTPAGE=1; privatestaticfinalintPREITEM=2; privatestaticfinalintNEXTITEM=3; @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initViews(); initDatas(); } privatevoidinitDatas(){ //从资源文件里读数据 mDatas=Arrays.asList(getResources().getStringArray(R.array.channellist)); //每次读8条数据 mPageDaoImpl=newPageHelper<String>(mDatas,8); mAdapter=newDataAdapter(this,mPageDaoImpl.currentList()); mListView.setAdapter(mAdapter); //设置当前页码与总页码 mTvPageNo.setText(mPageDaoImpl.getCurrentPage()+"/"+mPageDaoImpl.getPageNum()); } privatevoidinitViews(){ 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 publicvoidonClick(Viewv){ finalintflag=(Integer)v.getTag(); switch(flag){ casePREPAGE://首页 headPage(); break; caseNEXTPAGE://尾页 lastPage(); break; casePREITEM://上一条 prePage(); break; caseNEXTITEM://下一条 nextPage(); break; } } privatevoidprePage(){ 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; } } privatevoidnextPage(){ if(mPageDaoImpl.getCurrentPage()<=mPageDaoImpl.getPageNum()){ mPageDaoImpl.nextPage(); } mAdapter.setData(mPageDaoImpl.currentList()); mListView.setSelection(0); mTvPageNo.setText(mPageDaoImpl.getCurrentPage()+"/"+mPageDaoImpl.getPageNum()); } privatevoidlastPage(){ if(mPageDaoImpl.getCurrentPage()!=mPageDaoImpl.getPageNum()){ mPageDaoImpl.lastPage(); } mAdapter.setData(mPageDaoImpl.currentList()); mTvPageNo.setText(mPageDaoImpl.getCurrentPage()+"/"+mPageDaoImpl.getPageNum()); } privatevoidheadPage(){ if(mPageDaoImpl.getCurrentPage()!=1){ mPageDaoImpl.headPage(); } mAdapter.setData(mPageDaoImpl.currentList()); mTvPageNo.setText(mPageDaoImpl.getCurrentPage()+"/"+mPageDaoImpl.getPageNum()); } }
3、适配器代码,太简单了,我就不说了
publicclassDataAdapterextendsBaseAdapter{ privateContextmContext; privateList<String>mDatas; publicDataAdapter(Contextcontext,List<String>datas){ this.mContext=context; this.mDatas=datas; } @Override publicintgetCount(){ returnmDatas==null?0:mDatas.size(); } @Override publicObjectgetItem(intposition){ returnmDatas==null?null:mDatas.get(position); } @Override publiclonggetItemId(intposition){ returnposition; } @Override publicViewgetView(intposition,ViewconvertView,ViewGroupparent){ ViewHoldermHolder; if(convertView==null){ convertView=LayoutInflater.from(mContext).inflate(R.layout.channel_item,parent,false); mHolder=newViewHolder(); mHolder.nameView=(TextView)convertView.findViewById(R.id.channel_name); convertView.setTag(mHolder); } mHolder=(ViewHolder)convertView.getTag(); mHolder.nameView.setText(mDatas.get(position)); returnconvertView; } staticclassViewHolder{ publicTextViewnameView; } publicvoidsetData(List<String>datas){ mDatas=datas; notifyDataSetChanged(); } }
4、代码下载:http://xiazai.jb51.net/201611/yuanma/AndroidAppPage(jb51.net).rar
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。