package com.demo;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
public class MainActivity extends Activity implements OnScrollListener,OnItemClickListener {
private ListView listView;
private List<String> currentData;//当前视图显示的数据
private CustomAdapter customadapter;//自定义适配器
private View loadingView;//加载视图的布局
private int currentPage = 1;//当前页,默认为1
private int pageSize = 10;//每页显示十条信息
private int last_item_position;//最后item的位置
private boolean isLoading = false;//是否加载过,控制加载次数
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listView=(ListView)findViewById(R.id.lv_id);
//加载视图布局
loadingView = LayoutInflater.from(this).inflate(R.layout.list_page_load, null);
//创建当前用于显示视图的数据
currentData=CreateDataFactory.createUpdateData(currentPage,pageSize);
//添加底部加载视图
listView.addFooterView(loadingView);
//初始化适配器
customadapter=new CustomAdapter();
listView.setAdapter(customadapter);
//滚动条监听
listView.setOnScrollListener(this);
//ListView监听
listView.setOnItemClickListener(this);
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
last_item_position = firstVisibleItem + visibleItemCount - 1;
if(last_item_position==totalItemCount-2){//这里控制当焦点落在某一个位置时,开始加载.
//当前是在第9个位置开始加载,改为totalItemCount-1
//则会在第10个位置开始加载
System.out.println("开始加载..");
Toast.makeText(MainActivity.this, "第 "+currentPage+" 页", Toast.LENGTH_LONG).show();
if(!isLoading){
new Thread(){
public void run() {
isLoading = true;
//开启一个线程加载数据,否则会堵塞当前线程
updateCurrendData();
//发送一个消息,通知数据加载完成
loadingHandler.sendEmptyMessage(0);
}
}.start();
}
}
//当ListView没有FooterView时,添加FooterView(---loadingView---)
if(listView.getFooterViewsCount()==0){
listView.addFooterView(loadingView);
}
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
}
Handler loadingHandler = new Handler(){
public void handleMessage(android.os.Message msg) {
//修改adapter count
customadapter.count = currentPage*pageSize;
//更新
customadapter.notifyDataSetChanged();
//消除MSG
loadingHandler.removeMessages(0);
//删除FooterView
listView.removeFooterView(loadingView);
//进入下一页,此时视图未加载.
isLoading = false;
};
};
//添加List元素
private void updateCurrendData(){
//模拟联接网络以及从网络中获取数据花费的时间
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//更新一次,当前页加1
currentPage++;
//获取当前要更新的数据
List<String> updateDataList = CreateDataFactory.createUpdateData(currentPage, pageSize);
//需要更新的数据加入当前数据集合
for(String itemData:updateDataList){
currentData.add(itemData);
}
}
class CustomAdapter extends BaseAdapter{
int count=currentData.size();
@Override
public int getCount() {
// TODO Auto-generated method stub
return count;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view=LayoutInflater.from(MainActivity.this).inflate(R.layout.list_page_item, null);
TextView tv_content = (TextView) view.findViewById(R.id.tv_content);
tv_content.setText(currentData.get(position));
return view;
}
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
}
}
- 1
- 2
前往页