Skip to content

Commit

Permalink
优化滑动冲突处理
Browse files Browse the repository at this point in the history
  • Loading branch information
梁任彦 committed Jul 6, 2020
1 parent 6b854eb commit a50f514
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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<View> views = getTouchViews(rootView, touchX, touchY);
for (View view : views) {
if (view.canScrollHorizontally(1) || view.canScrollHorizontally(-1)) {
return true;
}
}
return false;
}


}

0 comments on commit a50f514

Please sign in to comment.