public boolean shouldHandleRecyclerViewLoadingMore(RecyclerView recyclerView) {
    if (mIsLoadingMore
        || mCurrentRefreshStatus == RefreshStatus.REFRESHING
        || mLoadMoreFooterView == null
        || mDelegate == null
        || recyclerView.getAdapter() == null
        || recyclerView.getAdapter().getItemCount() == 0) {
      return false;
    }

    RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
    if (manager == null || manager.getItemCount() == 0) {
      return false;
    }

    if (manager instanceof LinearLayoutManager) {
      LinearLayoutManager layoutManager = (LinearLayoutManager) manager;
      if (layoutManager.findLastCompletelyVisibleItemPosition()
          == recyclerView.getAdapter().getItemCount() - 1) {
        return true;
      }
    } else if (manager instanceof StaggeredGridLayoutManager) {
      StaggeredGridLayoutManager layoutManager = (StaggeredGridLayoutManager) manager;

      int[] out = layoutManager.findLastCompletelyVisibleItemPositions(null);
      int lastPosition = layoutManager.getItemCount() - 1;
      for (int position : out) {
        if (position == lastPosition) {
          return true;
        }
      }
    }
    return false;
  }
Beispiel #2
0
 @Override
 public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
   currentScrollState = newState;
   RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
   int visibleItemCount = layoutManager.getChildCount();
   int totalItemCount = layoutManager.getItemCount();
   if ((visibleItemCount > 0
       && currentScrollState == RecyclerView.SCROLL_STATE_IDLE
       && (lastVisibleItemPosition) >= totalItemCount - 1)) {
     onBottom(); // Scroll to bottom
   }
 }
  @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();
    }
  }
  private void maybeFireLoadMore() {
    if (hasLoadMoreFired) {
      return;
    }
    if (!showShowLoadMore) {
      return;
    }

    final RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
    int visibleItemCount = layoutManager.getChildCount();
    int totalItemCount = layoutManager.getItemCount();
    int firstVisibleItemPosition = findFirstVisibleItemPosition();

    if (totalItemCount == 0) {
      return;
    }

    if (firstVisibleItemPosition + visibleItemCount + bufferItems > totalItemCount) {
      if (onLoadMoreListener != null) {
        hasLoadMoreFired = true;
        onLoadMoreListener.onLoadMore(adapter.getLastItem());
      }
    }
  }
 /**
  * Returns the adapter item count.
  *
  * @return The total number on items in a layout manager
  */
 public int getItemCount() {
   return layoutManager == null ? 0 : layoutManager.getItemCount();
 }