Java Code Examples for androidx.recyclerview.widget.OrientationHelper#getStartAfterPadding()

The following examples show how to use androidx.recyclerview.widget.OrientationHelper#getStartAfterPadding() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CarouselSnapHelper.java    From CarouselView with MIT License 6 votes vote down vote up
private View findFirstView(RecyclerView.LayoutManager layoutManager, OrientationHelper helper) {
  if (layoutManager == null) return null;

  int childCount = layoutManager.getChildCount();
  if (childCount == 0) return null;

  int absClosest = Integer.MAX_VALUE;
  View closestView = null;
  int start = helper.getStartAfterPadding();

  for (int i=0; i < childCount; i++) {
    View child = layoutManager.getChildAt(i);
    int childStart = helper.getDecoratedStart(child);
    int absDistanceToStart = Math.abs(childStart - start);
    if (absDistanceToStart < absClosest) {
      absClosest = absDistanceToStart;
      closestView = child;
    }
  }

  return closestView;
}
 
Example 2
Source File: GalleryLayoutManager.java    From CardSlideView with Apache License 2.0 6 votes vote down vote up
/**
 * 垂直填充布局测量
 *
 * @param recycler RecyclerView.Recycler
 * @param offset   垂直偏移量
 */
private void fillWithVertical(RecyclerView.Recycler recycler, int offset) {
    final OrientationHelper orientationHelper = getOrientationHelper();
    final int topEdge = orientationHelper.getStartAfterPadding();
    final int bottomEdge = orientationHelper.getEndAfterPadding();
    if (getChildCount() > 0) {
        if (offset >= 0) {
            // 下滑
            removeAndRecyclerWithTop(recycler, topEdge + offset);
        } else {
            // 上滑
            removeAndRecyclerWithBottom(recycler, bottomEdge + offset);
        }

    }
    if (offset >= 0) {
        fillBottom(recycler, bottomEdge + offset);
    } else {
        fillTop(recycler, topEdge + offset);
    }
}
 
Example 3
Source File: GalleryLayoutManager.java    From CardSlideView with Apache License 2.0 6 votes vote down vote up
/**
 * 水平填充布局测量
 *
 * @param recycler RecyclerView.Recycler
 * @param offset   水平偏移量
 */
private void fillWithHorizontal(RecyclerView.Recycler recycler, int offset) {
    final OrientationHelper orientationHelper = getOrientationHelper();
    final int leftEdge = orientationHelper.getStartAfterPadding();
    final int rightEdge = orientationHelper.getEndAfterPadding();
    if (getChildCount() > 0) {
        if (offset >= 0) {
            removeAndRecyclerWithLeft(recycler, leftEdge + offset);
        } else {
            removeAndRecyclerWithRight(recycler, rightEdge + offset);
        }

    }
    if (offset >= 0) {
        // 右滑
        fillRight(recycler, rightEdge + offset);
    } else {
        // 左滑
        fillLeft(recycler, leftEdge + offset);
    }
}
 
Example 4
Source File: StartSnapHelper.java    From litho with Apache License 2.0 6 votes vote down vote up
/** @return the first View whose start is before the start of this recycler view */
@Nullable
private static View findFirstViewBeforeStart(
    LayoutManager layoutManager, OrientationHelper helper) {
  int childCount = layoutManager.getChildCount();
  if (childCount == 0) {
    return null;
  }

  View closestChild = null;
  final int start = helper.getStartAfterPadding();
  int absClosest = Integer.MAX_VALUE;

  for (int i = 0; i < childCount; i++) {
    final View child = layoutManager.getChildAt(i);
    int childStart = helper.getDecoratedStart(child);
    int absDistance = Math.abs(childStart - start);

    if (childStart < start && absDistance < absClosest) {
      absClosest = absDistance;
      closestChild = child;
    }
  }

  return closestChild;
}
 
Example 5
Source File: StartSnapHelper.java    From litho with Apache License 2.0 5 votes vote down vote up
private int distanceToStart(
    @NonNull RecyclerView.LayoutManager layoutManager,
    @NonNull View targetView,
    OrientationHelper helper) {
  final int childStart = helper.getDecoratedStart(targetView);
  final int containerStart = helper.getStartAfterPadding();
  return childStart - containerStart;
}
 
Example 6
Source File: StartSnapHelper.java    From litho with Apache License 2.0 5 votes vote down vote up
/**
 * Return the child view that is currently closest to the start of this parent.
 *
 * @param layoutManager The {@link LayoutManager} associated with the attached {@link
 *     RecyclerView}.
 * @param helper The relevant {@link OrientationHelper} for the attached {@link RecyclerView}.
 * @return the child view that is currently closest to the start of this parent.
 */
@Nullable
private static View findViewClosestToStart(
    LayoutManager layoutManager, OrientationHelper helper) {
  int childCount = layoutManager.getChildCount();
  if (childCount == 0) {
    return null;
  }

  View closestChild = null;
  final int start = helper.getStartAfterPadding();
  int absClosest = Integer.MAX_VALUE;

  for (int i = 0; i < childCount; i++) {
    final View child = layoutManager.getChildAt(i);
    int childStart = helper.getDecoratedStart(child);
    int absDistance = Math.abs(childStart - start);

    /** if child start is closer than previous closest, set it as closest * */
    if (absDistance < absClosest) {
      absClosest = absDistance;
      closestChild = child;
    }
  }

  return closestChild;
}
 
Example 7
Source File: GravityDelegate.java    From GetApk with MIT License 5 votes vote down vote up
private int distanceToStart(View targetView, OrientationHelper helper, boolean fromEnd) {
    if (isRtlHorizontal && !fromEnd) {
        return distanceToEnd(targetView, helper, true);
    }

    return helper.getDecoratedStart(targetView) - helper.getStartAfterPadding();
}
 
Example 8
Source File: GalleryLayoutManager.java    From CardSlideView with Apache License 2.0 5 votes vote down vote up
/**
 * @param child  计算的view
 * @param offset view的滑动偏移量
 * @return 返回view距离中心轴的距离
 */
private int calculateDistanceToCenter(View child, float offset) {
    final OrientationHelper orientationHelper = getOrientationHelper();
    final int centerToStart = (orientationHelper.getEndAfterPadding() - orientationHelper.getStartAfterPadding()) / 2 + orientationHelper.getStartAfterPadding();
    if (mOrientation == LinearLayout.HORIZONTAL) {
        return (int) (child.getWidth() / 2 - offset + child.getLeft() - centerToStart);
    } else {
        return (int) (child.getHeight() / 2 - offset + child.getTop() - centerToStart);
    }
}
 
Example 9
Source File: GalleryLayoutManager.java    From CardSlideView with Apache License 2.0 5 votes vote down vote up
@Override
public int scrollHorizontallyBy(int dx, RecyclerView.Recycler recycler, RecyclerView.State state) {
    if (mOrientation == LinearLayout.VERTICAL) {
        return 0;
    }
    if (getChildCount() == 0 || dx == 0) {
        return 0;
    }
    int offset = -dx;
    final OrientationHelper orientationHelper = getOrientationHelper();
    final int centerToStart = (orientationHelper.getEndAfterPadding() - orientationHelper.getStartAfterPadding()) / 2 + orientationHelper.getStartAfterPadding();
    View child;
    if (dx > 0) {
        child = getChildAt(getChildCount() - 1);
        if (child != null && getPosition(child) == getItemCount() - 1 && !isLooper) {
            // 计算全部加载完后item的偏移量,右边会留出空隙
            offset = -Math.max(0, Math.min(dx, (child.getRight() - child.getLeft()) / 2 + child.getLeft() - centerToStart));
        }
    } else {
        child = getChildAt(0);
        if (mFirstVisiblePosition == 0 && child != null && !isLooper) {
            // 计算首次加载item的偏移量,左边会留出空隙
            offset = -Math.min(0, Math.max(dx, ((child.getRight() - child.getLeft()) / 2 + child.getLeft()) - centerToStart));
        }
    }
    // 记录偏移量
    getState().scrollOffset = -offset;
    fill(recycler, -offset);
    offsetChildrenHorizontal(offset);
    return -offset;
}
 
Example 10
Source File: RecyclerViewPositionHelper.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
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;
}
 
Example 11
Source File: GravitySnapHelper.java    From GravitySnapHelper with Apache License 2.0 5 votes vote down vote up
private int getDistanceToStart(View targetView, @NonNull OrientationHelper helper) {
    int distance;
    // If we don't care about padding, just snap to the start of the view
    if (!snapToPadding) {
        int childStart = helper.getDecoratedStart(targetView);
        if (childStart >= helper.getStartAfterPadding() / 2) {
            distance = childStart - helper.getStartAfterPadding();
        } else {
            distance = childStart;
        }
    } else {
        distance = helper.getDecoratedStart(targetView) - helper.getStartAfterPadding();
    }
    return distance;
}
 
Example 12
Source File: RecyclerViewPositionHelper.java    From aptoide-client-v8 with GNU General Public License v3.0 5 votes vote down vote up
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;
}
 
Example 13
Source File: RecyclerViewPositionHelper.java    From UltimateRecyclerView with Apache License 2.0 5 votes vote down vote up
private 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;
}
 
Example 14
Source File: RecyclerViewPositionHelper.java    From Applozic-Android-SDK with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
View findOneVisibleChild(int fromIndex, int toIndex, boolean completelyVisible,
                         boolean acceptPartiallyVisible) {
    OrientationHelper helper;
    if (linearLayoutManager.canScrollVertically()) {
        helper = OrientationHelper.createVerticalHelper(linearLayoutManager);
    } else {
        helper = OrientationHelper.createHorizontalHelper(linearLayoutManager);
    }

    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 = linearLayoutManager.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;
}
 
Example 15
Source File: GravitySnapHelper.java    From MaterialDateTimePicker with Apache License 2.0 5 votes vote down vote up
private int distanceToStart(View targetView, OrientationHelper helper, boolean fromEnd) {
    if (isRtlHorizontal && !fromEnd) {
        return distanceToEnd(targetView, helper, true);
    }

    return helper.getDecoratedStart(targetView) - helper.getStartAfterPadding();
}
 
Example 16
Source File: CarouselSnapHelper.java    From CarouselView with MIT License 4 votes vote down vote up
private int distanceStart(View targetView, OrientationHelper helper) {
  int childStart = helper.getDecoratedStart(targetView);
  int containerStart = helper.getStartAfterPadding();
  return childStart - containerStart;
}
 
Example 17
Source File: GravitySnapHelper.java    From GravitySnapHelper with Apache License 2.0 4 votes vote down vote up
/**
 * Returns the first view that we should snap to.
 *
 * @param layoutManager the RecyclerView's LayoutManager
 * @param helper        orientation helper to calculate view sizes
 * @param gravity       gravity to find the closest view
 * @return the first view in the LayoutManager to snap to, or null if we shouldn't snap to any
 */
@Nullable
private View findView(@NonNull RecyclerView.LayoutManager layoutManager,
                      @NonNull OrientationHelper helper,
                      int gravity,
                      boolean checkEdgeOfList) {

    if (layoutManager.getChildCount() == 0 || !(layoutManager instanceof LinearLayoutManager)) {
        return null;
    }

    final LinearLayoutManager lm = (LinearLayoutManager) layoutManager;

    // If we're at an edge of the list, we shouldn't snap
    // to avoid having the last item not completely visible.
    if (checkEdgeOfList && (isAtEdgeOfList(lm) && !snapLastItem)) {
        return null;
    }

    View edgeView = null;
    int distanceToTarget = Integer.MAX_VALUE;
    final int center;
    if (layoutManager.getClipToPadding()) {
        center = helper.getStartAfterPadding() + helper.getTotalSpace() / 2;
    } else {
        center = helper.getEnd() / 2;
    }

    final boolean snapToStart = (gravity == Gravity.START && !isRtl)
            || (gravity == Gravity.END && isRtl);

    final boolean snapToEnd = (gravity == Gravity.START && isRtl)
            || (gravity == Gravity.END && !isRtl);

    for (int i = 0; i < lm.getChildCount(); i++) {
        View currentView = lm.getChildAt(i);
        int currentViewDistance;
        if (snapToStart) {
            if (!snapToPadding) {
                currentViewDistance = Math.abs(helper.getDecoratedStart(currentView));
            } else {
                currentViewDistance = Math.abs(helper.getStartAfterPadding()
                        - helper.getDecoratedStart(currentView));
            }
        } else if (snapToEnd) {
            if (!snapToPadding) {
                currentViewDistance = Math.abs(helper.getDecoratedEnd(currentView)
                        - helper.getEnd());
            } else {
                currentViewDistance = Math.abs(helper.getEndAfterPadding()
                        - helper.getDecoratedEnd(currentView));
            }
        } else {
            currentViewDistance = Math.abs(helper.getDecoratedStart(currentView)
                    + (helper.getDecoratedMeasurement(currentView) / 2) - center);
        }
        if (currentViewDistance < distanceToTarget) {
            distanceToTarget = currentViewDistance;
            edgeView = currentView;
        }
    }
    return edgeView;
}
 
Example 18
Source File: SnapToStartHelper.java    From aptoide-client-v8 with GNU General Public License v3.0 4 votes vote down vote up
private int distanceToStart(View targetView, OrientationHelper helper) {
  return helper.getDecoratedStart(targetView) - helper.getStartAfterPadding();
}