轉:http://www.jianshu.com/p/c138055af5d2ide
1、首先,咱們來介紹和分析一下第一種方法,也是網上最多人用的方法:函數
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,而這個子項的高度比屏幕還要高。工具
看看效果圖:測試
咱們能夠發現這個子項沒徹底顯示出來就已經被判斷爲拉到底部。固然,這種方法通常狀況下都能知足開發者的需求,只是遇到了強迫症的我~ui
2、下面咱們介紹第二種方法:spa
public static boolean isSlideToBottom(RecyclerView recyclerView) { if (recyclerView == null) return false; if (recyclerView.computeVerticalScrollExtent() + recyclerView.computeVerticalScrollOffset() >= recyclerView.computeVerticalScrollRange()) return true; return false; }
這種方法原理其實很簡單,並且也是View自帶的方法。code
這樣就很清晰明瞭,computeVerticalScrollExtent()是當前屏幕顯示的區域高度,computeVerticalScrollOffset() 是當前屏幕以前滑過的距離,而computeVerticalScrollRange()是整個View控件的高度。
這種方法通過測試,暫時還沒發現有bug,並且它用的是View自帶的方法,因此我的以爲比較靠譜。blog
3、下面講講第三種方法:ip
RecyclerView.canScrollVertically(1)的值表示是否能向上滾動,false表示已經滾動到底部 RecyclerView.canScrollVertically(-1)的值表示是否能向下滾動,false表示已經滾動到頂部
這種方法更簡單,就經過簡單的調用方法,就能夠獲得你想要的結果。我一講過這種方法與第二種方法實際上是同一種方法,那下面來分析一下,看看canScrollVertically的源碼:開發
是否是一目鳥然了,canScrollVertically方法的實現實際上運用到的是方法二的三個函數,只是這個方法Android已經幫咱們封裝好了,原理如出一轍的。
本人如今也是運用了這種方法作判斷的~懶人~工具類都省了~
4、最後一種方法實際上是比較呆板的,就是利用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; }