Android_listview分頁載入更多

來源:互聯網
上載者:User

只要使用listview,那麼listview的非同步載入是必不可少的.
我們知道,如果整個手機螢幕只能完整顯示10條記錄,那麼adapter的getView就會調用10次,
也就是說,手機螢幕顯示了幾條資料,那麼getView就會被調用幾次...這個例子待會將會示範,注意往回看也是一樣的,
也就是listview把以前的顯示過的已經被回收了,這樣就可以顯示更多的條目了.所以這裡需要listview最佳化

public class ArticleListAdapter extends BaseAdapter {private List<Article> mArticles;private Context mContext;static View view = null;public ArticleListAdapter(Context context, List<Article> articles) {this.mArticles = articles;this.mContext = context;}@Overridepublic int getCount() {return mArticles.size();}@Overridepublic Object getItem(int position) {return mArticles.get(position);}@Overridepublic long getItemId(int position) {return position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {
System.out.println("我是getView,我被調用了");if (convertView == null) {convertView = LayoutInflater.from(mContext).inflate(R.layout.article_list_item, null);}view = convertView;Article article = mArticles.get(position);TextView textView = (TextView) view.findViewById(R.id.tv_article_title);textView.setText(article.getTitle());return view;}}

先上幾個例子圖片:



現在我們來驗證一下關於listview最佳化的問題.


從上一張圖可以看出,顯示了6個條目,現在在看看控制台輸出情況:


也就是getView被調用了6次,並且當我們不斷的把listview往下拉,getView不斷的被調用.

也就是顯示一次調用一次! 比如第一次調用了6次,那麼就建立了6個view對象,

並且getView的convertView參數就是代表老的view對象,

這裡我們為了避免重複建立對象可以使用如上的Adapter使用方式,判斷convertView是否為空白,如果不為空白則複用老對象...

現在再來看看listview的非同步載入,其實非同步載入很簡單,我們知道listview肯定會對應一個Adapter,Adapter對應一個集合,

我們只需要改變這個集合即可.比如,第一次載入了10,那麼當載入更多的時候,只需要把又載入到的10條記錄加到集合即可,

然後再調用adapter.notifyDataSetChanged()方法即可..listview就會在原來的的基礎上載入10條,代碼如下

這裡用的AsyncTask

@Overrideprotected Object doInBackgroundTask2(String... params) {//在子線程執行String url = params[0];Source subSource = getSource(url);int index = pager * 25;List<Article> articles = articleDao.getArticleByParentIDPage(index,parent_id);if (articles != null && articles.size() == 25) {mSubArticles.addAll(articles);} else {if (subSource == null)return null;List<Element> subElements = subSource.getAllElementsByClass("leftList").get(1).getChildElements().get(0).getChildElements();int subSize = subElements.size();for (int i = 0; i < subSize; i++) {Article sub = new Article();Element href = subElements.get(i).getFirstElement("a");String link = href.getAttributeValue("href");String title = href.getTextExtractor().toString().trim();sub.setTitle(title);sub.setLink(link, true);mSubArticles.add(sub);// 緩衝到本機資料庫if (parent_id != -1) {sub.setId(parent_id);articleDao.add(sub);}}}Element custom = subSource.getAllElementsByClass("custom").get(0);pager_info = custom.getRenderer().toString();return custom;}@Overrideprotected void onPostTask2(Object result) {//執行完doInBackgroundTask2後,執行該方法//pd_tip.setVisibility(View.GONE);pd.dismiss();load_more_view.setVisibility(View.VISIBLE);if (result == null)return;load_more.setText(pager_info);if (adapter != null) {adapter.notifyDataSetChanged();}}

歡迎轉載: http://write.blog.csdn.net/postedit/7634238

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.