ListView分页
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="@+id/listView_main"
android:layout_below="@+id/button_main_init"
android:layout_width="match_parent"
android:layout_height="match_parent">
ListView>
<LinearLayout
android:id="@+id/layout_main_nextpage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#000"
android:visibility="invisible"
android:gravity="center"
android:onClick="clickButton"
android:padding="5dp">
<ProgressBar
android:id="@+id/progressBar_main"
style="?android:attr/progressBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/text_main_nextpage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:textSize="18sp"
android:onClick="clickButton"
android:textColor="#fff"
android:text="点击加载更多数据"/>
LinearLayout>
RelativeLayout>
publicclass MainActivity extends Activity {
privateStringTAG= "MainActivity";
privateListView listView_main;
privateLinearLayout layout_main_nextpage;
private MySQLiteDatabaseHelper dbHelper = null;
// 用于分页显示数据的属性
privateintpageSize= 30;// 每页显示的条数
privateintcurPage= 1;
privateintrowCount= 0;
privateintpageCount= 0;// 总页数
privatebooleanisBottom=false;// 判断是否滚动到数据最后一条
private List
private SimpleAdapter adapter = null;
@Override
protectedvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView_main = (ListView) findViewById(R.id.listView_main);
layout_main_nextpage = (LinearLayout) findViewById(R.id.layout_main_nextpage);
// 实例化访问数据库帮助类
dbHelper = new MySQLiteDatabaseHelper();
// 获取数据表一共有多少条,从而计算共有多少页
rowCount=dbHelper.selectCount("select id from android_basic",null);
// 计算总页码数
pageCount = (int) Math.ceil(rowCount / (float) pageSize);
// 如果当前页为第一页,则数据源集合中就是第一页的内容
if (curPage == 1) {
totalList = getCurpageList(1);
}
adapter = new SimpleAdapter(this, totalList,
R.layout.item_listview_main, new String[] { "_id", "title" },
newint[] { R.id.text_item_listview_id,
R.id.text_item_listview_title});
listView_main.setAdapter(adapter);
// 给ListView对象设置滚动监听器,以此来判断是否已经滚动到最后一条,从而决定是否加载新数据
listView_main.setOnScrollListener(new OnScrollListener() {
@Override
publicvoid onScrollStateChanged(AbsListView view, int scrollState) {
if (isBottom) {
// 如果滚到最后一条数据(即:屏幕最底端),则显示:“加载更多新数据”
if(curPage < pageCount) {
layout_main_nextpage.setVisibility(View.VISIBLE);
}
} else {
layout_main_nextpage.setVisibility(View.GONE);
}
}
@Override
publicvoid onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
// Log.i(TAG, "==" + firstVisibleItem + ":::" + visibleItemCount
// + ":::" + totalItemCount);
// 判断是否已经滚动到了最后一条,从而决定是否提示加载新数据
isBottom = (firstVisibleItem + visibleItemCount == totalItemCount);
}
});
}
publicvoid clickButton(View view) {
switch (view.getId()) {
caseR.id.layout_main_nextpage:
// Log.i(TAG, "==" + curPage + ":::" + pageCount);
// 如果不是最后一页,则让当前页码累加,让数据源累加新数据,并通知适配器信息发生变化
if(curPage < pageCount) {
curPage++;
totalList.addAll(getCurpageList(curPage));
adapter.notifyDataSetChanged();
}
// 只要点击了提示“加载新数据”的信息,就让其隐藏
layout_main_nextpage.setVisibility(View.GONE);
break;
default:
break;
}
}
// 获取每一页的数据,返回List集合
private List
int offset = (currentPage - 1) * pageSize;
String sql = "select id _id ,title from android_basic limit ? , ?";
returndbHelper.selectData(sql, new String[] { offset + "",
pageSize + "" });
}