How to implement endless list with RecyclerView?

前端 未结 30 2657
无人及你
无人及你 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:06

    None of these answers take into account if the list is too small or not.

    Here's a piece of code I've been using that works on RecycleViews in both directions.

    @Override
        public boolean onTouchEvent(MotionEvent motionEvent) {
    
            if (recyclerViewListener == null) {
                return super.onTouchEvent(motionEvent);
            }
    
            /**
             * If the list is too small to scroll.
             */
            if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
                if (!canScrollVertically(1)) {
                    recyclerViewListener.reachedBottom();
                } else if (!canScrollVertically(-1)) {
                    recyclerViewListener.reachedTop();
                }
            }
    
            return super.onTouchEvent(motionEvent);
        }
    
        public void setListener(RecyclerViewListener recycleViewListener) {
            this.recyclerViewListener = recycleViewListener;
            addOnScrollListener(new OnScrollListener() {
    
                @Override
                public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                    super.onScrolled(recyclerView, dx, dy);
    
                    if (recyclerViewListener == null) {
                        return;
                    }
    
                    recyclerViewListener.scrolling(dy);
    
                    if (!canScrollVertically(1)) {
                        recyclerViewListener.reachedBottom();
                    } else if (!canScrollVertically(-1)) {
                        recyclerViewListener.reachedTop();
                    }
                }
    
            });
        }
    

提交回复
热议问题