Recyclerview中最後一個可見Item的位置以及是否滑動到底部

目前很多項目裏都使用Recyclerview來做開發了,但是對於Recyclerview不是很瞭解可能會遇到各種問題,下面就對怎樣判斷Recyclerview中的最後一個item做判斷!

  1. mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
  2. @Override
  3. public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
  4. //當前RecyclerView顯示出來的最後一個的item的position
  5. int lastPosition = -1;
  6.  
  7. //當前狀態爲停止滑動狀態SCROLL_STATE_IDLE時
  8. if(newState == RecyclerView.SCROLL_STATE_IDLE){
  9. RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
  10. if(layoutManager instanceof GridLayoutManager){
  11. //通過LayoutManager找到當前顯示的最後的item的position
  12. lastPosition = ((GridLayoutManager) layoutManager).findLastVisibleItemPosition();
  13. }else if(layoutManager instanceof LinearLayoutManager){
  14. lastPosition = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition();
  15. }else if(layoutManager instanceof StaggeredGridLayoutManager){
  16. //因爲StaggeredGridLayoutManager的特殊性可能導致最後顯示的item存在多個,所以這裏取到的是一個數組
  17. //得到這個數組後再取到數組中position值最大的那個就是最後顯示的position值了
  18. int[] lastPositions = new int[((StaggeredGridLayoutManager) layoutManager).getSpanCount()];
  19. ((StaggeredGridLayoutManager) layoutManager).findLastVisibleItemPositions(lastPositions);
  20. lastPosition = findMax(lastPositions);
  21. }
  22.  
  23. //時判斷界面顯示的最後item的position是否等於itemCount總數-1也就是最後一個item的position
  24. //如果相等則說明已經滑動到最後了
  25. if(lastPosition == recyclerView.getLayoutManager().getItemCount()-1){
  26. Toast.makeText(RecyclerActivity.this, "滑動到底了", Toast.LENGTH_SHORT).show();
  27. }
  28.  
  29. }
  30. }
  31.  
  32. @Override
  33. public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
  34.  
  35. }
  36.  
  37. });

findMax方法:

  1. //找到數組中的最大值
  2. private int findMax(int[] lastPositions) {
  3. int max = lastPositions[0];
  4. for (int value : lastPositions) {
  5. if (value > max) {
  6. max = value;
  7. }
  8. }
  9. return max;
  10. }

        通過添加對RecyclerView的滑動事件,在滑動狀態改變的回調裏當滑動停止的時候通過layout找到當前RecyclerView顯示的最後一個item的position,然後判斷這個position是否爲Recyclerview的最後一個item的position值,如果是的話則表示已經滑動到最底部了。
上面的代碼基本上已經實現了對滑動到底部的監聽,只需要將上面的Toast彈出提示的代碼替換成我們自己的處理就可以了。但是上面的代碼也有一個bug,當最後一個item剛顯示出來的時候停止滑動這個時候也會觸發滑動到底部的操作,有時候我們可能並不希望這,希望當Recyclerview確實是滑動到底部滑動不了的時候才觸發。
修改上面的代碼如下:

  1. mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
  2. @Override
  3. public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
  4.  
  5. }
  6.  
  7. @Override
  8. public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
  9.  
  10. //得到當前顯示的最後一個item的view
  11. View lastChildView = recyclerView.getLayoutManager().getChildAt(recyclerView.getLayoutManager().getChildCount()-1);
  12. //得到lastChildView的bottom座標值
  13. int lastChildBottom = lastChildView.getBottom();
  14. //得到Recyclerview的底部座標減去底部padding值,也就是顯示內容最底部的座標
  15. int recyclerBottom = recyclerView.getBottom()-recyclerView.getPaddingBottom();
  16. //通過這個lastChildView得到這個view當前的position值
  17. int lastPosition = recyclerView.getLayoutManager().getPosition(lastChildView);
  18.  
  19. //判斷lastChildView的bottom值跟recyclerBottom
  20. //判斷lastPosition是不是最後一個position
  21. //如果兩個條件都滿足則說明是真正的滑動到了底部
  22. if(lastChildBottom == recyclerBottom && lastPosition == recyclerView.getLayoutManager().getItemCount()-1 ){
  23. Toast.makeText(RecyclerActivity.this, "滑動到底了", Toast.LENGTH_SHORT).show();
  24. }
  25. }
  26.  
  27. });
當最後一個item的高度放不下的時候需要使用下面的方法來做判斷。
//獲取最後一個完全顯示的ItemPosition  
int lastVisibleItem = lineanrManager.findLastCompletelyVisibleItemPosition();  
int totalItemCount = lineanrManager.getItemCount();

發佈了32 篇原創文章 · 獲贊 45 · 訪問量 13萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章