1.显示一页数据。一页数据超过了整个屏幕。
2.向下滑动ListView,到当前页最后一个数据时候,显示ListView的页脚FooterView(lin);(Loading..图标)(此时用到OnScrollListener监听,并在此启动判断是否到本页最后一条数据,如果是最后一条数据,则下载后续数据)
3.启动后台线程下载数据,并把下载数据通过Message发送到目标队列里。
4.在handleMessage中更新UI((BaseAdapter)madp.notifyDataSetChanged();)
5.到最后一页的时候去掉Loding图标
(范例-------------------------------------------------------------------------------------------------------)
package com.test.wt;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.test.R;
public class LoadingList extends Activity implements OnScrollListener {
private ListView listview;
private MAdapter content;
private ProgressBar progressBar;
private LinearLayout lin;
private Handler mhandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listi);
init();
}
private void init() {
// 进度条
progressBar = new ProgressBar(this);
// 进度条显示位置
progressBar.setPadding(0, 0, 0, 0);
LayoutParams lp = new LayoutParams(30, 30);
progressBar.setLayoutParams(lp);
lin = new LinearLayout(this);
lin.addView(progressBar, lp);
// 把进度条加入到layout中
listview = (ListView) findViewById(R.id.listviewloading);
listview.addFooterView(lin);
content = new MAdapter();
listview.setAdapter(content);
listview.setOnScrollListener(this);
}
class MAdapter extends BaseAdapter {
public int count = 9;// 初始化显示数据条数
@Override
public int getCount() {
return count;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
System.out.println("----------------------------getView "
+ position);
TextView view;
if (convertView == null) {
view = new TextView(LoadingList.this);
} else {
view = (TextView) convertView;
}
view.setText("ListItem " + position);
view.setTextSize(20f);
view.setGravity(Gravity.CENTER);
view.setHeight(60);
return view;
}
}
class onClickLisentner implements OnClickListener {
@Override
public void onClick(View v) {
Button but = (Button) v;
if (Integer.valueOf(but.getTag().toString()) == 1) {
System.out.println("-------------------------onClickLisentner");
content.count += 10;
content.notifyDataSetInvalidated();
}
}
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
System.out.println("----onScroll out");
if ((firstVisibleItem + visibleItemCount) == totalItemCount)// 判断是否到最后一条数据
{
System.out.println("----onScroll in");
mhandler = new MyHandler(content);
Thread t = new Thread(new MyThread(mhandler, content.getCount()));
t.start();
}
}
class MyThread implements Runnable {
private int mp;
private Handler h;
public MyThread(Handler h, int v) {
this.mp = v;
this.h = h;
}
@Override
public void run() {
System.out.println("------ run for");
mp += 10;
try {
Thread.currentThread().sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("------ run aft");
Message msg = new Message();
msg.what = mp;
msg.setTarget(h);
msg.sendToTarget();
}
}
class MyHandler extends Handler {
private MAdapter madp;
private MyHandler(MAdapter madp) {
this.madp = madp;
}
@Override
public void handleMessage(Message msg) {
System.out.println("------ handleMessage aft");
madp.count = msg.what;
madp.notifyDataSetChanged();
}
}
}