判斷RecyclerView是否滾動到底部


轉:http://www.jianshu.com/p/c138055af5d2

一、首先,我們來介紹和分析一下第一種方法,也是網上最多人用的方法:

public static boolean isVisBottom(RecyclerView recyclerView){ LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager(); //屏幕中最后一個可見子項的position int lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition(); //當前屏幕所看到的子項個數 int visibleItemCount = layoutManager.getChildCount(); //當前RecyclerView的所有子項個數 int totalItemCount = layoutManager.getItemCount(); //RecyclerView的滑動狀態 int state = recyclerView.getScrollState(); if(visibleItemCount > 0 && lastVisibleItemPosition == totalItemCount - 1 && state == recyclerView.SCROLL_STATE_IDLE){ return true; }else { return false; } }

很明顯,當屏幕中最后一個子項lastVisibleItemPosition等於所有子項個數totalItemCount - 1,那么RecyclerView就到達了底部。但是,我在這種方法中發現了極為極端的情況,就是當totalItemCount等於1,而這個子項的高度比屏幕還要高。


item_recycleview.png


看看效果圖:


效果圖.png


我們可以發現這個子項沒完全顯示出來就已經被判斷為拉到底部。當然,這種方法一般情況下都能滿足開發者的需求,只是遇到了強迫症的我~

二、下面我們介紹第二種方法:

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

這種方法原理其實很簡單,而且也是View自帶的方法。


原理圖.png


這樣就很清晰明了,computeVerticalScrollExtent()是當前屏幕顯示的區域高度,computeVerticalScrollOffset() 是當前屏幕之前滑過的距離,而computeVerticalScrollRange()是整個View控件的高度。
這種方法經過測試,暫時還沒發現有bug,而且它用的是View自帶的方法,所以個人覺得比較靠譜。

三、下面講講第三種方法:

RecyclerView.canScrollVertically(1)的值表示是否能向上滾動,false表示已經滾動到底部 RecyclerView.canScrollVertically(-1)的值表示是否能向下滾動,false表示已經滾動到頂部

這種方法更簡單,就通過簡單的調用方法,就可以得到你想要的結果。我一講過這種方法與第二種方法其實是同一種方法,那下面來分析一下,看看canScrollVertically的源碼:


canScrollVertically的源碼


是不是一目鳥然了,canScrollVertically方法的實現實際上運用到的是方法二的三個函數,只是這個方法Android已經幫我們封裝好了,原理一模一樣的。
本人現在也是運用了這種方法做判斷的~懶人~工具類都省了~

四、最后一種方法其實是比較呆板的,就是利用LinearLayoutManager的幾個方法,1.算出已經滑過的子項的距離,2.算出屏幕的高度,3.算出RecyclerView的總高度。然后用他們做比較,原理類似於方法二。

public static int getItemHeight(RecyclerView recyclerView) { int itemHeight = 0; View child = null; LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager(); int firstPos = layoutManager.findFirstCompletelyVisibleItemPosition(); int lastPos = layoutManager.findLastCompletelyVisibleItemPosition(); child = layoutManager.findViewByPosition(lastPos); if (child != null) { RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams(); itemHeight = child.getHeight() + params.topMargin + params.bottomMargin; } return itemHeight;}

算出一個子項的高度

public static int getLinearScrollY(RecyclerView recyclerView) {  
  int scrollY = 0; LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager(); int headerCildHeight = getHeaderHeight(recyclerView); int firstPos = layoutManager.findFirstVisibleItemPosition(); View child = layoutManager.findViewByPosition(firstPos); int itemHeight = getItemHeight(recyclerView); if (child != null) { int firstItemBottom = layoutManager.getDecoratedBottom(child); scrollY = headerCildHeight + itemHeight * firstPos - firstItemBottom; if(scrollY < 0){ scrollY = 0; } } return scrollY; }

算出滑過的子項的總距離

public static int getLinearTotalHeight(RecyclerView recyclerView) { int totalHeight = 0; LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager(); View child = layoutManager.findViewByPosition(layoutManager.findFirstVisibleItemPosition()); int headerCildHeight = getHeaderHeight(recyclerView); if (child != null) { int itemHeight = getItemHeight(recyclerView); int childCount = layoutManager.getItemCount(); totalHeight = headerCildHeight + (childCount - 1) * itemHeight; } return totalHeight; }

算出所有子項的總高度

public static boolean isLinearBottom(RecyclerView recyclerView) { boolean isBottom = true; int scrollY = getLinearScrollY(recyclerView); int totalHeight = getLinearTotalHeight(recyclerView); int height = recyclerView.getHeight(); // Log.e("height","scrollY " + scrollY + " totalHeight " + totalHeight + " recyclerHeight " + height); if (scrollY + height < totalHeight) { isBottom = false; } return isBottom; }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM