Example #1
0
 @Override
 public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
   RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
   StaggeredGridLayoutManager staggeredGridLayoutManager =
       (StaggeredGridLayoutManager) layoutManager;
   if (lastPositions == null) {
     lastPositions = new int[staggeredGridLayoutManager.getSpanCount()];
   }
   staggeredGridLayoutManager.findLastVisibleItemPositions(lastPositions);
   lastVisibleItemPosition = findMax(lastPositions);
 }
 /** 获取最后一条展示的位置 */
 private int getLastVisiblePosition() {
   int position;
   if (getLayoutManager() instanceof LinearLayoutManager) {
     position = ((LinearLayoutManager) getLayoutManager()).findLastVisibleItemPosition();
   } else if (getLayoutManager() instanceof GridLayoutManager) {
     position = ((GridLayoutManager) getLayoutManager()).findLastVisibleItemPosition();
   } else if (getLayoutManager() instanceof StaggeredGridLayoutManager) {
     StaggeredGridLayoutManager layoutManager = (StaggeredGridLayoutManager) getLayoutManager();
     int[] lastPositions =
         layoutManager.findLastVisibleItemPositions(new int[layoutManager.getSpanCount()]);
     position = getMaxPosition(lastPositions);
   } else {
     position = getLayoutManager().getItemCount() - 1;
   }
   return position;
 }
  @Override
  public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
    super.onScrolled(recyclerView, dx, dy);
    int lastVisibleItem = 0;
    int firstVisibleItem = 0;
    RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
    int totalItemCount = layoutManager.getItemCount();
    if (layoutManager instanceof LinearLayoutManager) {
      LinearLayoutManager linearLayoutManager = ((LinearLayoutManager) layoutManager);
      lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition();
      firstVisibleItem = linearLayoutManager.findFirstCompletelyVisibleItemPosition();
    } else if (layoutManager instanceof GridLayoutManager) {
      GridLayoutManager gridLayoutManager = ((GridLayoutManager) layoutManager);
      // 通过LayoutManager找到当前显示的最后的item的position
      lastVisibleItem = gridLayoutManager.findLastVisibleItemPosition();
      firstVisibleItem = gridLayoutManager.findFirstCompletelyVisibleItemPosition();
    } else if (layoutManager instanceof StaggeredGridLayoutManager) {
      StaggeredGridLayoutManager staggeredGridLayoutManager =
          ((StaggeredGridLayoutManager) layoutManager);
      // 因为StaggeredGridLayoutManager的特殊性可能导致最后显示的item存在多个,所以这里取到的是一个数组
      // 得到这个数组后再取到数组中position值最大的那个就是最后显示的position值了
      int[] lastPositions = new int[((StaggeredGridLayoutManager) layoutManager).getSpanCount()];
      staggeredGridLayoutManager.findLastVisibleItemPositions(lastPositions);
      lastVisibleItem = findMax(lastPositions);
      firstVisibleItem = staggeredGridLayoutManager.findFirstVisibleItemPositions(lastPositions)[0];
    }
    if (firstVisibleItem == 0) {
      if (!mPullLoadMoreRecyclerView.isLoadMore()) {
        mPullLoadMoreRecyclerView.setPullRefreshEnable(true);
      }
    } else {
      mPullLoadMoreRecyclerView.setPullRefreshEnable(false);
    }

    /** 无论水平还是垂直 */
    if (!mPullLoadMoreRecyclerView.isRefresh()
        && mPullLoadMoreRecyclerView.isHasMore()
        && (lastVisibleItem >= totalItemCount - 1)
        && !mPullLoadMoreRecyclerView.isLoadMore()
        && (dx > 0 || dy > 0)) {
      mPullLoadMoreRecyclerView.setIsLoadMore(true);
      mPullLoadMoreRecyclerView.loadMore();
    }
  }
  @Override
  public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
    super.onScrolled(recyclerView, dx, dy);

    RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
    //  int lastVisibleItemPosition = -1;
    if (layoutManagerType == null) {
      if (layoutManager instanceof LinearLayoutManager) {
        layoutManagerType = LAYOUT_MANAGER_TYPE.LINEAR;
      } else if (layoutManager instanceof GridLayoutManager) {
        layoutManagerType = LAYOUT_MANAGER_TYPE.GRID;
      } else if (layoutManager instanceof StaggeredGridLayoutManager) {
        layoutManagerType = LAYOUT_MANAGER_TYPE.STAGGERED_GRID;
      } else {
        throw new RuntimeException(
            "Unsupported LayoutManager used. Valid ones are LinearLayoutManager, GridLayoutManager and StaggeredGridLayoutManager");
      }
    }

    switch (layoutManagerType) {
      case LINEAR:
        lastVisibleItemPosition =
            ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition();
        break;
      case GRID:
        lastVisibleItemPosition = ((GridLayoutManager) layoutManager).findLastVisibleItemPosition();
        break;
      case STAGGERED_GRID:
        StaggeredGridLayoutManager staggeredGridLayoutManager =
            (StaggeredGridLayoutManager) layoutManager;
        if (lastPositions == null) {
          lastPositions = new int[staggeredGridLayoutManager.getSpanCount()];
        }
        staggeredGridLayoutManager.findLastVisibleItemPositions(lastPositions);
        lastVisibleItemPosition = findMax(lastPositions);
        break;
    }
  }
  /**
   * 检查是否需要执行Loadmore
   *
   * @param recycler RecyclerView
   * @param listener 加载更多监听
   */
  private static void checkLoadmore(RecyclerView recycler, OnLoadmoreListener listener) {
    RecyclerView.LayoutManager layoutManager = recycler.getLayoutManager();
    if (layoutManager instanceof LinearLayoutManager) {
      int lastVisibleItem = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition();

      int totalItemCount = recycler.getAdapter().getItemCount();
      if (lastVisibleItem >= totalItemCount - 1) {
        listener.onLoadmore();
      }
    } else if (layoutManager instanceof StaggeredGridLayoutManager) {
      StaggeredGridLayoutManager gridLayoutManager = (StaggeredGridLayoutManager) layoutManager;
      int last[] = new int[gridLayoutManager.getSpanCount()];
      gridLayoutManager.findLastVisibleItemPositions(last);

      int totalItemCount = recycler.getAdapter().getItemCount();
      for (int i = 0; i < last.length; i++) {
        int lastVisibleItem = last[i];
        if (lastVisibleItem >= totalItemCount - 1) {
          listener.onLoadmore();
          break;
        }
      }
    }
  }
  private int calculateLastVisiblePosition(LayoutManager layoutManager) {

    if (layoutManager instanceof LinearLayoutManager) {
      LinearLayoutManager linearLayoutManager = (LinearLayoutManager) layoutManager;
      return linearLayoutManager.findLastVisibleItemPosition();
    }

    if (layoutManager instanceof StaggeredGridLayoutManager) {
      StaggeredGridLayoutManager staggeredGridLayoutManager =
          (StaggeredGridLayoutManager) layoutManager;

      int[] lastVisibleItemPositions =
          staggeredGridLayoutManager.findLastVisibleItemPositions(null);
      int max = -1;
      for (int i = 0; i < lastVisibleItemPositions.length; i++) {
        max = Math.max(max, lastVisibleItemPositions[i]);
        Log.e(TAG, "lastVisibleItemPositions" + i + lastVisibleItemPositions[i]);
      }

      return max;
    }

    return -1;
  }