Android 中 ListView 分頁(yè)加載數(shù)據(jù)
40 }
41 TextView text = (TextView) view.findViewById(R.id.list_item_text);
42 text.setText(items.get(position));
43 return view;
44 }
45
46 /**
47 * 添加列表項(xiàng)
48 * @param item
49 */
50 public void addItem(String item) {
51 items.add(item);
52 }
53}
這個(gè)ListViewAdapter是我們自定義適配器,它繼承自BaseAdapter,實(shí)例化此適配器需要一個(gè)Context對(duì)象來(lái)獲取 LayoutInflater實(shí)例和一個(gè)集合對(duì)象來(lái)充當(dāng)適配器的數(shù)據(jù)集;在getView方法中我們填充list_item.xml布局文件,完成列表每 一項(xiàng)的數(shù)據(jù)顯示;addItem方法用來(lái)在加載數(shù)據(jù)時(shí)向數(shù)據(jù)集中添加新數(shù)據(jù)。
最后我們來(lái)看一下MainActivity:
001package com.scott.loadmore;
002
003import java.util.ArrayList;
004
005import android.app.ListActivity;
006import android.os.Bundle;
007import android.os.Handler;
008import android.util.Log;
009import android.view.View;
010import android.widget.AbsListView;
011import android.widget.AbsListView.OnScrollListener;
012import android.widget.Button;
013import android.widget.ListView;
014
015public class MainActivity extends ListActivity implements OnScrollListener {
016 private ListView listView;
017 private int visibleLastIndex = 0; //最后的可視項(xiàng)索引
018 private int visibleItemCount; // 當(dāng)前窗口可見(jiàn)項(xiàng)總數(shù)
019 private ListViewAdapter adapter;
020 private View loadMoreView;
021 private Button loadMoreButton;
022 private Handler handler = new Handler();
023
024 @Override
025 public void onCreate(Bundle savedInstanceState) {
026 super.onCreate(savedInstanceState);
027 setContentView(R.layout.main);
028
029 loadMoreView = getLayoutInflater().inflate(R.layout.load_more, null);
030 loadMoreButton = (Button) loadMoreView.findViewById(R.id.loadMoreButton);
031
032 listView = getListView(); //獲取id是list的ListView
033
034 listView.addFooterView(loadMoreView); //設(shè)置列表底部視圖
035
036 initAdapter();
037
038 setListAdapter(adapter); //自動(dòng)為id是list的ListView設(shè)置適配器
039
040 listView.setOnScrollListener(this); //添加滑動(dòng)監(jiān)聽(tīng)
041 }
042
043 /**
044 * 初始化適配器
045 */
046 private void initAdapter() {
047 ArrayList
048 for (int i = 0; i 10; i++) {
049 items.add(String.valueOf(i + 1));
050 }
051 adapter = new ListViewAdapter(this, items);
052 }
053
054 /**
055 * 滑動(dòng)時(shí)被調(diào)用
056 */
057 @Override
058 public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,int totalItemCount) {
059 this.visibleItemCount = visibleItemCount;
060 visibleLastIndex = firstVisibleItem + visibleItemCount - 1;
061 }
062
063 /**
064 * 滑動(dòng)狀態(tài)改變時(shí)被調(diào)用
065 */
066 @Override
067 public void onScrollStateChanged(AbsListView view, int scrollState) {
068 int itemsLastIndex = adapter.getCount() - 1; //數(shù)據(jù)集最后一項(xiàng)的索引
069 int lastIndex = itemsLastIndex + 1; //加上底部的loadMoreView項(xiàng)
070 if (scrollState == OnScrollListener.SCROLL_STATE_IDLE visibleLastIndex == lastIndex) {
071 //如果是自動(dòng)加載,可以在這里放置異步加載數(shù)據(jù)的代碼
072 Log.i(LOADMORE, loading...);
073 }
074 }
075
076 /**
077 * 點(diǎn)擊按鈕事件
078 * @param view
079 */
080 public void loadMore(View view) {
081 loadMoreButton.setText(loading...); //設(shè)置按鈕文字loading
082 handler.postDelayed(new Runnable() {
083 @Override
084 public void run() {
085
086 loadData();
087
088 adapter.notifyDataSetChanged(); //數(shù)據(jù)集變化后,通知adapter
089 listView.setSelection(visibleLastIndex - visibleItemCount + 1); //設(shè)置選中項(xiàng)
090
091 loadMoreButton.setText(load more); //恢復(fù)按鈕文字
092 }
093 }, 2000);
094 }
095
096 /**
097 * 模擬加載數(shù)據(jù)
098 */
099 private void loadData() {
100 int count = adapter.getCount();
101 for (int i = count; i count + 10; i++) {
102 adapter.addItem(String.valueOf(i + 1));
103 }
104 }
105}
如代碼所示,我們?cè)趏nCreate方法被調(diào)用時(shí)獲取listView組件,設(shè)置其底部視圖為loadMoreView,它包含一個(gè)按鈕,點(diǎn)擊時(shí)會(huì)觸發(fā) loadMore方法調(diào)用,另外在為listView設(shè)置完適配器時(shí),又為其設(shè)置了滑動(dòng)事件監(jiān)聽(tīng)器,滑動(dòng)列表時(shí)onScroll會(huì)被調(diào)用,滑動(dòng)狀態(tài)改變時(shí) onScrollStateChanged會(huì)被調(diào)用。
我們來(lái)演示一下這個(gè)加載過(guò)程:
評(píng)論