Skip to content

Commit

Permalink
处理RecyclerView判断滑动到顶部和底部不准确的问题。
Browse files Browse the repository at this point in the history
  • Loading branch information
teach committed Jul 30, 2020
1 parent 2aaa4e2 commit 5431a80
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1655,8 +1655,7 @@ public boolean isScrollTop() {
List<View> children = getEffectiveChildren();
if (children.size() > 0) {
View child = children.get(0);
View scrolledView = ScrollUtils.getScrolledView(child);
return getScrollY() <= 0 && !scrolledView.canScrollVertically(-1);
return getScrollY() <= 0 && !ScrollUtils.canScrollVertically(child,-1);
}
return true;
}
Expand All @@ -1670,12 +1669,20 @@ public boolean isScrollBottom() {
List<View> children = getEffectiveChildren();
if (children.size() > 0) {
View child = children.get(children.size() - 1);
View scrolledView = ScrollUtils.getScrolledView(child);
return getScrollY() >= mScrollRange && !scrolledView.canScrollVertically(1);
return getScrollY() >= mScrollRange && !ScrollUtils.canScrollVertically(child,1);
}
return true;
}

@Override
public boolean canScrollVertically(int direction) {
if (direction > 0){
return !isScrollBottom();
} else {
return !isScrollTop();
}
}

/**
* 禁止设置滑动监听,因为这个监听器已无效
* 若想监听容器的滑动,请使用
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import java.util.List;

/**
* @Author teach liang
* @Author donkingliang
* @Description
* @Date 2020/5/22
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import android.view.ViewGroup;

/**
* @Author teach liang
* @Author donkingliang
* @Description
* @Date 2020/5/22
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ static int computeVerticalScrollExtent(View view) {
*/
static int getScrollTopOffset(View view) {
if (isConsecutiveScrollerChild(view) && canScrollVertically(view, -1)) {
return -computeVerticalScrollOffset(view);
return Math.min(-computeVerticalScrollOffset(view), -1);
} else {
return 0;
}
Expand All @@ -92,8 +92,8 @@ static int getScrollTopOffset(View view) {
*/
static int getScrollBottomOffset(View view) {
if (isConsecutiveScrollerChild(view) && canScrollVertically(view, 1)) {
return computeVerticalScrollRange(view) - computeVerticalScrollOffset(view)
- computeVerticalScrollExtent(view);
return Math.max(computeVerticalScrollRange(view) - computeVerticalScrollOffset(view)
- computeVerticalScrollExtent(view), 1);
} else {
return 0;
}
Expand Down Expand Up @@ -127,6 +127,41 @@ static boolean canScrollVertically(View view, int direction) {
return false;
}
} else {
// RecyclerView通过canScrollVertically方法判断滑动到边界不准确,需要单独处理
if (scrolledView instanceof RecyclerView) {
RecyclerView recyclerView = (RecyclerView) scrolledView;
RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
RecyclerView.Adapter adapter = recyclerView.getAdapter();

if (layoutManager != null && adapter != null && adapter.getItemCount() > 0) {
View itemView = layoutManager.findViewByPosition(direction > 0 ? adapter.getItemCount() - 1 : 0);
if (itemView == null) {
return true;
}
} else {
return false;
}

int count = recyclerView.getChildCount();
if (direction > 0) {
for (int i = count - 1; i >= 0; i--) {
View child = recyclerView.getChildAt(i);
if (child.getY() + child.getHeight() > recyclerView.getHeight() - recyclerView.getPaddingBottom()) {
return true;
}
}
return false;
} else {
for (int i = 0; i < count; i++) {
View child = recyclerView.getChildAt(i);
if (child.getY() < recyclerView.getPaddingTop()) {
return true;
}
}
return false;
}
}

return scrolledView.canScrollVertically(direction);
}
}
Expand Down Expand Up @@ -325,4 +360,5 @@ static boolean isHorizontalScroll(View rootView, int touchX, int touchY) {
return false;
}


}

0 comments on commit 5431a80

Please sign in to comment.