【问题标题】:Implementing Endless RecyclerView实现 Endless RecyclerView
【发布时间】:2018-02-06 16:23:38
【问题描述】:

我想实现一个无限的 RecyclerView,但到目前为止,我遇到的所有实现和教程都描述了从服务器加载数据然后加载它或使用循环动态添加元素。

在我的例子中,我已经有一个大的 ArrayList,其中包含我想在 RecyclerView 中显示的数据。

我需要说的是,显示数组的前 10 项,加载更多时,显示下一批 10。

【问题讨论】:

  • 取决于您使用的数据库系统。
  • @EpicPandaForce 我没有使用数据库。它只是一个普通的 ArrayList。
  • 然后只需将新元素附加到列表中,然后调用notifyDatasetChanged()(或notifyItemRangeInserted(int positionStart, int itemCount),如果您也想要精美的动画)。但如果你想要每个页面只有 10 个项目的实际页面,那就有点不同了。

标签: android android-recyclerview endlessscroll


【解决方案1】:

这可以实现您的目标。

public abstract class EndlessRecyclerOnScrollListener extends RecyclerView.OnScrollListener {
    public static String TAG = EndlessRecyclerOnScrollListener.class.getSimpleName();

private int previousTotal = 0; // The total number of items in the dataset after the last load
private boolean loading = true; // True if we are still waiting for the last set of data to load.
private int visibleThreshold = 5; // The minimum amount of items to have below your current scroll position before loading more.
int firstVisibleItem, visibleItemCount, totalItemCount;

private int current_page = 1;

private LinearLayoutManager mLinearLayoutManager;

public EndlessRecyclerOnScrollListener(LinearLayoutManager linearLayoutManager) {
    this.mLinearLayoutManager = linearLayoutManager;
}

@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
    super.onScrolled(recyclerView, dx, dy);

    visibleItemCount = recyclerView.getChildCount();
    totalItemCount = mLinearLayoutManager.getItemCount();
    firstVisibleItem = mLinearLayoutManager.findFirstVisibleItemPosition();

    if (loading) {
        if (totalItemCount > previousTotal) {
            loading = false;
            previousTotal = totalItemCount;
        }
    }
    if (!loading && (totalItemCount - visibleItemCount)
            <= (firstVisibleItem + visibleThreshold)) {
        // End has been reached

        // Do something
        current_page++;

        onLoadMore(current_page);

        loading = true;
    }
}

public abstract void onLoadMore(int current_page);
}

以及示例活动

public class SampleActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sample);

    RecyclerView recyclerView = (RecyclerView) findViewById(R.id.list);
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(linearLayoutManager);
    recyclerView.setOnScrollListener(new EndlessRecyclerOnScrollListener(linearLayoutManager) {
        @Override
        public void onLoadMore(int current_page) {
            // do something...
        }
    });
}
}

编辑:See here: Endless Scrolling with AdapterViews

【讨论】:

  • 我实际上有这段代码,但问题是在 onLoadMore 方法中要做什么。我试过在主列表上做subList(),但数据仍然没有显示。
  • @AlexKombo 你基本上是想告诉适配器 items 和 items.size() 已经改变了。
  • @AlexKombo here is an example
【解决方案2】:

你可以用这个Endless Adapter

  • 它是通用的,你给它(你模型,Recycler.ViewHolder)
  • 您可以设置适配器是否显示加载。
  • 让侦听器检查加载完成时间和加载开始时间。

您还可以查看示例以获取更多信息

【讨论】:

    【解决方案3】:

    尝试使用:

    recyclerView.addOnScrollListener (recyclerViewOnScrollListener);
    
    private RecyclerView.OnScrollListener recyclerViewOnScrollListener = new RecyclerView.OnScrollListener () {
    
       private final String LOG_TAG = "PAGINATION_";
       boolean              isIdle;
    
       @Override
       public void onScrollStateChanged (RecyclerView recyclerView, int newState) {
    
           try {
               super.onScrollStateChanged (recyclerView, newState);
               isIdle = (newState == RecyclerView.SCROLL_STATE_IDLE);
           }
           catch (Exception e) {
               e.printStackTrace ();
           }
       }
    
       @Override
       public void onScrolled (RecyclerView recyclerView, int dx, int dy) {
    
           firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition ();
    
                       super.onScrolled (recyclerView, dx, dy);
    
                       if (!isIdle dy > 0) {
    
                           int totalItems = yourList.size()();
    
                           totalItemCount = layoutManager.getItemCount ();
                           visibleItemCount = layoutManager.getChildCount ();
    
                           Log.i (LOG_TAG, "Max: " + totalItems + ", Total: " + totalItemCount + ", Previous Total: " + previousTotal + ", Visible: " + visibleItemCount + ", First: " + firstVisibleItemPosition);
    
                           if (isLoading) {
                               if (totalItemCount != previousTotal) {
                                   isLoading = false;
                                   previousTotal = totalItemCount;
                               }
                           }
    
                           isLastPage = (totalItemCount >= totalItems);
    
                           Log.i (LOG_TAG, "DETAILS: currentPage: " + currentPage + ", isLoading: " + isLoading + ", isLast: " + isLastPage);
                           if (!isLoading && !isLastPage) {
                               if ((visibleItemCount + firstVisibleItemPosition) >= totalItemCount && firstVisibleItemPosition >= 0 && totalItems >= (currentPage * PAGE_LIMIT) && totalItems > totalItemCount) {
                                   if (totalItems > (currentPage * PAGE_LIMIT)) {
                                       currentPage = (totalItemCount / PAGE_LIMIT) + 1;
                                       isLoading = true;
                                       //load more data using currentPage and PAGE_LIMIT
                                   }
                               }
                           }
                       }
                       previousTotal = totalItemCount;
                   }
                   catch (Exception e) {
                       e.printStackTrace ();
                   }
               }
    
           }
       }
    
    };
    

    【讨论】:

      【解决方案4】:

      尝试使用此类对我有很大帮助:

      public abstract class EndlessRecyclerViewScrollListener extends RecyclerView.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;
      
          RecyclerView.LayoutManager mLayoutManager;
      
          public EndlessRecyclerViewScrollListener(LinearLayoutManager layoutManager) {
              this.mLayoutManager = layoutManager;
          }
      
          public EndlessRecyclerViewScrollListener(GridLayoutManager layoutManager) {
              this.mLayoutManager = layoutManager;
              visibleThreshold = visibleThreshold * layoutManager.getSpanCount();
          }
      
          public EndlessRecyclerViewScrollListener(StaggeredGridLayoutManager layoutManager) {
              this.mLayoutManager = layoutManager;
              visibleThreshold = visibleThreshold * layoutManager.getSpanCount();
          }
      
          public int getLastVisibleItem(int[] lastVisibleItemPositions) {
              int maxSize = 0;
              for (int i = 0; i < lastVisibleItemPositions.length; i++) {
                  if (i == 0) {
                      maxSize = lastVisibleItemPositions[i];
                  }
                  else if (lastVisibleItemPositions[i] > maxSize) {
                      maxSize = lastVisibleItemPositions[i];
                  }
              }
              return maxSize;
          }
      
          // 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 onScrolled(RecyclerView view, int dx, int dy) {
              int lastVisibleItemPosition = 0;
              int totalItemCount = mLayoutManager.getItemCount();
      
              if (mLayoutManager instanceof StaggeredGridLayoutManager) {
                  int[] lastVisibleItemPositions = ((StaggeredGridLayoutManager) mLayoutManager).findLastVisibleItemPositions(null);
                  // get maximum element within the list
                  lastVisibleItemPosition = getLastVisibleItem(lastVisibleItemPositions);
              } else if (mLayoutManager instanceof GridLayoutManager) {
                  lastVisibleItemPosition = ((GridLayoutManager) mLayoutManager).findLastVisibleItemPosition();
              } else if (mLayoutManager instanceof LinearLayoutManager) {
                  lastVisibleItemPosition = ((LinearLayoutManager) mLayoutManager).findLastVisibleItemPosition();
              } 
      
              // 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;
              }
      
              // 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.
              // threshold should reflect how many total columns there are too
              if (!loading && (lastVisibleItemPosition + visibleThreshold) > totalItemCount) {
                  currentPage++;
                  onLoadMore(currentPage, totalItemCount, view);
                  loading = true;
              }
          }
      
          // Call this method whenever performing new searches
          public void resetState() {
              this.currentPage = this.startingPageIndex;
              this.previousTotalItemCount = 0;
              this.loading = true;
          }
      
          // Defines the process for actually loading more data based on page
          public abstract void onLoadMore(int page, int totalItemsCount, RecyclerView view);
      
      }
      

      参考:

      【讨论】:

        猜你喜欢
        • 2017-11-20
        • 1970-01-01
        • 2016-03-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多