Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

找到个bug, #3

Open
54itfan opened this issue Dec 7, 2019 · 2 comments
Open

找到个bug, #3

54itfan opened this issue Dec 7, 2019 · 2 comments

Comments

@54itfan
Copy link

54itfan commented Dec 7, 2019

MyWebView
//增加最大递归深度判断,防止出现ANR或者异常
return findViewParentIfNeeds(parent, depth-1)
应该加个 return 不然会回调到第一次递归执行,最后的 return null
实际效果确实不错,感谢!!

@gaojingwen945
Copy link

gaojingwen945 commented Dec 18, 2019

internal fun findViewParentIfNeeds(tag: View, depth: Int): ViewGroup? {
if (depth < 0) {
return null
}
val parent = tag.parent?: return null
if (parent is ViewGroup){
if (canScrollHorizontal(parent)){
return parent
}else {
//增加最大递归深度判断,防止出现ANR或者异常
findViewParentIfNeeds(parent, depth-1)
}
}
return null
}

上面的接口这一行应该加return:
//增加最大递归深度判断,防止出现ANR或者异常
return findViewParentIfNeeds(parent, depth-1)

另外再手动赞一下~

@gaojingwen945
Copy link

gaojingwen945 commented Dec 19, 2019

发现在我这里有这个问题:在webview中,手指按下横向滑动,这个时候表现正常,但是继续纵向滑动(也就是说在同一次ACTION_DOWN事件中)时,父容器会响应这个事件,导致用户总是会误触发父容器的纵向滑动事件。

以下是我的最终代码:

@Override
public boolean onTouchEvent(MotionEvent event) {
    // 解决webview与viewpager等滑动手势冲突问题
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        ViewParent viewParent = findViewParentIfNeeds(this, MAX_PARENT_DEPTH);
        if (viewParent != null) {
            viewParent.requestDisallowInterceptTouchEvent(true);
        }
    }
    return super.onTouchEvent(event);
}

private ViewParent findViewParentIfNeeds(View tag, int depth) {
    if (depth < 0) {
        return null;
    }

    ViewParent parent = tag.getParent();
    if (parent == null) {
        return null;
    }

    if (parent instanceof ViewGroup){
        if (canScrollHorizontally((View) parent) || canScrollVertically((View) parent)){
            return parent;
        }else {
            // 增加最大递归深度判断,防止出现ANR或者异常
            return findViewParentIfNeeds((View) parent, depth - 1);
        }
    }
    return null;
}

/**
 * 是否可以横向滑动
 */
private boolean canScrollHorizontally(View view) {
    return view.canScrollHorizontally(100) || view.canScrollHorizontally(-100);
}

/**
 * 是否可以纵向滑动
 */
private boolean canScrollVertically(View view) {
    // 测试发现Android 4.4的机器上收不到onOverScrolled回调,会导致ACTION_DOWN在webview内部时父容器无法纵向滑动
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        return false;
    }
    return view.canScrollVertically(100) || view.canScrollVertically(-100);
}

@Override
protected void onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY) {
    // 解决webview与viewpager等滑动手势冲突问题
    if (clampedX || clampedY) {
        ViewParent viewParent = findViewParentIfNeeds(this, MAX_PARENT_DEPTH);
        if (viewParent != null) {
            viewParent.requestDisallowInterceptTouchEvent(false);
        }
    }
    super.onOverScrolled(scrollX, scrollY, clampedX, clampedY);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants