From c9919b2c43f03fa37f5f6a9e51267bb0b3c2ec97 Mon Sep 17 00:00:00 2001 From: teach Date: Thu, 1 Jul 2021 10:43:54 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96fling?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../consecutivescroller/ConsecutiveScrollerLayout.java | 10 +++++++--- .../consecutivescroller/ConsecutiveViewPager2.java | 10 ++++++++++ .../donkingliang/consecutivescroller/ScrollUtils.java | 10 ++++++++++ 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/consecutivescroller/src/main/java/com/donkingliang/consecutivescroller/ConsecutiveScrollerLayout.java b/consecutivescroller/src/main/java/com/donkingliang/consecutivescroller/ConsecutiveScrollerLayout.java index 9e62bd5..0881fea 100644 --- a/consecutivescroller/src/main/java/com/donkingliang/consecutivescroller/ConsecutiveScrollerLayout.java +++ b/consecutivescroller/src/main/java/com/donkingliang/consecutivescroller/ConsecutiveScrollerLayout.java @@ -633,7 +633,9 @@ public boolean dispatchTouchEvent(MotionEvent ev) { recycleAdjustVelocityTracker(); int touchX = ScrollUtils.getRawX(this, ev, actionIndex); int touchY = ScrollUtils.getRawY(this, ev, actionIndex); - boolean canScrollVerticallyChild = ScrollUtils.canScrollVertically(getTouchTarget(touchX, touchY)); + View targetView = getTouchTarget(touchX, touchY); + boolean canScrollVerticallyChild = ScrollUtils.canScrollVertically(targetView); + boolean canScrollHorizontallyChild = ScrollUtils.canScrollHorizontally(targetView); if (SCROLL_ORIENTATION != SCROLL_VERTICAL && canScrollVerticallyChild && Math.abs(yVelocity) >= mMinimumVelocity && !ScrollUtils.isHorizontalScroll(this, touchX, touchY)) { @@ -642,9 +644,11 @@ public boolean dispatchTouchEvent(MotionEvent ev) { ev.setAction(MotionEvent.ACTION_CANCEL); } - if (SCROLL_ORIENTATION == SCROLL_NONE && !ScrollUtils.isConsecutiveScrollParent(this) + if (SCROLL_ORIENTATION != SCROLL_VERTICAL && !ScrollUtils.isConsecutiveScrollParent(this) && isIntercept(ev) && Math.abs(yVelocity) >= mMinimumVelocity) { - fling(-mAdjustYVelocity); + if (SCROLL_ORIENTATION == SCROLL_NONE || !canScrollHorizontallyChild){ + fling(-mAdjustYVelocity); + } } } diff --git a/consecutivescroller/src/main/java/com/donkingliang/consecutivescroller/ConsecutiveViewPager2.java b/consecutivescroller/src/main/java/com/donkingliang/consecutivescroller/ConsecutiveViewPager2.java index cf9b9fb..8393d5a 100644 --- a/consecutivescroller/src/main/java/com/donkingliang/consecutivescroller/ConsecutiveViewPager2.java +++ b/consecutivescroller/src/main/java/com/donkingliang/consecutivescroller/ConsecutiveViewPager2.java @@ -257,6 +257,16 @@ public void unregisterOnPageChangeCallback(@NonNull ViewPager2.OnPageChangeCallb mViewPager2.unregisterOnPageChangeCallback(callback); } + @Override + public boolean canScrollHorizontally(int direction) { + return mViewPager2.canScrollHorizontally(direction); + } + + @Override + public boolean canScrollVertically(int direction) { + return mViewPager2.canScrollVertically(direction); + } + private static class AttachListener implements OnAttachStateChangeListener { WeakReference reference; diff --git a/consecutivescroller/src/main/java/com/donkingliang/consecutivescroller/ScrollUtils.java b/consecutivescroller/src/main/java/com/donkingliang/consecutivescroller/ScrollUtils.java index cf076e6..bb1edaa 100644 --- a/consecutivescroller/src/main/java/com/donkingliang/consecutivescroller/ScrollUtils.java +++ b/consecutivescroller/src/main/java/com/donkingliang/consecutivescroller/ScrollUtils.java @@ -111,6 +111,16 @@ static int getScrollBottomOffset(View view) { } } + /** + * 是否是可以水平滚动View。(内容可以滚动,或者本身就是个滚动布局) + * + * @param view + * @return + */ + static boolean canScrollHorizontally(View view) { + return isConsecutiveScrollerChild(view) && (view.canScrollHorizontally(1) || view.canScrollHorizontally(-1)); + } + /** * 是否是可以垂直滚动View。(内容可以滚动,或者本身就是个滚动布局) *