效果图
<1>
当前listview里的原始数据
![](http://dl.iteye.com/upload/attachment/610444/f7ac76db-354f-35ec-9ecd-8f222c0d9bb0.bmp)
<2>
当滑动到最低端时候,开始加载新数据,出现loading
![](http://dl.iteye.com/upload/attachment/610448/b79c28bd-3f11-3778-b978-92dd1484c365.bmp)
<3>
新数据加载后显示
![](http://dl.iteye.com/upload/attachment/610446/bc0f01aa-a7a8-3812-8004-5ac63176cbd5.bmp)
具体实现
写一个适配器,一般会自己继承,然后重新一些东西
public class MyAdapter extends ArrayAdapter<bean>{
private final LayoutInflater mInflater;
public MyAdapter(Context context, List<bean> apps) {
super(context, 0, apps);
mInflater = LayoutInflater.from(context);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
bean bean = this.getItem(position);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.item, parent, false);
}
TextView data = (TextView) convertView.findViewById(R.id.text);
data.setText(bean.getName());
return convertView;
}
}
activity加载数据,新数据一般是当你第一次默认显示的数据已经完全显示,然后滚轮拖动到屏幕下端时候加载,也就是
firstVisibleItem + visibleItemCount = totalItemCount的时候,此时才启动线程开始加载数据。
public class LoadActivity extends Activity {
private ListView lv = null;
ArrayList<bean> beans = new ArrayList<bean>();
MyAdapter adapter = null;
boolean isRefresh = true;
boolean isFirstRun = true;
private bean bean=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.refresh);
lv = (ListView) findViewById(R.id.listview);
lv.setOnScrollListener(new OnScrollListener() {
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
Log.i("v", "" + visibleItemCount);
Log.i("f", "" + firstVisibleItem);
Log.i("t", "" + totalItemCount);
//第一次进入时候,如果当前显示页面放不下第一次的所有数据,会分页显示
//就算滚轮到最低端,也不会加载的新的数据,只有当第一次数据显示完后才会刷新出现loading
//如果没有下面的判断,在你进入这个listview就会直接加载一次新数据,在这个例子里,比如你第一次加载的事40条数据,会变成45条
if (isFirstRun) {
isFirstRun = false;
return;
}
if ((firstVisibleItem + visibleItemCount) == totalItemCount
&& isRefresh) {
isRefresh = false;
//loading条显示出来
findViewById(R.id.loadtext).setVisibility(View.VISIBLE);
new LoadThread().start();
}
}
public void onScrollStateChanged(AbsListView view, int scrollState) {
});
adapter = new MyAdapter(this.getBaseContext(), getFirstBean());
lv.setAdapter(adapter);
}
//第一次加载的数据
private ArrayList<bean> getFirstBean(){
bean =new bean();
for (int i = 0; i < 20; i++) {
bean = new bean();
bean.setName("Json " + i + " 号");
beans.add(bean);
}
return beans;
}
//需要加载的新数据
private void refreshListView() {
if (beans == null || adapter == null) {
return;
}
bean bean = null;
for (int i = 0; i < 5; i++) {
bean = new bean();
bean.setName("新数据: " + i);
beans.add(bean);
}
}
Handler _handler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
adapter.notifyDataSetChanged();
//当数据已经改变后loading条不显示出来
findViewById(R.id.loadtext).setVisibility(View.INVISIBLE);
isRefresh = true;
break;
case 2:
}
}
};
@Override
public void run() {
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//传给handler处理
_handler.sendEmptyMessage(1);
}
}
}