From 6aac43d8daa2b2367c3ce834334d436f280a8a65 Mon Sep 17 00:00:00 2001 From: teach Date: Wed, 22 Dec 2021 11:08:27 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9bug=EF=BC=9B=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ConsecutiveScrollerLayout.java | 4 +-- .../consecutivescroller/ScrollUtils.java | 29 +++++++++++++------ 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/consecutivescroller/src/main/java/com/donkingliang/consecutivescroller/ConsecutiveScrollerLayout.java b/consecutivescroller/src/main/java/com/donkingliang/consecutivescroller/ConsecutiveScrollerLayout.java index 1bbf9b2..a8c0e31 100644 --- a/consecutivescroller/src/main/java/com/donkingliang/consecutivescroller/ConsecutiveScrollerLayout.java +++ b/consecutivescroller/src/main/java/com/donkingliang/consecutivescroller/ConsecutiveScrollerLayout.java @@ -672,10 +672,10 @@ public boolean dispatchTouchEvent(MotionEvent ev) { int touchY = ScrollUtils.getRawY(this, ev, actionIndex); View targetView = getTouchTarget(touchX, touchY); boolean canScrollVerticallyChild = ScrollUtils.canScrollVertically(targetView); - boolean canScrollHorizontallyChild = ScrollUtils.canScrollHorizontally(targetView); + boolean canScrollHorizontallyChild = ScrollUtils.isHorizontalScroll(this, touchX, touchY); if (SCROLL_ORIENTATION != SCROLL_VERTICAL && canScrollVerticallyChild && Math.abs(yVelocity) >= mMinimumVelocity - && !ScrollUtils.isHorizontalScroll(this, touchX, touchY)) { + && !canScrollHorizontallyChild) { //如果当前是横向滑动,但是触摸的控件可以垂直滑动,并且产生垂直滑动的fling事件, // 为了不让这个控件垂直fling,把事件设置为MotionEvent.ACTION_CANCEL。 ev.setAction(MotionEvent.ACTION_CANCEL); diff --git a/consecutivescroller/src/main/java/com/donkingliang/consecutivescroller/ScrollUtils.java b/consecutivescroller/src/main/java/com/donkingliang/consecutivescroller/ScrollUtils.java index afb8a3a..46018d3 100644 --- a/consecutivescroller/src/main/java/com/donkingliang/consecutivescroller/ScrollUtils.java +++ b/consecutivescroller/src/main/java/com/donkingliang/consecutivescroller/ScrollUtils.java @@ -22,6 +22,10 @@ */ public class ScrollUtils { + static Method computeVerticalScrollOffsetMethod; + static Method computeVerticalScrollRangeMethod; + static Method computeVerticalScrollExtentMethod; + static int computeVerticalScrollOffset(View view) { View scrolledView = getScrolledView(view); @@ -30,9 +34,11 @@ static int computeVerticalScrollOffset(View view) { } try { - Method method = View.class.getDeclaredMethod("computeVerticalScrollOffset"); - method.setAccessible(true); - Object o = method.invoke(scrolledView); + if (computeVerticalScrollOffsetMethod == null){ + computeVerticalScrollOffsetMethod = View.class.getDeclaredMethod("computeVerticalScrollOffset"); + computeVerticalScrollOffsetMethod.setAccessible(true); + } + Object o = computeVerticalScrollOffsetMethod.invoke(scrolledView); if (o != null){ return (int) o; } @@ -50,9 +56,11 @@ static int computeVerticalScrollRange(View view) { } try { - Method method = View.class.getDeclaredMethod("computeVerticalScrollRange"); - method.setAccessible(true); - Object o = method.invoke(scrolledView); + if (computeVerticalScrollRangeMethod == null) { + computeVerticalScrollRangeMethod = View.class.getDeclaredMethod("computeVerticalScrollRange"); + computeVerticalScrollRangeMethod.setAccessible(true); + } + Object o = computeVerticalScrollRangeMethod.invoke(scrolledView); if (o != null){ return (int) o; } @@ -70,9 +78,12 @@ static int computeVerticalScrollExtent(View view) { } try { - Method method = View.class.getDeclaredMethod("computeVerticalScrollExtent"); - method.setAccessible(true); - Object o = method.invoke(scrolledView); + if (computeVerticalScrollExtentMethod == null){ + computeVerticalScrollExtentMethod = View.class.getDeclaredMethod("computeVerticalScrollExtent"); + computeVerticalScrollExtentMethod.setAccessible(true); + } + + Object o = computeVerticalScrollExtentMethod.invoke(scrolledView); if (o != null){ return (int) o; }