Android ListView数据的分批显示功能

Android ListView数据的分批显示功能,第1张

概述AndroidListView数据的分批显示布局文件activity_call_safe2.xml<?xmlversion=\"1.0\"encoding=\"utf-8\"?>

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     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的滑动事件,滑动的时候就更新数据@H_404_6@

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,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,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,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; }}@H_404_6@

以上所述是小编给大家介绍的AndroID ListVIEw数据的分批显示功能,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!

总结

以上是内存溢出为你收集整理的Android ListView数据的分批显示功能全部内容,希望文章能够帮你解决Android ListView数据的分批显示功能所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1146414.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存