Android實現(xiàn)ListView分頁自動加載數(shù)據(jù)的方法
Android應用開發(fā)中,采用ListView組件來展示數(shù)據(jù)是很常用的功能,當一個應用要展現(xiàn)很多的數(shù)據(jù)時,一般情況下都不會把所有的數(shù)據(jù)一次就展示出來,而是通過分頁的形式來展示數(shù)據(jù),個人覺得這樣會有更好的用戶體驗。因此,很多應用都是采用分批次加載的形式來獲取用戶所需的數(shù)據(jù)。例如:微博客戶端可能會在用戶滑動至列表底端時自動加載下一頁數(shù)據(jù),也可能在底部放置一個"查看更多"按鈕,用戶點擊后,加載下一頁數(shù)據(jù)。
下面通過一個Demo來展示ListView功能如何實現(xiàn):該Demo通過在ListView列表的底部添加一個“查看更多...”按鈕來加載新聞(模擬新聞客戶端)分頁數(shù)據(jù)。同時限定每次加載10條記錄,但完全加載完數(shù)據(jù)后,就把ListView列表底部視圖“查看更多...”刪除。假設加載的數(shù)據(jù)總數(shù)為 38 條記錄。先看下該Demo工程的程序結構圖:

其中包 com.andyidea.bean中News.java類是新聞實體類,包com.andyidea.listview中paginationListViewActivity.java類是用來展示ListView列表。布局layout中包含三個布局文件,分別為:list_item.xml , loadmore.xml , main.xml 。下面分別貼下源碼:
layout中的 list_item.xml源碼:
<span style="font-size:13px;"><?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <TextView android:id="@+id/newstitle" android:layout_width="fill_parent" android:layout_height="wrap_content"/> <TextView android:id="@+id/newscontent" android:layout_width="fill_parent" android:layout_height="wrap_content"/> </LinearLayout></span>
layout中l(wèi)oadmore.xml源碼:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <Button android:id="@+id/loadMoreButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="查看更多..." /> </LinearLayout>
layout中main.xml源碼:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="@+id/lvNews"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayou
包 com.andyidea.bean中News.java類源碼:
package com.andyidea.bean;
public class News {
private String title; //標題
private String content; //內容
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
包com.andyidea.listview中paginationListViewActivity.java類源碼:
package com.andyidea.listview;
import java.util.ArrayList;
import java.util.List;
import com.andyidea.bean.News;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class PaginationListViewActivity extends Activity implements OnScrollListener {
private ListView listView;
private int visibleLastIndex = 0; //最后的可視項索引
private int visibleItemCount; // 當前窗口可見項總數(shù)
private int datasize = 38; //模擬數(shù)據(jù)集的條數(shù)
private PaginationAdapter adapter;
private View loadMoreView;
private Button loadMoreButton;
private Handler handler = new Handler();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
loadMoreView = getLayoutInflater().inflate(R.layout.loadmore, null);
loadMoreButton = (Button)loadMoreView.findViewById(R.id.loadMoreButton);
loadMoreButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
loadMoreButton.setText("正在加載中..."); //設置按鈕文字
handler.postDelayed(new Runnable() {
@Override
public void run() {
loadMoreData();
adapter.notifyDataSetChanged();
loadMoreButton.setText("查看更多..."); //恢復按鈕文字
}
},2000);
}
});
listView = (ListView)findViewById(R.id.lvNews);
listView.addFooterView(loadMoreView); //設置列表底部視圖
initializeAdapter();
listView.setAdapter(adapter);
listView.setOnScrollListener(this);
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
int itemsLastIndex = adapter.getCount()-1; //數(shù)據(jù)集最后一項的索引
int lastIndex = itemsLastIndex + 1;
if (scrollState == OnScrollListener.SCROLL_STATE_IDLE
&& visibleLastIndex == lastIndex) {
// 如果是自動加載,可以在這里放置異步加載數(shù)據(jù)的代碼
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
this.visibleItemCount = visibleItemCount;
visibleLastIndex = firstVisibleItem + visibleItemCount - 1;
Log.e("========================= ","========================");
Log.e("firstVisibleItem = ",firstVisibleItem+"");
Log.e("visibleItemCount = ",visibleItemCount+"");
Log.e("totalItemCount = ",totalItemCount+"");
Log.e("========================= ","========================");
//如果所有的記錄選項等于數(shù)據(jù)集的條數(shù),則移除列表底部視圖
if(totalItemCount == datasize+1){
listView.removeFooterView(loadMoreView);
Toast.makeText(this, "數(shù)據(jù)全部加載完!", Toast.LENGTH_LONG).show();
}
}
/**
* 初始化ListView的適配器
*/
private void initializeAdapter(){
List<News> news = new ArrayList<News>();
for(int i=1;i<=10;i++){
News items = new News();
items.setTitle("Title"+i);
items.setContent("This is News Content"+i);
news.add(items);
}
adapter = new PaginationAdapter(news);
}
/**
* 加載更多數(shù)據(jù)
*/
private void loadMoreData(){
int count = adapter.getCount();
if(count+10 <= datasize){
for(int i=count+1; i<=count+10; i++){
News item = new News();
item.setTitle("Title"+i);
item.setContent("This is News Content"+i);
adapter.addNewsItem(item);
}
}else{
for(int i=count+1; i<=datasize; i++){
News item = new News();
item.setTitle("Title"+i);
item.setContent("This is News Content"+i);
adapter.addNewsItem(item);
}
}
}
class PaginationAdapter extends BaseAdapter{
List<News> newsItems;
public PaginationAdapter(List<News> newsitems){
this.newsItems = newsitems;
}
@Override
public int getCount() {
return newsItems.size();
}
@Override
public Object getItem(int position) {
return newsItems.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
if(view == null){
view = getLayoutInflater().inflate(R.layout.list_item, null);
}
//新聞標題
TextView tvTitle = (TextView)view.findViewById(R.id.newstitle);
tvTitle.setText(newsItems.get(position).getTitle());
//新聞內容
TextView tvContent = (TextView)view.findViewById(R.id.newscontent);
tvContent.setText(newsItems.get(position).getContent());
return view;
}
/**
* 添加數(shù)據(jù)列表項
* @param newsitem
*/
public void addNewsItem(News newsitem){
newsItems.add(newsitem);
}
}
}
最后,運行程序的結果截圖如下:

通過上面的截圖,當我們點擊"查看更多..."按鈕時,就會加載下10條記錄,當加載完所有的記錄后,ListView的底部視圖將會移除。
希望本文所述對大家學習Android軟件編程有所幫助。
相關文章
Android碎片fragment實現(xiàn)靜態(tài)加載的實例代碼
這篇文章主要介紹了Android碎片fragment實現(xiàn)靜態(tài)加載的實例代碼,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-11-11
Android—基于微信開放平臺v3SDK開發(fā)(微信支付填坑)
這篇文章主要介紹了Android—基于微信開放平臺v3SDK開發(fā)(微信支付填坑),具有一定的參考價值,有需要的可以了解一下。2016-11-11
Android 使用fast-verification實現(xiàn)驗證碼填寫功能的實例代碼
這篇文章主要介紹了Android 使用fast-verification實現(xiàn)驗證碼填寫功能,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-04-04
android圖像繪制(四)自定義一個SurfaceView控件
自定義控件(類似按鈕等)的使用,自定義一個SurfaceView。如某一塊的動態(tài)圖(自定義相應),或者類似UC瀏覽器下面的工具欄,感興趣的朋友可以了解下2013-01-01
Android下錄制App操作生成Gif動態(tài)圖的全過程
這篇文章主要為大家分享了Android下錄制App操作生成Gif動態(tài)圖的全過程,感興趣的小伙伴們可以參考一下2016-01-01
Android實現(xiàn)狀態(tài)欄白底黑字效果示例代碼
這篇文章主要介紹了Android實現(xiàn)狀態(tài)欄白底黑字效果的相關資料,實現(xiàn)后的效果非常適合日常開發(fā)中使用,文中給出了詳細的示例代碼供大家參考學習,需要的朋友們下面隨著小編來一起學習學習吧。2017-10-10

