@Override
 public int getItemsVisible(RecyclerView recyclerView) {
   if (recyclerView == null) return 0;
   RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
   if (!(layoutManager instanceof LinearLayoutManager)) {
     throw new RuntimeException("LlScrollPercentCalculator needs a LinearLayoutManager.");
   }
   LinearLayoutManager linearLayoutManager = (LinearLayoutManager) layoutManager;
   return linearLayoutManager.findLastCompletelyVisibleItemPosition()
       - linearLayoutManager.findFirstCompletelyVisibleItemPosition();
 }
  @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 float getScrollPercentage(RecyclerView recyclerView) {
    if (recyclerView == null) return 0;
    RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
    if (!(layoutManager instanceof LinearLayoutManager)) {
      throw new RuntimeException("LlScrollPercentCalculator needs a LinearLayoutManager.");
    }
    LinearLayoutManager linearLayoutManager = (LinearLayoutManager) layoutManager;

    float lastFullyVisiblePosition = linearLayoutManager.findFirstVisibleItemPosition();
    float visibleItems =
        linearLayoutManager.findLastCompletelyVisibleItemPosition()
            - linearLayoutManager.findFirstCompletelyVisibleItemPosition();

    RecyclerView.Adapter adapter = recyclerView.getAdapter();
    if (adapter == null) return 0;
    float itemCount = adapter.getItemCount();
    return lastFullyVisiblePosition / (itemCount - visibleItems);
  }