RecyclerView中的无限滚动

时间:2016-12-04 10:24:47

标签: android android-recyclerview

我通过以下方式应用listview滚动:

mylistview.setOnScrollListener(new AbsListView.OnScrollListener() {
    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {

    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
      boolean loadMore = /* maybe add a padding */
        firstVisibleItem + visibleItemCount >= totalItemCount;

      if(loadMore) {
        int tempLimit = totalItemCount/50 + 1;
        FriendsAdapter temp = (FriendsAdapter) friendsList.getAdapter();
        if (temp!=null&&temp.ev.size()>0&&temp.ev.size()+50<=tempLimit*50) {
          limit = tempLimit;
          new GetFriendRequestsAsync(getActivity(),String.valueOf(limit)).execute();
        }

      }
    }
});

现在我需要更新它并使用RecycleView。如何将其应用于RecycleView?每50件事,我都会做点什么。

1 个答案:

答案 0 :(得分:2)

您可以使用以下内容:

mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
        }

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            int lastVisibleItemPosition = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition();
            boolean loadMore = /* maybe add a padding */
                    lastVisibleItemPosition + 6 >= totalItemCount;

            if (loadMore) {
                int tempLimit = totalItemCount / 50 + 1;
                FriendsAdapter temp = (FriendsAdapter) friendsList.getAdapter();
                if (temp != null && temp.ev.size() > 0 && temp.ev.size() + 50 <= tempLimit * 50) {
                    limit = tempLimit;

                    new GetFriendRequestsAsync(getActivity(), String.valueOf(limit)).execute();

                }
            }
        }
    });