当前位置: 移动技术网 > 移动技术>移动开发>Android > android ListView结合xutils3仿微信实现下拉加载更多

android ListView结合xutils3仿微信实现下拉加载更多

2019年07月24日  | 移动技术网移动技术  | 我要评论

前言:最近涉及到和qq打交道,定义所有的好友一共只能有300条消息,如果一次性从数据库读取300条或者更多,界面会有细微的卡顿.所以考虑了下分页,第一次进来只显示20条(仿微信),当用户滑到第一条后,如果数据库有消息,则再加载20条.

步骤-问把大象关冰箱,总共分几步?

1.自定义abslistview.scrolllisterner

核心的东西是监听listview的scrolllisterner,这里采取拿来主义 原文连接找不到了,如果原作者看到,请联系青楼爱小生,将您的文章链接更新出来 ,网上扒了一个挺不错的,大家用的时候实现这个scrolllisterner,完善自己的逻辑即可

public class myonscrolllistener implements onscrolllistener {
 private int totalitemcount;
  //listview最后的item项
  private int lastitem;
  //listview第一项
  private int firstitem;
  //用于判断当前是否在加载
  private boolean isloading;
  //底部加载更多布局
  private view footer;
  //接口回调的实例
  private onloaddatalistener listener;
  
  //数据
  private list<msgbean> data;
  handler handler = new handler();
  
  
  public myonscrolllistener(view footer, list<msgbean> data) {
  this.footer = footer;
  this.data = data;
  }
  //设置接口回调的实例
  public void setonloaddatalistener(onloaddatalistener listener) {
  this.listener = listener;
  }
  /**
  * 滑动状态变化
  *
  * @param view
  * @param scrollstate 1 scroll_state_touch_scroll是拖动 2 scroll_state_fling是惯性滑动 0scroll_state_idle是停止 , 只有当在不同状态间切换的时候才会执行
  */
  @override
  public void onscrollstatechanged(abslistview view, int scrollstate) {
  //如果数据没有加载,并且滑动状态是停止的,并且滚到了第一个item,可在此做下拉更新或者上拉更新的判断
  if (!isloading && firstitem == 0 && scrollstate == scroll_state_idle) {
   //显示加载更多
   footer.setvisibility(view.visible);
  
   //模拟一个延迟两秒的刷新功能
   handler.postdelayed(new runnable() {
   @override
   public void run() {
    if (listener != null) {
    //开始加载更多数据
    loadmoredata();
    //回调设置listview的数据
    listener.onloaddata(data);
     //加载完成后操作什么
    loadcomplete();
    }
   }
   }, 2000);
  }
  }
  /**
  * 当加载数据完成后,设置加载标志为false表示没有加载数据了
  * 并且设置底部加载更多为隐藏
  */
  private void loadcomplete() {
  isloading = false;
  footer.setvisibility(view.gone);
  
  }
  /**
  * 开始加载更多新数据,这里每次只更新三条数据
  */
  private void loadmoredata() {
  isloading = true;
  msgbean msg = null;
  for (int i = 0; i < 3; i++) {
   msg = new msgbean();
   msg .setremark("liming"+i);
   msg .setmsgid(i);
   data.add(stu);
  }
  }
  /**
  * 监听可见界面的情况
  *
  * @param view  listview
  * @param firstvisibleitem 第一个可见的 item 的索引
  * @param visibleitemcount 可以显示的 item的条数
  * @param totalitemcount 总共有多少个 item
  */
  @override
  public void onscroll(abslistview view, int firstvisibleitem, int visibleitemcount, int totalitemcount) {
  //实现下拉加载
  lastitem = firstvisibleitem + visibleitemcount;
   //实现上拉加载
  firstitem = firstvisibleitem;
   //总listview的item个数
  this.totalitemcount = totalitemcount;
  }
  //回调接口
  public interface onloaddatalistener {
  void onloaddata(list<msgbean> data);
  }
  }

2.实现此接口

public class listpageactivity extends activity implements myonscrolllistener.onloaddatalistener {
 @override
  protected void oncreate(bundle savedinstancestate) {
  super.oncreate(savedinstancestate);
  setcontentview(r.layout.activity_list_page);
 
  //显示到listview上
  showlistview(data);
 //自定义的滚动监听事件
  myonscrolllistener onscrolllistener = new myonscrolllistener(header, data);
  //设置接口回调
  onscrolllistener.setonloaddatalistener(this);
  //设置listview的滚动监听事件
  mlistview.setonscrolllistener(onscrolllistener);

 @override
  public void onloaddata(list<msgbean> data) {
  //加载数据完成后,展示数据到listview
  showlistview(data);
  }
}

showlistview里面无疑是普通的更新adapter的工作

那么我们如何借助xutils的数据库进行分类呢?

3.利用xutils数据库操作进行分页处理

首先,我们理一下思路,上面我们已经实现了上拉的回调,在此回调中把新来的数据加载到adapter即可.

下文db是dbmanager的实例

/**
 * 当前屏幕显示的消息数量
 */
private int max_msg_number = 20;
private list<msgbean> getdatafromdb() {

  list<?> dbsize = db.selector(msgbean.class).where(wherebuilder.b("id", "=", 400)).findall();//记得捕获null指针和dbexception异常
//如果数据库比我们显示的页数小,则不偏移,否则,偏移到我们需要显示的位置
  if (dbsize.size() < max_msg_number) {
   indexoffset = 0;
  } else {
   indexoffset = dbsize.size() - max_msg_number;
  }
  
  list<msgbean> datas = db.selector(msgbean.class).where(wherebuilder.b("id", "=", 400)).limit(max_msg_number)
     .offset(indexoffset).findall();
  return datas;
 }

这里解释一下

db.selector(msgbean.class).where(wherebuilder.b("id", "=", 400)).limit(max_msg_number).offset(indexoffset).findall();是我们实现分页的关键

.limit是我们定义的分页大小

.offset偏移量,我们数据库的大小是不变的,如果不定义偏移量,那么我们定义的分页大小每次只从0取到19.假设数据库中有21条数据,那么我们需要从1取到20,而不是0到19,所以偏移1.

然后我们在loadmoredata中

max_msg_number += max_msg_number;
getdatafromdb();

将大小自加,即完成加载更多的功能,在onloaddata(list<msgbean> data)中加载数据即可.

后面贴上我对xutils数据库操作的封装,还有很多不完善之处

/**
 * 数据库 xutils用法
 * @author 青楼爱小生
 */
public class dbutil {
 private static final string tag = dbutil.class.getname();
 private static dbutil dbutil;
 private dbmanager db;
 private dbutil(){
  db = x.getdb(myapplication.getinstance().daoconfig);
 }
 public static dbutil getinstance(){
  if(dbutil == null){
   synchronized (dbutil.class) {
    if(dbutil == null){
     dbutil = new dbutil();
    }
   }
  }
  return dbutil;
 }
 /**
  * 增加数据
  * @param list
  * @throws dbexception 
  */
 public void addmsglist(list<msgbean> list) {
  try {
   db.saveorupdate(list);
  } catch (dbexception e) {
   e.printstacktrace();
   loghelper.e(tag, e.getmessage());
  }
 }
 /**
  * 增加一条数据
  * @param node
  * @throws dbexception
  */
 public void addmsgtodb(msgbean node) {
  try {
   db.saveorupdate(node);
  } catch (dbexception e) {
   e.printstacktrace();
   loghelper.e(tag, e.getmessage());
  }
 }
 /**
  * 删除表中所有数据
  * @param cls 创建的表的映射
  * @throws dbexception 
  */
 public void deleteall(class cls) {
  try {
   db.delete(cls);
  } catch (dbexception e) {
   loghelper.e(tag, e.getmessage());
   e.printstacktrace();
  }
 }
 /**
  * 删除第一条数据
  * @param cls
  */
 @suppresswarnings("unchecked")
 public void deletefirst(class cls){
  try {
   db.delete(db.findfirst(cls));
  } catch (dbexception e) {
   // todo auto-generated catch block
   e.printstacktrace();
  }
 }
 /**
  * 查询表中所有数据
  * @throws dbexception 
  */
 @suppresswarnings("unchecked")
 public list<?> findall(class cls) {
  try {
   return db.findall(cls) == null ? collections.emptylist() : db.findall(cls);
  } catch (dbexception e) {
  e.printstacktrace();
   loghelper.e(tag, e.getmessage());
   return collections.emptylist();
  }
 }
 /**
  * //添加查询条件进行查询
  list<childinfo> all = db.selector(childinfo.class).where("id",">",2).and("id","<",4).findall();
  * @return 搜索指定条件的数据
  */
  @suppresswarnings("unchecked")
 public list<?> finddatabywhere(class cls,wherebuilder format){
  try {
   return db.selector(cls).where(format).findall()== null ?
     collections.emptylist() :db.selector(cls).where(format).findall();
  } catch (dbexception e) {
  loghelper.e(tag, e.getmessage());
   e.printstacktrace();
   return collections.emptylist();
  }
 
 }
 /**
  * 添加查询条件进行查询
  * @param cls 表映射
  * @param str select语句
  * @param format where语句
  * @return list<dbmodel> dbmodel key为数据库列名 value为值
  * eg:(selector.from(parent.class) 
         .where("id" ,"<", 54) 
         .and(wherebuilder.b("age", ">", 20).or("age", " < ", 30)) 
         .orderby("id") 
         .limit(pagesize) .offset(pagesize * pageindex)); 
  * 
  * 
  * 
  */
  @suppresswarnings("unchecked")
 public selector<?> finddatabyselector(class cls,wherebuilder format){
  try {
   return db.selector(cls).where(format);
  } catch (dbexception e) {
  // todo auto-generated catch block
   e.printstacktrace();
  }
  return null;
 }
  
}

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网