2012-03-30 63 views
2

我想要實現在Android的分頁列表視圖,所以當我向下滾動到最後,每次更多的項目應該添加到我的名單,目前我取出由Web服務10項,並顯示他們在一個列表視圖中,現在我想,當用戶向下滾動增加10多個項目的列表的末尾。 有沒有辦法做到這一點?如何實現分頁列表視圖中的Android

回答

6

編輯:這一個是更好:http://p-xr.com/android-tutorial-dynamicaly-load-more-items-to-the-listview-never-ending-list/

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - * - - - - - - - - - - - - - - - - - - - - - -

http://benjii.me/2010/08/endless-scrolling-listview-in-android/

public class EndlessScrollListener implements OnScrollListener { 

    private int visibleThreshold = 5; 
    private int currentPage = 0; 
    private int previousTotal = 0; 
    private boolean loading = true; 

    public EndlessScrollListener() { 
    } 
    public EndlessScrollListener(int visibleThreshold) { 
     this.visibleThreshold = visibleThreshold; 
    } 

    @Override 
    public void onScroll(AbsListView view, int firstVisibleItem, 
      int visibleItemCount, int totalItemCount) { 
     if (loading) { 
      if (totalItemCount > previousTotal) { 
       loading = false; 
       previousTotal = totalItemCount; 
       currentPage++; 
      } 
     } 
     if (!loading && (totalItemCount - visibleItemCount) <= (firstVisibleItem + visibleThreshold)) { 
      // I load the next page of gigs using a background task, 
      // but you can call any function here. 
      new LoadGigsTask().execute(currentPage + 1); 
      loading = true; 
     } 
    } 

    @Override 
    public void onScrollStateChanged(AbsListView view, int scrollState) { 
    } 
} 
+0

謝謝..這將解決我的問題:) – abhishek 2012-03-30 07:25:36

+0

代碼存在一些問題,它甚至在我不滾動我的列表時總是執行,可能是因爲列表加載時它可能會自動滾動,你知道嗎有關它的一些事情。 – abhishek 2012-03-30 11:51:11

+0

LoadGigsTask沒有很好的文檔記錄,但我認爲你將能夠處理;-)你需要添加你的數據在列表視圖適配器,然後使用notifidatasetchanged – 2012-03-30 12:22:05

7

我在這裏找到一個更好的解決方案,它實際上看起來像一個增強Waza_Be的解決方案:https://github.com/thecodepath/android_guides/wiki/Endless-Scrolling-with-AdapterViews(我不是作者,那麼學分到https://github.com/thecodepath)。

抽象偵聽器類是用負責加載數據的抽象方法實現的,這對於發送其特定請求的活動非常有用。

我只是張貼代碼在這裏,如果莫名其妙的代碼在wiki消失。

public abstract class EndlessScrollListener implements OnScrollListener { 
    // The minimum amount of items to have below your current scroll position 
    // before loading more. 
    private int visibleThreshold = 5; 
    // The current offset index of data you have loaded 
    private int currentPage = 0; 
    // The total number of items in the dataset after the last load 
    private int previousTotalItemCount = 0; 
    // True if we are still waiting for the last set of data to load. 
    private boolean loading = true; 
    // Sets the starting page index 
    private int startingPageIndex = 0; 

    public EndlessScrollListener() { 
    } 

    public EndlessScrollListener(int visibleThreshold) { 
     this.visibleThreshold = visibleThreshold; 
    } 

    public EndlessScrollListener(int visibleThreshold, int startPage) { 
     this.visibleThreshold = visibleThreshold; 
     this.startingPageIndex = startPage; 
     this.currentPage = startPage; 
    } 

    // This happens many times a second during a scroll, so be wary of the code you place here. 
    // We are given a few useful parameters to help us work out if we need to load some more data, 
    // but first we check if we are waiting for the previous load to finish. 
    @Override 
    public void onScroll(AbsListView view,int firstVisibleItem,int visibleItemCount,int totalItemCount) { 
     // If the total item count is zero and the previous isn't, assume the 
     // list is invalidated and should be reset back to initial state 
     if (totalItemCount < previousTotalItemCount) { 
      this.currentPage = this.startingPageIndex; 
      this.previousTotalItemCount = totalItemCount; 
      if (totalItemCount == 0) { this.loading = true; } 
     } 

     // If it’s still loading, we check to see if the dataset count has 
     // changed, if so we conclude it has finished loading and update the current page 
     // number and total item count. 
     if (loading && (totalItemCount > previousTotalItemCount)) { 
      loading = false; 
      previousTotalItemCount = totalItemCount; 
      currentPage++; 
     } 

     // If it isn’t currently loading, we check to see if we have breached 
     // the visibleThreshold and need to reload more data. 
     // If we do need to reload some more data, we execute onLoadMore to fetch the data. 
     if (!loading && (totalItemCount - visibleItemCount)<=(firstVisibleItem + visibleThreshold)) { 
      onLoadMore(currentPage + 1, totalItemCount); 
      loading = true; 
     } 
    } 

    // Defines the process for actually loading more data based on page 
    public abstract void onLoadMore(int page, int totalItemsCount); 

    @Override 
    public void onScrollStateChanged(AbsListView view, int scrollState) { 
     // Don't take any action on changed 
    } 
} 
+0

這真棒。工作得很好。 – Asim 2014-10-30 06:52:07

+0

plse使用適配器發送類文件它對我非常有用plse我需要實現分頁我的輸入具有頁碼和每頁pager與url一起如何加載服務器數據和填充gridview.and如何實現onscrolllistener如何可以我加載剩餘的數據並追加到現有的適配器,請幫助我 – Harsha 2015-05-20 05:39:40

0

我想發表烏鴉滾動監聽器是一個很好的路要走。 但在Android中處理數據加載的清潔方法是使用一個Loader(CursorLoader或AsyncTaskLoader,這取決於你從你的數據)。

令人驚訝的是,我找不到任何清理方式與加載器進行分頁,因此我繼續創建一個支持它的基礎加載器。這是非常早期階段,因此隨時提交引入請求,如果你發現錯誤:

https://github.com/nbarraille/paginated_loader

它幾乎增加了一個loadMore()方法對加載,你不得不實施loadInBackground()加載數據的單頁,和appendResults()告訴裝載機如何將結果結合在一起,就是這樣。

示例應用程序使用無限滾動偵聽器在列表結束後立即請求更多數據,將請求更多數據。