`

Android学习之滑动分页

阅读更多

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:id="@+id/mylayout"
	android:orientation="vertical"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent"> 
</LinearLayout>

 

 

 

tab_info.xml:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout 
	android:layout_width="fill_parent"
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_height="wrap_content">
	<TableRow>
		<TextView 
			android:id="@+id/id"
			android:textSize="30px"	
			android:layout_height="wrap_content"
			android:layout_width="50px"/>
		<TextView
			android:id="@+id/name" 
			android:textSize="30px"	
			android:layout_height="wrap_content"
			android:layout_width="130px"/>
		<TextView
			android:id="@+id/birthday" 
			android:textSize="30px"	
			android:layout_height="wrap_content"
			android:layout_width="180px"/>
	</TableRow>
</TableLayout>

 

 

 

MySQLiteDemo.java:

import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
 
public class MySQLiteDemo extends Activity {
	private SQLiteOpenHelper helper = null ;					// 数据库操作
	private LinearLayout mylayout = null ;						// 定义布局管理器
	private ListView listView ;									// ListView组件
	private int currentPage = 1 ;								// 当前页
	private int lineSize = 15 ;									// 每页显示15条数据
	private int allRecorders = 0 ;								// 保存全部记录数
	private int pageSize = 1 ; 									// 总页数
	private int lastItem = 0 ;									// 保存最后一个记录点
	private SimpleAdapter simpleAdapter = null; 				// 适配器
	private LinearLayout loadLayout = null ;					// 定义布局管理器
	private TextView loadInfo = null ;							// 定义提示文本
	private List<Map<String, Object>> all = null ;				// 保存适配器数据
	private LayoutParams layoutParams = new LinearLayout.LayoutParams(
			LinearLayout.LayoutParams.FILL_PARENT,
			LinearLayout.LayoutParams.WRAP_CONTENT);			// 布局参数
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);						// 父类onCreate()
        super.setContentView(R.layout.main);					// 默认布局管理器
        this.mylayout = (LinearLayout) super.findViewById(R.id.mylayout) ;	// 取得组件
        this.loadLayout = new LinearLayout(this) ;				// 定义线性布局管理器
        this.helper = new MyDatabaseHelper(this) ;				// 定义数据库辅助类
        this.loadInfo = new TextView(this) ;					// 实例化文本组件
        this.loadInfo.setText("数据加载中ing..") ;				// 定义文本
        this.loadInfo.setGravity(Gravity.CENTER) ;				// 文字居中显示
        this.loadInfo.setTextSize(30.0f) ;						// 设置显示文字大小
		this.loadLayout.addView(this.loadInfo, this.layoutParams);// 增加新组件
		this.loadLayout.setGravity(Gravity.CENTER) ;			// 数据居中显示
        this.showAllData() ;									// 显示数据
		this.pageSize = (this.allRecorders + this.currentPage - 1)
				/ this.lineSize; 								// 求出总页数
    }
    private class OnScrollListenerImpl implements OnScrollListener {
		@Override
		public void onScroll(AbsListView view, int firstVisibleItem,
				int visibleItemCount, int totalItemCount) {
			MySQLiteDemo.this.lastItem = firstVisibleItem + visibleItemCount - 1;
		}

		@Override
		public void onScrollStateChanged(AbsListView view, int scrollState) {
			if (MySQLiteDemo.this.lastItem == MySQLiteDemo.this.simpleAdapter.getCount()								// 当前记录已经是在最底部
					&& MySQLiteDemo.this.currentPage < MySQLiteDemo.this.pageSize	// 当前页小于总页数
					&& scrollState == OnScrollListener.SCROLL_STATE_IDLE) { // 屏幕不再滚动时触发
				MySQLiteDemo.this.currentPage ++ ;				// 修改当前所在页
				MySQLiteDemo.this.listView
						.setSelection(MySQLiteDemo.this.lastItem);	// 设置显示位置
				MySQLiteDemo.this.appendData() ;				// 刷新显示数据
			}
		}
    }
	private void showAllData() {
		this.helper = new MyDatabaseHelper(MySQLiteDemo.this) ;	// 实例化对象
		this.listView = new ListView(MySQLiteDemo.this); 		// 定义ListView
		MytabCursor cur = new MytabCursor(this.helper
				.getReadableDatabase()) ;						// 数据库查询操作
		this.allRecorders = cur.getCount() ;					// 查询全部记录数
		this.all = cur.find(this.currentPage, this.lineSize); 	// 取出查询数据
		this.simpleAdapter = new SimpleAdapter (this ,
			all, 												// 将数据包装
			R.layout.tab_info, 									// 每行显示一条数据
			new String[] { "id", "name","birthday" } ,			// 设置组件的字段
			new int[] { R.id.id, R.id.name, R.id.birthday });	// 实例化适配器对象
		this.listView.addFooterView(MySQLiteDemo.this.loadLayout) ;	// 增加一个标注
		this.listView.setAdapter(this.simpleAdapter); 			// 设置显示数据
		this.listView.setOnScrollListener(new OnScrollListenerImpl()) ;	// 设置滚动监听
		MySQLiteDemo.this.mylayout.addView(this.listView); 		// 追加组件
	}
	private void appendData() {									// 追加新数据
		MytabCursor cur = new MytabCursor(this.helper
				.getReadableDatabase()) ;						// 数据库查询操作
		List<Map<String, Object>> newData = cur.find(this.currentPage,
				this.lineSize); 								// 取出查询数据
		this.all.addAll(newData) ;								// 追加数据
		this.simpleAdapter.notifyDataSetChanged() ;				// 更新记录通知
	}
}

 

 

 

MytabCursor.java:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class MytabCursor {
	private static final String TABLENAME = "mytab" ;			// 数据表名称 
	private SQLiteDatabase db = null ;							// SQLiteDatabase
	public MytabCursor(SQLiteDatabase db) { 					// 构造方法
		this.db = db ;											// 接收SQLiteDatabase
	}
	public int getCount() {										// 返回记录数
		int count = 0 ; 										// 保存返回结果
		String sql = "SELECT COUNT(id) FROM " + TABLENAME ;		// 查询SQL
		Cursor result = db.rawQuery(sql, null);					// 查询
		for (result.moveToFirst(); !result.isAfterLast(); result.moveToNext()) {
			count = result.getInt(0) ;							// 取出查询结果
		}
		return count ;
	} 
	public List<Map<String,Object>> find(int currentPage, int lineSize) {	// 查询数据表
		List<Map<String,Object>> all = new ArrayList<Map<String,Object>>() ;// 定义List集合
		String sql = "SELECT id,name,birthday FROM " + TABLENAME
				+ " LIMIT ?,? " ;								// 查询SQL
		String selectionArgs[] = new String[] {
				String.valueOf((currentPage - 1) * lineSize),
				String.valueOf(lineSize) }; 					// 查询参数
		Cursor result = db.rawQuery(sql, selectionArgs);		// 查询
		for (result.moveToFirst(); !result.isAfterLast(); result.moveToNext()) {
			Map<String,Object> map = new HashMap<String,Object>() ;
			map.put("id", result.getInt(0));					// 取出id字段的内容
			map.put("name", result.getString(1));				// 取出name字段的内容
			map.put("birthday", result.getString(2));			// 取出birthday字段的内容
			all.add(map);										// 向集合保存
		}
		this.db.close() ;										// 关闭数据库连接
		return all ;  
	}
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics