mian.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"
android:orientation="vertical" >
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/listView" />
</LinearLayout>
listview_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="18sp" />
</LinearLayout>
页脚,模拟加载进度footer.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ProgressBar android:id="@+id/c81_forthBar"
android:layout_width="50dp"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyle" />
<TextView android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:textSize="20sp"
android:text="@string/loadtext"
/>
</LinearLayout>
com.lzb.service.DataService
package com.lzb.service;
import java.util.ArrayList;
import java.util.List;
public class DataService {
public static List<String> getData(int offset, int maxResult){
List<String> data = new ArrayList<String>();
for(int i=0 ; i < 20 ; i++){
data.add("数据"+ i);
}
return data;
}
}
com.lzb.datapageload.MainActivity
package com.lzb.datapageload;
import java.util.ArrayList;
import java.util.List;
import com.lzb.service.DataService;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
@SuppressLint("HandlerLeak")
public class MainActivity extends Activity {
private ListView listView;
private List<String> data = new ArrayList<String>();
private ArrayAdapter<String> adapter;
private View footer;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
footer = getLayoutInflater().inflate(R.layout.footer, null);
listView = (ListView) this.findViewById(R.id.listView);
listView.setOnScrollListener(new ScrollListener()); //监听是否滑动尽头,是的话就加载新的内容
data.addAll(DataService.getData(0, 20));
adapter = new ArrayAdapter<String>(this, R.layout.listview_item,R.id.textView, data);
listView.addFooterView(footer);// 添加页脚(放在ListView最后)
listView.setAdapter(adapter);
listView.removeFooterView(footer); //删除页脚
}
private int number = 20;// 每次获取多少条数据
private int maxpage = 5;// 总共有多少页
private boolean loadfinish = true;
private final class ScrollListener implements OnScrollListener {
public void onScrollStateChanged(AbsListView view, int scrollState) {
Log.i("MainActivity", "onScrollStateChanged(scrollState="
+ scrollState + ")");
}
//开始先运行这个方法
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
Log.i("MainActivity", "onScroll(firstVisibleItem="
+ firstVisibleItem + ",visibleItemCount="
+ visibleItemCount + ",totalItemCount=" + totalItemCount
+ ")");
final int loadtotal = totalItemCount;
int lastItemid = listView.getLastVisiblePosition();// 获取当前屏幕最后Item的ID
if ((lastItemid + 1) == totalItemCount) {// 达到数据的最后一条记录
if (totalItemCount > 0) {
// 当前页
int currentpage = totalItemCount % number == 0 ? totalItemCount
/ number : totalItemCount / number + 1;
int nextpage = currentpage + 1;// 下一页
if (nextpage <= maxpage && loadfinish) { //当数据加载完
loadfinish = false;
listView.addFooterView(footer);
new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(3000); //只是模拟在网络加载数据,延迟时间
} catch (InterruptedException e) {
e.printStackTrace();
}
List<String> result = DataService.getData(loadtotal, number);
handler.sendMessage(handler.obtainMessage(100,result));
}
}).start();
}
}
}
}
}
private Handler handler = new Handler() { //数据必须在主线程中加载
@SuppressWarnings("unchecked")
public void handleMessage(Message msg) {
data.addAll((List<String>) msg.obj);
adapter.notifyDataSetChanged();// 告诉ListView数据已经发生改变,要求ListView更新界面显示
if (listView.getFooterViewsCount() > 0)
listView.removeFooterView(footer);
loadfinish = true;
}
};
}