How to implement endless list with RecyclerView?

前端 未结 30 3092
无人及你
无人及你 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条回答
  •  Happy的楠姐
    2020-11-22 03:07

    For those who only want to get notified when the last item is totally shown, you can use View.canScrollVertically().

    Here is my implementation:

    public abstract class OnVerticalScrollListener
            extends RecyclerView.OnScrollListener {
    
        @Override
        public final void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            if (!recyclerView.canScrollVertically(-1)) {
                onScrolledToTop();
            } else if (!recyclerView.canScrollVertically(1)) {
                onScrolledToBottom();
            } else if (dy < 0) {
                onScrolledUp();
            } else if (dy > 0) {
                onScrolledDown();
            }
        }
    
        public void onScrolledUp() {}
    
        public void onScrolledDown() {}
    
        public void onScrolledToTop() {}
    
        public void onScrolledToBottom() {}
    }
    

    Note: You can use recyclerView.getLayoutManager().canScrollVertically() if you want to support API < 14.

提交回复
热议问题