实现效果:当滑动ListView列表最底端时,动态的添加新的列表项
实现步骤:
调用ListView的setOnScrollListener()方法设置滑动监听器,实现OnScrollListener接口的方法
判断当列表滑动到最低端时,加载新的列表项
接口OnScrollListener
onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount)
view 报告滑动状态的视图
firstVisibleItem 可视的第一个列表项的索引
visibleItemCount 可视的列表项个数
totalItemCount 总共的列表项个数
onScrollStateChanged(AbsListView view, int scrollState)
view 报告滑动状态的视图
scrollState 滑动状态
滑动状态
SCROLL_STATE_IDLE : 0 视图没有滑动
SCROLL_STATE_TOUCH_SCROLL : 1 用户正在触摸滑动,手指仍在屏幕上
SCROLL_STATE_FLING : 2 用户之前触摸滑动,现在正在滑行,直到停止
示例代码:
package dyingbleed.iteye; import Android.app.ListActivity; 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.BaseAdapter; import android.widget.ListView; import android.widget.TextView; public class Main extends ListActivity { private ListView listView; private MyListViewAdapter adapter; private int scrollState; private View footerView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); listView = getListView(); footerView = LayoutInflater.from(this).inflate(R.layout.progress, null); listView.addFooterView(footerView); //在ListView底部添加正在加载视图 //注意:addFooterView方法需要在调用setListAdapter方法前调用! adapter = new MyListViewAdapter(); setListAdapter(adapter); getListView().setOnScrollListener(new AbsListView.OnScrollListener() { @Override public void onScrollStateChanged(AbsListView view, int scrollState) { Main.this.scrollState = scrollState; } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { int lastVisibleItem = firstVisibleItem + visibleItemCount - 1; //可视的最后一个列表项的索引 /* * 当列表正处于滑动状态且滑动到列表底部时,执行 * */ if(Main.this.scrollState != AbsListView.OnScrollListener.SCROLL_STATE_IDLE && lastVisibleItem == totalItemCount - 1) { /* * 执行线程,睡眠5秒钟后添加10个列表项 * */ new Thread() { private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); adapter.count += 10; adapter.notifyDataSetChanged(); } }; @Override public void run() { super.run(); try { sleep(5000); handler.sendEmptyMessage(0); } catch (InterruptedException e) { e.printStackTrace(); } } }.start(); } } }); } private class MyListViewAdapter extends BaseAdapter { int count = 50; //初始列表项数量 @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) { TextView result = (TextView) convertView; if(result == null) { result = new TextView(Main.this); result.setTextAppearance(Main.this, android.R.style.TextAppearance_Large); AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams(AbsListView.LayoutParams.FILL_PARENT, AbsListView.LayoutParams.WRAP_CONTENT); result.setLayoutParams(layoutParams); result.setGravity(Gravity.CENTER); } result.setText("Item "+(position+1)); return result; } } }运行结果