[Android]如何?無限滾動的ListViw/GridView(翻譯)

來源:互聯網
上載者:User

ListView和GridView已經成為原生的Android應用實現中兩個最流行的設計模式。目前,這些模式被大量的開發人員使用,主要是因為他們是簡單而直接的實現,同時他們提供了一個良好,整潔的使用者體驗。

對於ListView和GridView一個共同的需求就是在使用者不斷向下滾動,組件仍能動態載入更多地載入更多資料。這篇部落格就將帶領大家實現在ListView和GridView中這個功能。

我們需要的主要組件就是我們的InfiniteScrollListener類,這個類是繼承於OnScrollListener,就讓我們來看下具體的代碼實現:

public abstract class InfiniteScrollListener implements AbsListView.OnScrollListener {    private int bufferItemCount = 10;    private int currentPage = 0;    private int itemCount = 0;    private boolean isLoading = true;     public InfiniteScrollListener(int bufferItemCount) {        this.bufferItemCount = bufferItemCount;    }     public abstract void loadMore(int page, int totalItemsCount);     @Override    public void onScrollStateChanged(AbsListView view, int scrollState) {        // Do Nothing    }     @Override    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount)    {        if (totalItemCount < itemCount) {            this.itemCount = totalItemCount;            if (totalItemCount == 0) {                this.isLoading = true; }        }         if (isLoading && (totalItemCount > itemCount)) {            isLoading = false;            itemCount = totalItemCount;            currentPage++;        }         if (!isLoading && (totalItemCount - visibleItemCount)<=(firstVisibleItem + bufferItemCount)) {            loadMore(currentPage + 1, totalItemCount);            isLoading = true;        }    }}

 

// Attach the listener to the AdapterView onCreateyourListView.setOnScrollListener(new InfiniteScrollListener(5) {    @Override    public void loadMore(int page, int totalItemsCount) {        List<HashMap<String, String>> newData = loader.loadData();        dataList.addAll(newData);        adapter.notifyDataSetChanged();    }});

如上,我們已經將這個類作為抽象類別,這是一個很好的設計。我們的InfiniteScrollListener類實現了onScroll()方法,但是沒有實現loadMore(),而是又給實作類別來實現。

onScroll()方法在我們滾動時,程式會自動調用。所以我們推薦在這個方法裡,不要進行過重的處理和資料計算,因為滾動是很頻繁,這個方法調用也是很頻繁的。

為了實現這個效果,我們只需要在實現InfiniteScrollListener,並將這個實作類別設定給ListView/GridView的setOnScrollListener()方法,就像是第二個程式碼片段的匿名類。

我們實現了InfiniteScrollListener類,我們也需要實現loadMore()方法,在這個方法裡,我們可以給ListView/GridView添加Item並通過notifyDataSetChanged()通知ListView/GridView資料發生改變。當然我們可以手動添加本機資料,也可以l從資料庫或者RESTful service來Load更多的資料。

這就是我們在Android中實現的在ListView/GridView的可以無限滾動的所作的。對於有大量資訊,ListView/GridView無疑有著很好的出處使用者體驗。

 

原文:http://www.avocarrot.com/blog/implement-infinitely-scrolling-list-android/

聯繫我們

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