View findOneVisibleChild(
      int fromIndex, int toIndex, boolean completelyVisible, boolean acceptPartiallyVisible) {
    OrientationHelper helper;
    if (layoutManager.canScrollVertically()) {
      helper = OrientationHelper.createVerticalHelper(layoutManager);
    } else {
      helper = OrientationHelper.createHorizontalHelper(layoutManager);
    }

    final int start = helper.getStartAfterPadding();
    final int end = helper.getEndAfterPadding();
    final int next = toIndex > fromIndex ? 1 : -1;
    View partiallyVisible = null;
    for (int i = fromIndex; i != toIndex; i += next) {
      final View child = layoutManager.getChildAt(i);
      final int childStart = helper.getDecoratedStart(child);
      final int childEnd = helper.getDecoratedEnd(child);
      if (childStart < end && childEnd > start) {
        if (completelyVisible) {
          if (childStart >= start && childEnd <= end) {
            return child;
          } else if (acceptPartiallyVisible && partiallyVisible == null) {
            partiallyVisible = child;
          }
        } else {
          return child;
        }
      }
    }
    return partiallyVisible;
  }
Пример #2
0
  private int findPartlyVisiblePosition() {
    int positionView = 1;

    RecyclerView.LayoutManager layoutManager = videoList.getLayoutManager();
    int start = layoutManager.getPaddingStart();
    int end = layoutManager.getHeight() + start;
    int next = isScrolledUp ? 1 : -1;
    int fromIndex = isScrolledUp ? 0 : layoutManager.getChildCount() - 1;
    int toIndex = isScrolledUp ? layoutManager.getChildCount() : -1;

    for (int i = fromIndex; i != toIndex; i += next) {
      View child = layoutManager.getChildAt(i);
      View childTex = child.findViewById(R.id.iv_feed_center_video);
      int[] texLocation = new int[2];
      childTex.getLocationInWindow(texLocation);
      int childStart = texLocation[VIEW_TOP_LOCATION_INDEX] - actionbarSize;
      int childEnd = childStart + childTex.getHeight();
      if (childStart <= end && childEnd >= start) {
        positionView = layoutManager.getPosition(child);
        break;
      }
    }
    return positionView;
  }