2017-06-12 21 views
0

我正嘗試使用LoadManager將更多項加載到列表視圖中。但每次我的listview更新,我看不到舊的結果。我怎樣才能看到所有的結果,並加載到底部?如何使用LoaderManager將更多項加載到ListView中

private OnScrollListener onScrollListener() { 
return new OnScrollListener() { 

    @Override 
    public void onScrollStateChanged(AbsListView view, int scrollState) { 
     int threshold = 1; 
     int count = listView.getCount(); 

     if (scrollState == SCROLL_STATE_IDLE) { 
      // is it good condition to load new data dynamically? 
      if (listView.getLastVisiblePosition() >= count - threshold) { 

       // startIndex is a start point for the query 
       // default value is 0 
       startIndex += 10; 
       // this method format the URL using URI.Builder and change startIndex 
       formatUrl(false, keywords, startIndex); 

       // Clear the adapter 
       mAdapter.clear(); 

       // I think the problem is here?! But how to fix it?! 
       getLoaderManager().restartLoader(LOADER_ID, null, MainActivity.this); 
      } 
     } 
    } 

    @Override 
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, 
         int totalItemCount) { 
    } 
}; 

回答

0

一個好的方法來處理裝載更多的物品進入listview是:

  • 調用加載的項目到您的項目列表中loadMoreItems()功能,當用戶達到你listview的底部。使用notifyDataSetChanged()方法通知adapter項目List已經改變。

一個簡單的執行情況的項目加載功能:

public loadMoreItems() 
    { 
     // your network logic here 

     //append your items to your list 
     for(Item item : fetchedItems) 
     { 
     items.add(item); 
     } 

     //inform the adapter about the data change 
     adapter.notifyDataSetChanged(); 

    } 
相關問題