From a50f51412f4b22d11ea76d548bf3921c69905687 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A2=81=E4=BB=BB=E5=BD=A6?= Date: Mon, 6 Jul 2020 23:07:56 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=BB=91=E5=8A=A8=E5=86=B2?= =?UTF-8?q?=E7=AA=81=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ConsecutiveScrollerLayout.java | 16 ++++++-- .../consecutivescroller/ScrollUtils.java | 40 ++++++++++++++++++- 2 files changed, 50 insertions(+), 6 deletions(-) diff --git a/consecutivescroller/src/main/java/com/donkingliang/consecutivescroller/ConsecutiveScrollerLayout.java b/consecutivescroller/src/main/java/com/donkingliang/consecutivescroller/ConsecutiveScrollerLayout.java index bf8a717..30e6efe 100644 --- a/consecutivescroller/src/main/java/com/donkingliang/consecutivescroller/ConsecutiveScrollerLayout.java +++ b/consecutivescroller/src/main/java/com/donkingliang/consecutivescroller/ConsecutiveScrollerLayout.java @@ -547,15 +547,19 @@ public boolean dispatchTouchEvent(MotionEvent ev) { mAdjustVelocityTracker.computeCurrentVelocity(1000, mMaximumVelocity); int yVelocity = (int) mAdjustVelocityTracker.getYVelocity(); recycleAdjustVelocityTracker(); - boolean canScrollVerticallyChild = ScrollUtils.canScrollVertically(getTouchTarget( - ScrollUtils.getRawX(this, ev, actionIndex), ScrollUtils.getRawY(this, ev, actionIndex))); - if (SCROLL_ORIENTATION != SCROLL_VERTICAL && canScrollVerticallyChild && Math.abs(yVelocity) >= mMinimumVelocity) { + int touchX = ScrollUtils.getRawX(this, ev, actionIndex); + int touchY= ScrollUtils.getRawY(this, ev, actionIndex); + boolean canScrollVerticallyChild = ScrollUtils.canScrollVertically(getTouchTarget(touchX, touchY)); + if (SCROLL_ORIENTATION != SCROLL_VERTICAL && canScrollVerticallyChild + && Math.abs(yVelocity) >= mMinimumVelocity + && !ScrollUtils.isHorizontalScroll(this,touchX,touchY)) { //如果当前是横向滑动,但是触摸的控件可以垂直滑动,并且产生垂直滑动的fling事件, // 为了不让这个控件垂直fling,把事件设置为MotionEvent.ACTION_CANCEL。 ev.setAction(MotionEvent.ACTION_CANCEL); } - if (SCROLL_ORIENTATION == SCROLL_NONE && isIntercept(ev) && Math.abs(yVelocity) >= mMinimumVelocity) { + if (SCROLL_ORIENTATION == SCROLL_NONE && !ScrollUtils.isConsecutiveScrollParent(this) + && isIntercept(ev) && Math.abs(yVelocity) >= mMinimumVelocity) { fling(-yVelocity); } } @@ -614,6 +618,10 @@ public boolean onInterceptTouchEvent(MotionEvent ev) { @Override public boolean onTouchEvent(MotionEvent ev) { + if (ScrollUtils.isConsecutiveScrollParent(this)){ + return false; + } + MotionEvent vtev = MotionEvent.obtain(ev); if (ev.getActionMasked() == MotionEvent.ACTION_DOWN) { diff --git a/consecutivescroller/src/main/java/com/donkingliang/consecutivescroller/ScrollUtils.java b/consecutivescroller/src/main/java/com/donkingliang/consecutivescroller/ScrollUtils.java index 95e30ba..be1ebf0 100644 --- a/consecutivescroller/src/main/java/com/donkingliang/consecutivescroller/ScrollUtils.java +++ b/consecutivescroller/src/main/java/com/donkingliang/consecutivescroller/ScrollUtils.java @@ -266,7 +266,7 @@ static View getScrolledView(View view) { } static boolean startInterceptRequestLayout(RecyclerView view) { - if ("InterceptRequestLayout".equals(view.getTag())){ + if ("InterceptRequestLayout".equals(view.getTag())) { try { Method method = RecyclerView.class.getDeclaredMethod("startInterceptRequestLayout"); method.setAccessible(true); @@ -279,7 +279,7 @@ static boolean startInterceptRequestLayout(RecyclerView view) { } static void stopInterceptRequestLayout(RecyclerView view) { - if ("InterceptRequestLayout".equals(view.getTag())){ + if ("InterceptRequestLayout".equals(view.getTag())) { try { Method method = RecyclerView.class.getDeclaredMethod("stopInterceptRequestLayout", boolean.class); method.setAccessible(true); @@ -289,4 +289,40 @@ static void stopInterceptRequestLayout(RecyclerView view) { } } + /** + * 判断父级容器是否是isConsecutive:true.判断到最近的ConsecutiveScrollerLayout容器 + * @param view + * @return + */ + static boolean isConsecutiveScrollParent(View view) { + View child = view; + while (child.getParent() instanceof ViewGroup && !(child.getParent() instanceof ConsecutiveScrollerLayout)) { + child = (View) child.getParent(); + } + + if (child.getParent() instanceof ConsecutiveScrollerLayout) { + return isConsecutiveScrollerChild(child); + } else { + return false; + } + } + + /** + * 判断当前触摸点下是否有view可以水平滑动 + * @param rootView + * @param touchX + * @param touchY + * @return + */ + static boolean isHorizontalScroll(View rootView, int touchX, int touchY) { + List views = getTouchViews(rootView, touchX, touchY); + for (View view : views) { + if (view.canScrollHorizontally(1) || view.canScrollHorizontally(-1)) { + return true; + } + } + return false; + } + + }