简体   繁体   English

如何使用LoaderManager将更多项目加载到ListView

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

I am trying to load more items to the listview with LoadManager. 我正在尝试使用LoadManager将更多项目加载到listview中。 But each time my listview updated and I can't see the old results. 但是每次我的列表视图更新时,我都看不到旧结果。 How can I see all results and load new one to the bottom part? 如何查看所有结果并将新结果加载到底部?

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) {
    }
};

A good approach to deal with loading more items into listview is: 处理将更多项目加载到listview一种好方法是:

  • calling a loadMoreItems() function that loads the items into your items List, whenever the user reached the bottom of your listview . 每当用户到达listview的底部时,调用loadMoreItems()函数将项目加载到您的项目列表中。

  • using notifyDataSetChanged() method to notify the adapter that the items List has changed. 使用notifyDataSetChanged()方法来通知adapter项目列表已更改。

A simple implementation of the item-loading function: 项加载功能的简单实现:

    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