實現效果:當滑動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;
- }
-
- }
- }
運行結果