Skip to content

Commit

Permalink
优化fling
Browse files Browse the repository at this point in the history
  • Loading branch information
teach committed Jul 1, 2021
1 parent 310a0bd commit c9919b2
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ConsecutiveViewPager2> reference;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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。(内容可以滚动,或者本身就是个滚动布局)
*
Expand Down

0 comments on commit c9919b2

Please sign in to comment.