本文实例讲述了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;
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程序设计有所帮助。
您可能感兴趣的文章:Android滑动动态分页实现方法Android程序开发之Listview下拉刷新上拉(滑动分页)加载更多Android App中使用ViewPager实现滑动分页的要点解析Android中实现多行、水平滚动的分页的Gridview实例源码Android之ListView分页加载数据功能实现代码Android实现ListView分页自动加载数据的方法android实现listview分页的方法Android提高之SQLite分页读取实现方法Android实现简单的分页效果Android ListView分页功能实现方法Android开发中滑动分页功能实例详解