目前很多項目裏都使用Recyclerview來做開發了,但是對於Recyclerview不是很瞭解可能會遇到各種問題,下面就對怎樣判斷Recyclerview中的最後一個item做判斷!
- mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
- @Override
- public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
- //當前RecyclerView顯示出來的最後一個的item的position
- int lastPosition = -1;
- //當前狀態爲停止滑動狀態SCROLL_STATE_IDLE時
- if(newState == RecyclerView.SCROLL_STATE_IDLE){
- RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
- if(layoutManager instanceof GridLayoutManager){
- //通過LayoutManager找到當前顯示的最後的item的position
- lastPosition = ((GridLayoutManager) layoutManager).findLastVisibleItemPosition();
- }else if(layoutManager instanceof LinearLayoutManager){
- lastPosition = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition();
- }else if(layoutManager instanceof StaggeredGridLayoutManager){
- //因爲StaggeredGridLayoutManager的特殊性可能導致最後顯示的item存在多個,所以這裏取到的是一個數組
- //得到這個數組後再取到數組中position值最大的那個就是最後顯示的position值了
- int[] lastPositions = new int[((StaggeredGridLayoutManager) layoutManager).getSpanCount()];
- ((StaggeredGridLayoutManager) layoutManager).findLastVisibleItemPositions(lastPositions);
- lastPosition = findMax(lastPositions);
- }
- //時判斷界面顯示的最後item的position是否等於itemCount總數-1也就是最後一個item的position
- //如果相等則說明已經滑動到最後了
- if(lastPosition == recyclerView.getLayoutManager().getItemCount()-1){
- Toast.makeText(RecyclerActivity.this, "滑動到底了", Toast.LENGTH_SHORT).show();
- }
- }
- }
- @Override
- public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
- }
- });
findMax方法:
- //找到數組中的最大值
- private int findMax(int[] lastPositions) {
- int max = lastPositions[0];
- for (int value : lastPositions) {
- if (value > max) {
- max = value;
- }
- }
- return max;
- }
通過添加對RecyclerView的滑動事件,在滑動狀態改變的回調裏當滑動停止的時候通過layout找到當前RecyclerView顯示的最後一個item的position,然後判斷這個position是否爲Recyclerview的最後一個item的position值,如果是的話則表示已經滑動到最底部了。
上面的代碼基本上已經實現了對滑動到底部的監聽,只需要將上面的Toast彈出提示的代碼替換成我們自己的處理就可以了。但是上面的代碼也有一個bug,當最後一個item剛顯示出來的時候停止滑動這個時候也會觸發滑動到底部的操作,有時候我們可能並不希望這,希望當Recyclerview確實是滑動到底部滑動不了的時候才觸發。
修改上面的代碼如下:
- mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
- @Override
- public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
- }
- @Override
- public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
- //得到當前顯示的最後一個item的view
- View lastChildView = recyclerView.getLayoutManager().getChildAt(recyclerView.getLayoutManager().getChildCount()-1);
- //得到lastChildView的bottom座標值
- int lastChildBottom = lastChildView.getBottom();
- //得到Recyclerview的底部座標減去底部padding值,也就是顯示內容最底部的座標
- int recyclerBottom = recyclerView.getBottom()-recyclerView.getPaddingBottom();
- //通過這個lastChildView得到這個view當前的position值
- int lastPosition = recyclerView.getLayoutManager().getPosition(lastChildView);
- //判斷lastChildView的bottom值跟recyclerBottom
- //判斷lastPosition是不是最後一個position
- //如果兩個條件都滿足則說明是真正的滑動到了底部
- if(lastChildBottom == recyclerBottom && lastPosition == recyclerView.getLayoutManager().getItemCount()-1 ){
- Toast.makeText(RecyclerActivity.this, "滑動到底了", Toast.LENGTH_SHORT).show();
- }
- }
- });