package rw.listview;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AbsListView.OnScrollListener;
public class listviewActivity extends Activity implements OnScrollListener {
private listViewAdapter adapter ;
ListView listView;
private Thread mThread;
private LayoutInflater layoutInflater;
private View footerView;
List<String> olist = new ArrayList<String>();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
}
private void init() {
//LayoutInflater是用来找layout下xml布局文件,并且实例化!
layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
footerView = layoutInflater.inflate(R.layout.list, null);
// 得到一个ListView用来显示条目
listView = (ListView) findViewById(R.id.tv);
// 添加到脚页显示,当光标到最下面的是的时候,出发这个方法
listView.addFooterView(footerView);
olist.add("000000");
olist.add("111111");
olist.add("222222");
olist.add("333333");
olist.add("444444");
olist.add("555555");
olist.add("666666");
olist.add("777777");
olist.add("888888");
olist.add("999999");
olist.add("-------------------");
adapter = new listViewAdapter(this, 0, olist);
// 给ListView添加适配器
listView.setAdapter(adapter);
// 给ListView注册滚动监听
listView.setOnScrollListener(this);
}
/**
* 生成显示数据
*/
class listViewAdapter extends ArrayAdapter<String> {
private List<String> myList;
int count = 10;
public String getItem(int pos) {
return myList.get(pos);
}
public listViewAdapter(Context context, int textViewResourceId, List<String> objects) {
super(context, textViewResourceId, objects);
this.myList = objects;
}
public View getView(int pos, View v, ViewGroup p) {
TextView view;
if (v == null) {
view = new TextView(listviewActivity.this);
} else {
view = (TextView) v;
}
view.setText("ListItem" + getItem(pos));
view.setTextSize(20f);
view.setGravity(Gravity.CENTER);
view.setHeight(60);
return view;
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub
//当前页面可以看见的第一个ListItem+当前所有看见的ListItem=适配器上总共的ListItem,说明Scroll到最下面了
if(firstVisibleItem+visibleItemCount==totalItemCount)
{
//开线程去下载网络数据
if (mThread == null || !mThread.isAlive()) {
mThread = new Thread() {
@Override
public void run() {
try {
if (adapter.count <= 49) {
//这里放你网络数据请求的方法,我在这里用线程休眠5秒方法来处理
olist.add("1 &" + adapter.count);
olist.add("2 &" + adapter.count);
olist.add("3 &" + adapter.count);
olist.add("4 &" + adapter.count);
olist.add("5 &" + adapter.count);
olist.add("6 &" + adapter.count);
olist.add("7 &" + adapter.count);
olist.add("8 &" + adapter.count);
olist.add("9 &" + adapter.count);
olist.add("------------------->");
}
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//用handler 传递线程
Message message = new Message();
message.what = 1;
handler.sendMessage(message);
}
};
mThread.start();
}
}
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
}
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
switch (msg.what) {
case 1:
if (adapter.count <= 49) {
adapter.count += 10;
int currentPage = adapter.count / 10;
Toast.makeText(getApplicationContext(),"第" + currentPage + "页", Toast.LENGTH_LONG).show();
} else {
listView.removeFooterView(footerView);
Toast.makeText(getApplicationContext(),"木有数据了!", Toast.LENGTH_LONG).show();
}
//重新刷新Listview的adapter里面数据
adapter.notifyDataSetChanged();
break;
default:
break;
}
}
};
// private List<Map<String, Object>> list=
}
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/tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>
文件list.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="wrap_content"
android:orientation="vertical">
<LinearLayout android:id="@+id/linearLayout1"
android:layout_gravity="top|center_horizontal"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1"
android:baselineAligned="true">
<ProgressBar android:layout_height="wrap_content"
android:id="@+id/progressBar1"
android:layout_width="wrap_content" />
<TextView android:text="加载中.."
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center_vertical"
android:id="@+id/textView1" />
</LinearLayout>
</LinearLayout>