How to implement endless list with RecyclerView?

前端 未结 30 3064
无人及你
无人及你 2020-11-22 02:22

I would like to change ListView to RecyclerView. I want to use the onScroll of the OnScrollListener in RecyclerView to determine if a

30条回答
  •  没有蜡笔的小新
    2020-11-22 03:04

    My answer is a modified version of Noor. I passed from a ListView where i had EndlessScrollListener (that you can find easily in many answers on SO) to a RecyclerView so i wanted a EndlessRecyclScrollListener to easily update my past listener.

    So here is the code, hope it helps:

    public abstract class EndlessScrollRecyclListener extends RecyclerView.OnScrollListener
    {
        // The total number of items in the dataset after the last load
        private int previousTotalItemCount = 0;
        private boolean loading = true;
        private int visibleThreshold = 5;
        int firstVisibleItem, visibleItemCount, totalItemCount;
        private int startingPageIndex = 0;
        private int currentPage = 0;
    
        @Override
        public void onScrolled(RecyclerView mRecyclerView, int dx, int dy)
        {
            super.onScrolled(mRecyclerView, dx, dy);
            LinearLayoutManager mLayoutManager = (LinearLayoutManager) mRecyclerView
                    .getLayoutManager();
    
            visibleItemCount = mRecyclerView.getChildCount();
            totalItemCount = mLayoutManager.getItemCount();
            firstVisibleItem = mLayoutManager.findFirstVisibleItemPosition();
            onScroll(firstVisibleItem, visibleItemCount, totalItemCount);
        }
    
        public void onScroll(int firstVisibleItem, int visibleItemCount, int totalItemCount)
        {
            // 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;
                currentPage++;
            }
    
            // 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.
            if (!loading && (totalItemCount - visibleItemCount) <= (firstVisibleItem +
                    visibleThreshold))
            {
                onLoadMore(currentPage + 1, totalItemCount);
                loading = true;
            }
        }
    
        // Defines the process for actually loading more data based on page
        public abstract void onLoadMore(int page, int totalItemsCount);
    
    }
    

提交回复
热议问题