private void selectShade(int position) {
    ImageView shellSelected;

    for (int i = 0; i < shadeAdapter.getItemCount(); i++) {
      if (recyclerView.getChildAt(i) != null) {
        shellSelected = ((ImageView) recyclerView.getChildAt(i).findViewById(R.id.shell_selected));
        if (shellSelected.getVisibility() == View.VISIBLE) {
          shellSelected.setVisibility(View.GONE);
        }
      }
    }

    GridLayoutManager layoutManager = (GridLayoutManager) recyclerView.getLayoutManager();

    if (getSelectionMode() == SelectionModeEnum.SINGLE_SELECTION_MODE) {

      View view = recyclerView.getChildAt(position - layoutManager.findFirstVisibleItemPosition());

      if (shadeAdapter.getSelectedShade() == position) {
        shadeAdapter.setSelectedShade(-1);

        view.findViewById(R.id.shell_selected).setVisibility(View.INVISIBLE);

      } else {
        shadeAdapter.setSelectedShade(position);

        view.findViewById(R.id.shell_selected).setVisibility(View.VISIBLE);
      }

    } else if (getSelectionMode() == SelectionModeEnum.MULTIPLE_SELECTION_MODE) {

      if (selected.contains(position)) {
        selected.remove((Integer) position);

      } else {
        selected.add(position);
      }

      shadeAdapter.setSelected(selected);

      for (int item : shadeAdapter.getSelected()) {

        if (item >= layoutManager.findFirstVisibleItemPosition()
            && item <= layoutManager.findLastVisibleItemPosition()) {

          View view = recyclerView.getChildAt(item - layoutManager.findFirstVisibleItemPosition());
          view.findViewById(R.id.shell_selected).setVisibility(View.VISIBLE);
        }
      }
    }
  }
Пример #2
0
  @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();
    }
  }