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

Android ListView数据的分批显示功能

程序员文章站 2022-10-24 17:10:20
android listview数据的分批显示 布局文件activity_call_safe2.xml

android listview数据的分批显示

布局文件activity_call_safe2.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" >
  <relativelayout 
    android:layout_width="match_parent"
   android:layout_height="wrap_content"
    >
    <textview 
 style="@style/titlestyle"
   android:text="黑名单管理"
    />
    <button 
      android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignparentright="true"
    android:text="添加"
      />
  </relativelayout>
  <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>逻辑文件,实现页面的分页显示,监听listview的滑动事件,滑动的时候就更新数据

casllsafeactivity.java

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.view.view;
import android.view.view.onclicklistener;
import android.view.viewgroup;
import android.widget.abslistview;
import android.widget.abslistview.onscrolllistener;
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 callsafeadapter adapter;
 private int totalnumber;//总的条数
 private int mstartindex = 0;//开始加载的位置
 private int maxcount = 20;//每页加载多少数据
 @override
  protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.activity_call_safe2);
    initui();
    initdata();
 }
 //初始化数据
 private handler handler = new handler(){
 @override
    public void handlemessage(message msg) {
  //数据初始化结束,关闭家在全的展示
  ll_pb.setvisibility(view.invisible);
  //防止适配器一直刷新
  if(adapter == null){
  adapter = new callsafeadapter(blacknumberinfos, callsafeactivity.this);
  //配置适配器
  list_view_callsafe.setadapter(adapter);
  }else{
   adapter.notifydatasetchanged();
  }
 }
 };
 private edittext et_page_number;
 //初始化数据
 private void initdata() {
 //防止阻塞主线程,在子线程中初始化数据(后期数据会很多)
 new thread(){
  @override
  public void run(){
  dao = new blacknumberdao(callsafeactivity.this);
   //一共有多少条数据
     totalnumber = dao.gettotalnumber();
  //分批加载数据
     if(blacknumberinfos == null){
      //第一次加载
      blacknumberinfos = dao.findpar2(mstartindex, maxcount);
     }else{
      //把后面的数据。追加到blacknumberinfos集合里面。防止黑名单被覆盖
      blacknumberinfos.addall(dao.findpar2(mstartindex, maxcount));
     }
  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);
 //设置listview的滚动监听器
 list_view_callsafe.setonscrolllistener(new onscrolllistener(){
      //状态改变时候回调的方法
      /*
       * @param scrollstate 表示滚动的状态
       *abslistview.onscrolllistener.scroll_state_idle 闲置状态
       *abslistview.onscrolllistener.scroll_state_touch_scroll 手指触摸的时候的状态
       *abslistview.onscrolllistener.scroll_state_fling 惯性
       */
  @override
  public void onscrollstatechanged(abslistview view, int scrollstate) {
        switch (scrollstate) {
        case abslistview.onscrolllistener.scroll_state_idle:
          //获取到最后一条显示的数据条数
          int lastvisibleposition = list_view_callsafe.getlastvisibleposition();
          system.out.println("lastvisibleposition==========" + lastvisibleposition);
            if(lastvisibleposition == blacknumberinfos.size() - 1){//判断最后一个数据是不是每页显示的数据-1
              // 加载更多的数据。 更改加载数据的开始位置
              mstartindex += maxcount;
              if (mstartindex >= totalnumber) {
                toast.maketext(getapplicationcontext(),
                    "没有更多的数据了。", toast.length_short).show();
                return;
              }
              initdata();
            }
          break;
      }
  }
  //listview滚动的时候调用的方法
      //时时调用。当我们的手指触摸的屏幕的时候就调用
  @override
  public void onscroll(abslistview view, int firstvisibleitem,
   int visibleitemcount, int totalitemcount) {
  // todo auto-generated method stub
  }
 });
 }
 //对代码进行优化,继承自定义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;
 }
}

以上所述是小编给大家介绍的android listview数据的分批显示功能,希望对大家有所帮助