Determine if recycleviw slides to the bottom
Tags: android
Published: 2020-09-12 12:30:51

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](https://dev-to-uploads.s3.amazonaws.com/i/osotn077smbz10s9pvsl.png)
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
                }
            }
        });
```