在使用ListView時,會遇到當ListView列表滑動到最底端時,添加新的列表項的問題,本文通過代碼演示如何動態的添加新的列表項到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 用戶之前觸摸滑動,現在正在滑行,直到停止
下面通過代碼給大家演示效果
- <?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/android:list"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:drawSelectorOnTop="false"
- />
- </LinearLayout>
// footer.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:gravity="center"
- android:orientation="horizontal"
- >
- <ProgressBar
- android:id="@+id/progressbar"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- style="\?android:attr/progressBarStyleSmall"
- />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textSize="20.0sp"
- android:text="正在加載..."
- />
- </LinearLayout>
然後是主代碼:
- public class MainActivity extends ListActivity
- {
- private ListView mListView;
- private CustomAdapter mAdapter;
- private int mScrollState;
- private View mFooter;
-
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
-
- //獲取ListView
- mListView = getListView();
- //根據footer.xml描述創建View
- mFooter = getLayoutInflater().inflate(R.layout.footer, null);
- // 在ListView底部添加正在加載視圖
- //注意:addFooterView方法需要在調用setListAdapter方法前調用!
- mListView.addFooterView(mFooter);
- mAdapter = new CustomAdapter();
- setListAdapter(mAdapter);
- //給ListView添加滾動監聽器
- mListView.setOnScrollListener(new AbsListView.OnScrollListener()
- {
- @Override
- public void onScrollStateChanged(AbsListView view, int scrollState)
- {
- //記錄當前狀態
- mScrollState = scrollState;
- }
-
- @Override
- public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount)
- {
- // 可視的最後一個列表項的索引
- int lastVisibleItem = firstVisibleItem + visibleItemCount - 1;
- //當列表正處於滑動狀態且滑動到列表底部時,執行
- if (mScrollState != 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);
- //增加Item數量
- mAdapter.count += 10;
- //通知數據集變化
- mAdapter.notifyDataSetChanged();
- }
-
- };
-
- @Override
- public void run()
- {
- super.run();
- try
- {
- sleep(5000);
- handler.sendEmptyMessage(0);
- } catch (InterruptedException e)
- {
- e.printStackTrace();
- }
- }
-
- }.start();
- }
- }
- });
- }
-
- private class CustomAdapter extends BaseAdapter
- {
- // 初始列表項數量
- int count = 20;
-
- @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;
- //動態創建TextView添加早ListView中
- if (result == null)
- {
- result = new TextView(MainActivity.this);
- result.setTextAppearance(MainActivity.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("第 " + (position + 1)+"行");
- return result;
- }
- }
- }
效果預覽:
Android中動態更新ListView源碼下載
免費下載地址在 http://linux.linuxidc.com/
用戶名與密碼都是www.linuxidc.com
具體下載目錄在 /2012年資料/8月/4日/Android中動態更新ListView【附源碼】/