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 d58994f commit 0f961b0
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1656,8 +1656,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 @@ -1671,12 +1670,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 @@ -15,7 +15,7 @@
import androidx.viewpager.widget.ViewPager;

/**
* @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 @@ -79,7 +79,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 @@ -93,8 +93,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 @@ -128,6 +128,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 @@ -291,6 +326,7 @@ static void stopInterceptRequestLayout(RecyclerView view) {

/**
* 判断父级容器是否是isConsecutive:true.判断到最近的ConsecutiveScrollerLayout容器
*
* @param view
* @return
*/
Expand All @@ -309,6 +345,7 @@ static boolean isConsecutiveScrollParent(View view) {

/**
* 判断当前触摸点下是否有view可以水平滑动
*
* @param rootView
* @param touchX
* @param touchY
Expand Down

0 comments on commit 0f961b0

Please sign in to comment.