欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Android 中 webview加载vue中滑动与viewpager冲突

程序员文章站 2024-02-11 23:37:10
...

Android 中 webview加载vue中滑动与viewpager冲突

重写webview 的onOverScrolled和onTouchEvent方法

public class MyWebview extends WebView {

    public MyWebview(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyWebview(Context context) {
        super(context);
    }

    @Override
    protected void onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY) {
        if (clampedX) {
            ViewParent viewParent = findViewParent(this);
            viewParent.requestDisallowInterceptTouchEvent(false);
        }

        super.onOverScrolled(scrollX, scrollY, clampedX, clampedY);

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            ViewParent viewParent = findViewParent(this);
            viewParent.requestDisallowInterceptTouchEvent(true);
        }
        return super.onTouchEvent(event);

    }

    private ViewParent findViewParent(ViewParent view) {
        ViewParent parent = view.getParent();
        if (parent == null) {
            return null;
        }
        if (parent instanceof ViewPager ||
                parent instanceof AbsListView ||
                parent instanceof ScrollView ||
                parent instanceof HorizontalScrollView ||
                parent instanceof GridView) {
            return parent;
        } else {
            if (parent instanceof View) {
                findViewParent(parent);
            }
        }
        return parent;
    }
}
相关标签: Android学习总结