Android ListView分頁加載數據Demo

 一個簡單的Android分頁加載數據的實例,實例中加載方式是滑動到底部自動加載下頁數據,現在加載方式可以是點擊拖拽一個自己定義的控件。
    下面是一個主要的Activity代碼:
01 public class MainActivity extends Activity {
02     private ListView listview;
03     private View footer;
04     private List<String> data;
05     private ArrayAdapter<String> adapter;
06 
07     @Override
08     public void onCreate(Bundle savedInstanceState) {
09         super.onCreate(savedInstanceState);
10         setContentView(R.layout.main);
11         listview = (ListView) findViewById(R.id.listview);
12         // 給adapter添加數據
13         data = new DataService().getData();
14         adapter = new ArrayAdapter<String>(this, R.layout.item, R.id.tv_name, data);
15         // 必須在setAdapter之前把head和Footer設置好
16         footer = getLayoutInflater().inflate(R.layout.footer, null);
17         listview.addFooterView(footer);
18 
19         // listview設置adpater
20         listview.setAdapter(adapter);
21         listview.removeFooterView(footer);
22         // 以上就可以顯示ListView上的數據,可是不能分頁顯示,沒有給LIstView添加滾動事件,
23         // 下面我們來給ListVIew添加滾動事件,從而達到自動刷新ListView分頁加載數據的功能
24         listview.setOnScrollListener(new MyOnScrollListener());
25     }
26 
27     public final class MyOnScrollListener implements OnScrollListener {
28         protected static final int GET_DATA_SUCCESS = 0;
29         private int pageCount = 20;
30         private int pageSize = 5;
31         // 是否加載完成
32         Boolean finish = true;
33         private Handler mHandler = new Handler() {
34             public void handleMessage(Message msg) {
35                 msg.what = GET_DATA_SUCCESS;
36                 List<String> result = (List<String>) msg.obj;
37                 data.addAll(result);
38                 finish = true;
39                 if (listview.getFooterViewsCount() > 0) {
40                     listview.removeFooterView(footer);
41                 }
42                 // 通知適配器更新數據
43                 adapter.notifyDataSetChanged();
44             };
45         };
46 
47         @Override
48         public void onScrollStateChanged(AbsListView view, int scrollState) {
49 
50         }
51 
52         @Override
53         public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
54             // 計算當前加載上來的全部item數目
55             int totalItem = firstVisibleItem + visibleItemCount;
56             // 計算當前頁
57             int currenPage = totalItemCount / pageSize;
58             // 計算下一頁的數值
59             int nextPage = currenPage + 1;
60             // 當 當前加載過的item總數目等於總顯示過的Item數目的時候可以去加載
61             if (totalItem == totalItemCount) {
62                 if (nextPage < pageCount && finish) {
63                     finish = false;
64                     listview.addFooterView(footer);
65                     // 開始加載數據
66                     new Thread() {
67                         public void run() {
68                             SystemClock.sleep(3000);
69                             List<String> result = new DataService().getData();
70                             Message msg = new Message();
71                             msg.what = GET_DATA_SUCCESS;
72                             msg.obj = result;
73                             mHandler.sendMessage(msg);
74                         }   www.aiwalls.com
75 
76                     }.start();
77                 }
78             }
79         }
80     }
81 }
如下是提供數據的service,
01 public class DataService {
02     /*
03      * 每次生成一個20個元素的List列表返回
04      */
05     public List<String> getData() {
06         List<String> data = new ArrayList<String>();
07         for (int i = 0; i < 20; i++) {
08             data.add("當前item的id是:" + i);
09         }
10         return data;
11     }
12 }

 作者:Simpleness

發佈留言