本文实例讲述了android实现基于滑动的sqlite数据分页加载技术。分享给大家供大家参考,具体如下:
main.xml如下:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/action_settings"
android:orderincategory="100"
android:showasaction="never"
android:title="@string/action_settings"/>
</menu>
mainactivity.java如下:
package com.example.testscrollsqlite;
import java.util.arraylist;
import android.app.actionbar.layoutparams;
import android.app.activity;
import android.os.bundle;
import android.view.gravity;
import android.view.menu;
import android.view.view;
import android.view.viewgroup;
import android.widget.abslistview;
import android.widget.abslistview.onscrolllistener;
import android.widget.adapter;
import android.widget.baseadapter;
import android.widget.linearlayout;
import android.widget.listview;
import android.widget.textview;
public class mainactivity extends activity implements onscrolllistener{
private textview loadinfo;
private listview listview;
private linearlayout loadlayout;
private arraylist<string> items;
private databaseservice service;
private int currentpage = 1; //默认在第一页
private static final int linesize = 7; //每次显示数
private int allrecorders = 0; //全部记录数
private int pagesize = 1; //默认共一页
private int lastitem;
private aleph0 baseadapter;
@override
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.activity_main);
listview = (listview) findviewbyid(r.id.listview);
//创建一个角标线性布局用来显示"正在加载"
loadlayout = new linearlayout(this);
loadlayout.setgravity(gravity.center);
//定义一个文本显示“正在加载”
loadinfo = new textview(this);
loadinfo.settext("正在加载...");
loadinfo.setgravity(gravity.center);
//增加组件
loadlayout.addview(loadinfo, new layoutparams(
linearlayout.layoutparams.match_parent, linearlayout.layoutparams.wrap_content));
//增加到listview底部
listview.addfooterview(loadlayout);
listview.setonscrolllistener(this);
showalldata();
}
/**
* 读取全部数据
*/
public void showalldata(){
service = new databaseservice(this);
allrecorders = service.getcount();
//计算总页数
pagesize = (allrecorders + linesize -1) / linesize;
system.out.println("allrecorders = " + allrecorders);
system.out.println("pagesize = " + pagesize);
items = service.getallitems(currentpage, linesize);
for(int i=0; i<items.size(); i++){
system.out.println(items.get(i));
}
baseadapter = new aleph0();
listview.setadapter(baseadapter);
}
@override
public void onscroll(abslistview view, int firstvisible, int visiblecount,
int totalcount) {
lastitem = firstvisible + visiblecount - 1; //统计是否到最后
}
@override
public void onscrollstatechanged(abslistview view, int scorllstate) {
system.out.println("进入滚动界面了");
//是否到最底部并且数据没读完
if(lastitem == baseadapter.getcount()
&& currentpage < pagesize //不再滚动
&& scorllstate == onscrolllistener.scroll_state_idle){
currentpage ++;
//设置显示位置
listview.setselection(lastitem);
//增加数据
appenddate();
}
}
/**
* 增加数据
*/
private void appenddate(){
arraylist<string> additems = service.getallitems(currentpage, linesize);
baseadapter.setcount(baseadapter.getcount() + additems.size());
//判断,如果到了最末尾则去掉“正在加载”
if(allrecorders == baseadapter.getcount()){
listview.removefooterview(loadlayout);
}
items.addall(additems);
//通知记录改变
baseadapter.notifydatasetchanged();
}
class aleph0 extends baseadapter {
int count = linesize; /* starting amount */
public int getcount() {
return count;
}
public void setcount(int count){
this.count = count;
}
public object getitem(int pos) {
return pos;
}
public long getitemid(int pos) {
return pos;
}
public view getview(int pos, view v, viewgroup p) {
textview view = new textview(mainactivity.this);
view.settextsize(60);
if(items != null){
view.settext(items.get(pos));
}else{
view.settext(pos);
}
return view;
}
}
}
完整实例代码点击此处本站下载。
更多关于android相关内容感兴趣的读者可查看本站专题:《android操作sqlite数据库技巧总结》、《android操作json格式数据技巧总结》、《android数据库操作技巧总结》、《android编程之activity操作技巧总结》、《android文件操作技巧汇总》、《android编程开发之sd卡操作方法汇总》、《android开发入门与进阶教程》、《android资源操作技巧汇总》、《android视图view技巧总结》及《android控件用法总结》
希望本文所述对大家android程序设计有所帮助。