DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Stone
Stone

Posted on

Determine if recycleviw slides to the bottom

How to judge whether recycleviw slides to the bottom,combine my experience and online examples,I summarize the following 4 methods:

method 1:

 mRecyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrollStateChanged(recyclerView: RecyclerView?, newState: Int) {
                super.onScrollStateChanged(recyclerView, newState)
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    //The number of items seen on the current screen
val childCount = mRecyclerView.childCount
                   //or
                   //val childCount = mRecyclerView.layoutManager.childCount
                   //number of all items of the RecyclerView
                    val itemCount = mRecyclerView.layoutManager.itemCount
                  //The position of the first visible item in the screen
                    val firstVisibleItem = (mRecyclerView.layoutManager as LinearLayoutManager).findFirstVisibleItemPosition()
                    if (firstVisibleItem + childCount == itemCount) {
                        //have slide to the bottom...
                    }
                }
            }
}

method 2:

in fact,this method is same as first method,see the codes directly

override fun onScrollStateChanged(recyclerView: RecyclerView?, newState: Int) {
                super.onScrollStateChanged(recyclerView, newState)
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    //The number of items seen on the current screen
                    val childCount = mRecyclerView.childCount
                    //or
                    val childCount = mRecyclerView.layoutManager.childCount
                    //number of all items of the RecyclerView
                    val itemCount = mRecyclerView.layoutManager.itemCount
                    //The position of the last visible item in the screen
                    val lastVisibleItem = (mRecyclerView.layoutManager as LinearLayoutManager).findLastVisibleItemPosition()
                    if (childCount > 0 && lastVisibleItem== itemCount - 1) {
                   //have slide to the bottom...
                    }
                }
            }

method 3:

public static boolean isSlideToBottom(RecyclerView recyclerView) {    
   if (recyclerView == null) return false; 
   if (recyclerView.computeVerticalScrollExtent() + recyclerView.computeVerticalScrollOffset() 
        >= recyclerView.computeVerticalScrollRange())   
     return true;  
   return false;
}

Alt Text
Computeverticalscrollextent() is the height of the area displayed on the current screen
Computeverticalscrolloffset() is the distance that the current screen has swiped before
Computeverticalscrollrange() is the height of the entire view

method 4:

RecyclerView.canScrollVertically (1) The value of indicates whether it can scroll up or not, false indicates that it has been scrolled to the bottom;
RecyclerView.canScrollVertically (-1) indicates whether it can scroll down or not, false indicates that it has scrolled to the top

mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if(mRecyclerView.canScrollVertically(1)){
                    Log.i(TAG, "direction 1: true");
                }else {
                    Log.i(TAG, "direction 1: false");//has been scrolled to the bottom
                }
                if(mRecyclerView.canScrollVertically(-1)){
                    Log.i(TAG, "direction -1: true");
                }else {
                    Log.i(TAG, "direction -1: false");//has scrolled to the top
                }
            }
        });

Top comments (0)

🌚 Browsing with dark mode makes you a better developer by a factor of exactly 40.

It's a scientific fact.