簡體   English   中英

如何使用LoaderManager將更多項目加載到ListView

[英]How to load more items to the ListView with a LoaderManager

我正在嘗試使用LoadManager將更多項目加載到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) {
    }
};

處理將更多項目加載到listview一種好方法是:

  • 每當用戶到達listview的底部時,調用loadMoreItems()函數將項目加載到您的項目列表中。

  • 使用notifyDataSetChanged()方法來通知adapter項目列表已更改。

項加載功能的簡單實現:

    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();

    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM