android webview strange scrolling behaviour

时间:2016-10-20 12:46:28

标签: android webview

My webview doesn't scroll properly . It scroll only , when I press one finger on the screen and scroll using another finger . It doesn't scroll when I remove one finger(which one is used to press) . I know it sounds strange and silly . Here is my code for layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:clickable="false"
              android:orientation="vertical">  
    <ScrollView
        android:id="@+id/childScroll"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.surroundapps.watchme.util.NestedWebView
            android:id="@id/web_view_"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    </ScrollView>
</LinearLayout>

This is how I set up webview

  WebView webView = (WebView) view.findViewById(R.id.web_view_);
        webView.setVerticalScrollBarEnabled(true);
        webView.setHorizontalScrollBarEnabled(true);
        WebSettings settings = webView.getSettings();
        settings.setDomStorageEnabled(true);
        settings.setJavaScriptEnabled(true);
        settings.setLoadWithOverviewMode(false);
        settings.setUseWideViewPort(false);
        settings.setSupportZoom(false);
        settings.setBuiltInZoomControls(true);
        settings.setDisplayZoomControls(true);
        webView.setWebViewClient(new WebViewClient()
        {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url)
            {
                return super.shouldOverrideUrlLoading(view, url);
            }

            @Override
            public void onPageFinished(WebView view, final String url)
            {
            }
        });

        webView.loadUrl(getUrl());

        ScrollView childScroll = (ScrollView) view.findViewById(R.id.childScroll);


        childScroll.setOnTouchListener(new View.OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {
                // Disallow the touch request for parent scroll on touch of child view
                v.getParent().requestDisallowInterceptTouchEvent(true);
                return false;
            }
        });

1 个答案:

答案 0 :(得分:1)

Try using the following custome WebView

package com.mypackage.common.custom.android.widgets

public class TouchyWebView extends WebView {

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

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

public TouchyWebView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

    @Override
    public boolean onTouchEvent(MotionEvent event){
        requestDisallowInterceptTouchEvent(true);
        return super.onTouchEvent(event);
    }          
}

Here requestDisallowInterceptTouchEvent(true); method will allow webview to handle the scroll event.