Android分页中显示出下面翻页的导航栏的布局实例代码
程序员文章站
2022-05-30 18:17:18
当页面条目过多的时候需要分页,要在布局中显示出分页的相关布局,使用android:layout_weight="11"
activity_call_safe.xml...
当页面条目过多的时候需要分页,要在布局中显示出分页的相关布局,使用android:layout_weight="11"
activity_call_safe.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <textview style="@style/titlestyle" android:text="黑名单管理" /> <framelayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="11" > <linearlayout android:id="@+id/ll_pb" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:visibility="invisible" android:orientation="vertical" > <progressbar android:layout_width="wrap_content" android:layout_height="wrap_content" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="玩命加载中..." /> </linearlayout> <include android:id="@+id/list_view_callsafe" layout="@layout/list_view_callsafe"/> </framelayout> <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <button android:onclick="prepage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="上一页" android:layout_weight="1" /> <button android:onclick="nextpage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="下一页" android:layout_weight="1" /> <button android:onclick="jump" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="跳转" android:layout_weight="1" /> <edittext android:id="@+id/et_page_number" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" /> <textview android:id="@+id/tv_page_number" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="0/12" android:textsize="20sp" android:layout_weight="1" /> </linearlayout> </linearlayout>
逻辑文件,实现分页显示,页面的跳转,页数的显示,数据的删除同时更新listview
package com.ldw.safe.activity; import java.util.list; import android.app.activity; import android.content.context; import android.os.bundle; import android.os.handler; import android.os.message; import android.text.textutils; import android.view.view; import android.view.view.onclicklistener; import android.view.viewgroup; import android.widget.adapter; import android.widget.edittext; import android.widget.imageview; import android.widget.linearlayout; import android.widget.listview; import android.widget.textview; import android.widget.toast; import com.ldw.safe.r; import com.ldw.safe.adapter.mybaseadapter; import com.ldw.safe.bean.blacknumberinfo; import com.ldw.safe.db.dao.blacknumberdao; public class callsafeactivity extends activity { private listview list_view_callsafe; private list<blacknumberinfo> blacknumberinfos; private linearlayout ll_pb; private blacknumberdao dao; private textview tv_page_number; private int pagenumber; private int mpagesize = 20;//每一页显示多少数据 private int mcurrentpagenumber = 0;//当前页面 private int totalpage; private callsafeadapter adapter; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_call_safe); initui(); initdata(); } //初始化数据 private handler handler = new handler(){ @override public void handlemessage(message msg) { //通过总数,和每页显示多少条显示有多少条数据 tv_page_number.settext(mcurrentpagenumber + "/" + totalpage); //数据初始化结束,关闭家在全的展示 ll_pb.setvisibility(view.invisible); adapter = new callsafeadapter(blacknumberinfos, callsafeactivity.this); //配置适配器 list_view_callsafe.setadapter(adapter); } }; private edittext et_page_number; //初始化数据 private void initdata() { //防止阻塞主线程,在子线程中初始化数据(后期数据会很多) new thread(){ @override public void run(){ dao = new blacknumberdao(callsafeactivity.this); //获取到总条数和页数 totalpage = dao.gettotalnumber() / mpagesize; //获取到表的所有数据 //blacknumberinfos = dao.findall(); //分页显示数据 blacknumberinfos = dao.findpar(mcurrentpagenumber, mpagesize); handler.sendemptymessage(0); } }.start(); } //初始化ui private void initui() { ll_pb = (linearlayout) findviewbyid(r.id.ll_pb); //初始化的时候展示加载的圆圈 ll_pb.setvisibility(view.visible); list_view_callsafe = (listview) findviewbyid(r.id.list_view_callsafe); tv_page_number = (textview) findviewbyid(r.id.tv_page_number); et_page_number = (edittext) findviewbyid(r.id.et_page_number); } //对代码进行优化,继承自定义mybaseadapter,mybaseadapter继承自baseadapter封装一些常用的方法 private class callsafeadapter extends mybaseadapter<blacknumberinfo>{ private callsafeadapter(list lists, context mcontext){ super(lists, mcontext); } @override public view getview(int position, view convertview, viewgroup parent) { //谷歌推出的优化方案 viewholder holder; if(convertview == null){ convertview = view.inflate(callsafeactivity.this, r.layout.item_call_safe, null); holder = new viewholder(); holder.tv_number = (textview) convertview.findviewbyid(r.id.tv_number); holder.tv_mode = (textview) convertview.findviewbyid(r.id.tv_mode); holder.iv_delete = (imageview) convertview.findviewbyid(r.id.iv_delete); convertview.settag(holder); }else{ holder = (viewholder) convertview.gettag(); } holder.tv_number.settext(lists.get(position).getnumber()); string mode = lists.get(position).getmode(); if(mode.equals("1")){ holder.tv_mode.settext("电话和短信拦截"); }else if(mode.equals("2")){ holder.tv_mode.settext("电话拦截"); }else if(mode.equals("3")){ holder.tv_mode.settext("短信拦截"); } final blacknumberinfo info = lists.get(position); //监听删除数据按钮,删除数据 holder.iv_delete.setonclicklistener(new onclicklistener(){ @override public void onclick(view v) { string number = info.getnumber(); boolean result = dao.delete(number); if(result){ toast.maketext(callsafeactivity.this, "删除成功", toast.length_short).show(); lists.remove(info); //更新listview adapter.notifydatasetchanged(); }else{ toast.maketext(callsafeactivity.this, "删除失败", toast.length_short).show(); } } }); return convertview; /* //谷歌推出的优化方案 viewholder holder; if(convertview == null){ convertview = view.inflate(callsafeactivity.this, r.layout.item_call_safe, null); holder = new viewholder(); holder.tv_number = (textview) convertview.findviewbyid(r.id.tv_number); holder.tv_mode = (textview) convertview.findviewbyid(r.id.tv_mode); convertview.settag(holder); }else{ holder = (viewholder) convertview.gettag(); } holder.tv_number.settext(blacknumberinfos.get(position).getnumber()); string mode = blacknumberinfos.get(position).getmode(); if(mode.equals("1")){ holder.tv_mode.settext("电话和短信拦截"); }else if(mode.equals("2")){ holder.tv_mode.settext("电话拦截"); }else if(mode.equals("3")){ holder.tv_mode.settext("短信拦截"); } return convertview; */ /*初步的优化 convertview = view.inflate(callsafeactivity.this, r.layout.item_call_safe, null); textview tv_number = (textview) convertview.findviewbyid(r.id.tv_number); textview tv_mode = (textview) convertview.findviewbyid(r.id.tv_mode); tv_number.settext(blacknumberinfos.get(position).getnumber()); string mode = blacknumberinfos.get(position).getmode(); if(mode.equals("1")){ tv_mode.settext("电话和短信拦截"); }else if(mode.equals("2")){ tv_mode.settext("电话拦截"); }else if(mode.equals("3")){ tv_mode.settext("短信拦截"); } return convertview; */ /*传统的方式 view view = view.inflate(callsafeactivity.this, r.layout.item_call_safe, null); textview tv_number = (textview) view.findviewbyid(r.id.tv_number); textview tv_mode = (textview) view.findviewbyid(r.id.tv_mode); tv_number.settext(blacknumberinfos.get(position).getnumber()); string mode = blacknumberinfos.get(position).getmode(); if(mode.equals("1")){ tv_mode.settext("电话和短信拦截"); }else if(mode.equals("2")){ tv_mode.settext("电话拦截"); }else if(mode.equals("3")){ tv_mode.settext("短信拦截"); } return view; */ } } static class viewholder{ textview tv_number; textview tv_mode; imageview iv_delete; } /* * 上一页 */ public void prepage(view v){ //页码不能超过范围 if(mcurrentpagenumber <= 0){ toast.maketext(this, "这已经是第一页", toast.length_short).show(); return; } mcurrentpagenumber --; initdata(); } /* * 下一页 */ public void nextpage(view v){ //页码不能超过范围 if(mcurrentpagenumber >= totalpage - 1){ toast.maketext(this, "这已经是最后一页", toast.length_short).show(); return; } mcurrentpagenumber ++; initdata(); } /* * 跳转页面 */ public void jump(view v){ string jump_page = et_page_number.gettext().tostring(); if(textutils.isempty(jump_page)){ toast.maketext(this, "请输入正确的页码", toast.length_short).show(); }else{ //读取的字符串转换成int int pagenumber = integer.parseint(jump_page); if(pagenumber >=0 && pagenumber<= totalpage - 1){ mcurrentpagenumber = pagenumber; initdata(); }else{ toast.maketext(this, "请输入正确的页码", toast.length_short).show(); } } } }
以上所述是小编给大家介绍的android分页中显示出下面翻页的导航栏的布局实例代码,希望对大家有所帮助
上一篇: MySQL数据库 — chap3-01 数据类型和约束
下一篇: 减肥吃五花肉可以吗