Detect when RecyclerView reaches the bottom most position while scrolling

后端 未结 11 1842
没有蜡笔的小新
没有蜡笔的小新 2020-11-29 19:56

I have this code for a RecyclerView.

    recyclerView = (RecyclerView)rootview.findViewById(R.id.fabric_recyclerView);
    recyclerView.setLayoutManager(layo         


        
相关标签:
11条回答
  • 2020-11-29 20:43

    there is also a simple way to do it

    recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
    
            if (!recyclerView.canScrollVertically(1)) {
                Toast.makeText(YourActivity.this, "Last", Toast.LENGTH_LONG).show();
    
            }
        }
    });
    

    direction integers: -1 for up, 1 for down, 0 will always return false.

    0 讨论(0)
  • 2020-11-29 20:44

    I was not getting a perfect solution by the above answers because it was triggering twice even on onScrolled

    override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)
               if( !recyclerView.canScrollVertically(RecyclerView.FOCUS_DOWN))
                   context?.toast("Scroll end reached")
            }
    
    0 讨论(0)
  • 2020-11-29 20:45

    Just implement a addOnScrollListener() on your recyclerview. Then inside the scroll listener implement the code below.

    RecyclerView.OnScrollListener mScrollListener = new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                if (mIsLoading)
                    return;
                int visibleItemCount = mLayoutManager.getChildCount();
                int totalItemCount = mLayoutManager.getItemCount();
                int pastVisibleItems = mLayoutManager.findFirstVisibleItemPosition();
                if (pastVisibleItems + visibleItemCount >= totalItemCount) {
                    //End of list
                }
            }
        };
    
    0 讨论(0)
  • 2020-11-29 20:46

    This is my solution:

        val onScrollListener = object : RecyclerView.OnScrollListener() {
    
        override fun onScrolled(recyclerView: RecyclerView?, dx: Int, dy: Int) {
            directionDown = dy > 0
        }
    
        override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
            if (recyclerView.canScrollVertically(1).not()
                    && state != State.UPDATING
                    && newState == RecyclerView.SCROLL_STATE_IDLE
                    && directionDown) {
                   state = State.UPDATING
                // TODO do what you want  when you reach bottom, direction
                // is down and flag for non-duplicate execution
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-29 20:50

    After not being satisfied with most the other answers in this thread, I found something I think is better and is not anywhere on here.

    recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            if (!recyclerView.canScrollVertically(1) && dy > 0)
            {
                 //scrolled to bottom
            }else if (!recyclerView.canScrollVertically(-1) && dy < 0)
            {
                //scrolled to bottom
            }
        }
    });
    

    This is simple and will hit exactly one time under all conditions when you have scrolled to the top or bottom.

    0 讨论(0)
提交回复
热议问题