Skip to content

Commit

Permalink
fix #5
Browse files Browse the repository at this point in the history
  • Loading branch information
shenbengit committed Nov 3, 2021
1 parent 0d0f1de commit f5f7c0a
Showing 1 changed file with 30 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public int findTargetSnapPosition(RecyclerView.LayoutManager layoutManager, int
//布局中心位置,水平滑动为X轴坐标,垂直滑动为Y轴坐标
final int layoutCenter = getLayoutCenter(manager);

reacquireSnapList(manager, forwardDirection);
reacquireSnapList(manager);

//目标位置
int targetPosition = RecyclerView.NO_POSITION;
Expand All @@ -96,7 +96,7 @@ public int findTargetSnapPosition(RecyclerView.LayoutManager layoutManager, int
targetPosition = position;
} else {
//寻找上一个锚点位置
targetPosition = position - manager.getOnePageSize();
targetPosition = position - 1;
if (targetPosition < 0) {
targetPosition = RecyclerView.NO_POSITION;
}
Expand All @@ -106,13 +106,20 @@ public int findTargetSnapPosition(RecyclerView.LayoutManager layoutManager, int
//方向向后
if (Math.abs(scrollDistance) >= layoutCenter) {
//计算滑动的距离直接超过布局一半值
targetPosition = position;
targetPosition = position - 1;
if (targetPosition < 0) {
targetPosition = RecyclerView.NO_POSITION;
}
} else {
int viewDecoratedEnd = getViewDecoratedEnd(manager, view);
if (viewDecoratedEnd + Math.abs(scrollDistance) > layoutCenter) {
targetPosition = position;
//寻找上一个锚点位置
targetPosition = position - 1;
if (targetPosition < 0) {
targetPosition = RecyclerView.NO_POSITION;
}
} else {
targetPosition = position + 1;
targetPosition = position;
}
}
}
Expand All @@ -136,18 +143,27 @@ public int findTargetSnapPosition(RecyclerView.LayoutManager layoutManager, int
//即view在中间线的左边或者上边
targetPosition = position2;
} else {
targetPosition = position1;
targetPosition = position2 - 1;
if (targetPosition < 0) {
targetPosition = RecyclerView.NO_POSITION;
}
}
}
} else {
if (Math.abs(scrollDistance) >= layoutCenter) {
targetPosition = position1;
targetPosition = position2 - 1;
if (targetPosition < 0) {
targetPosition = RecyclerView.NO_POSITION;
}
} else {
int viewDecoratedEnd1 = getViewDecoratedEnd(manager, view1);
if (viewDecoratedEnd1 + Math.abs(scrollDistance) >= layoutCenter) {
targetPosition = position1;
targetPosition = position2 - 1;
if (targetPosition < 0) {
targetPosition = RecyclerView.NO_POSITION;
}
} else {
targetPosition = position1 + 1;
targetPosition = position2;
}
}
}
Expand Down Expand Up @@ -176,7 +192,7 @@ public View findSnapView(RecyclerView.LayoutManager layoutManager) {
View snapView = null;
if (layoutManager instanceof PagerGridLayoutManager) {
PagerGridLayoutManager manager = (PagerGridLayoutManager) layoutManager;
reacquireSnapList(manager, true);
reacquireSnapList(manager);
switch (snapList.size()) {
case 1: {
snapView = snapList.get(0);
Expand Down Expand Up @@ -231,10 +247,12 @@ public int[] calculateDistanceToFinalSnap(@NonNull RecyclerView.LayoutManager la
Rect targetRect = new Rect();
layoutManager.getDecoratedBoundsWithMargins(targetView, targetRect);
if (viewDecoratedStart <= layoutCenter) {
//向前回退
Rect snapRect = manager.getStartSnapRect();
dx = PagerGridSmoothScroller.calculateDx(manager, snapRect, targetRect);
dy = PagerGridSmoothScroller.calculateDy(manager, snapRect, targetRect);
} else {
//向后前进
dx = -calculateDxToNextPager(manager, targetRect);
dy = -calculateDyToNextPager(manager, targetRect);
}
Expand All @@ -259,9 +277,8 @@ private boolean isForwardFling(RecyclerView.LayoutManager layoutManager, int vel
/***
* 获取锚点view
* @param manager
* @param forwardDirection
*/
private void reacquireSnapList(PagerGridLayoutManager manager, boolean forwardDirection) {
private void reacquireSnapList(PagerGridLayoutManager manager) {
if (!snapList.isEmpty()) {
snapList.clear();
}
Expand All @@ -272,7 +289,7 @@ private void reacquireSnapList(PagerGridLayoutManager manager, boolean forwardDi
continue;
}
//先去寻找符合锚点位置的view
if (manager.getPosition(child) % manager.getOnePageSize() == (forwardDirection ? 0 : manager.getOnePageSize() - 1)) {
if (manager.getPosition(child) % manager.getOnePageSize() == 0) {
snapList.add(child);
}
}
Expand Down

0 comments on commit f5f7c0a

Please sign in to comment.