android 實現listview動態載入清單項目__listview

來源:互聯網
上載者:User

最近了一個動態載入listview類表項的列子,分享出來大家學習學習,我說說這個例子的實現過程,首先我限定每次載入的清單項目資料為10條資料,當我拖動listview滾動到最後一條資料的時候再載入10條,並在Listview下方顯示載入提示。

下面是我的java源碼:

 private void showContent() { listView = (ListView) findViewById(R.id.journals_list_one); loadData(); adapter = new MyListAdapter(this, data); footerView = View.inflate(TestLayout.this, R.layout.journal_listitem_loading_bar, null); listView.addFooterView(footerView, null, true); listView.setAdapter(adapter); listView.setOnScrollListener(listener); } private AbsListView.OnScrollListener listener = new AbsListView.OnScrollListener() { @Override public void onScrollStateChanged(AbsListView view, int scrollState) { if (view.getLastVisiblePosition() == view.getCount() - 1) { loadData(); adapter.notifyDataSetChanged(); } } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { } }; /** * 構造List列表資料 */ private void loadData() { if (data.size() <= 40) { for (int i = 0; i < 10; i++) { Map<String, String> map = new HashMap<String, String>(); map.put("title", getString(R.string.title)); map.put("desc", getString(R.string.content)); data.add(map); } } else { listView.removeFooterView(footerView); } }

實現這個功能主要是添加ListView的OnScrollListener監聽,判斷是否清單項目到了最有一條,如果是最後一條就載入資料。載入資料時的載入效果是ListView.addFooterView()方法實現這個方法必須在listview.setAdapter()前才有用,當所有資料載入完成就調用listView.removeFooterView把footerview移除就可以了.

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.