解决ScrollView嵌套WebView后导致网页加载无法显示问题

原因:ScrollView和WebView都有焦点和滑动事件,高度无法自适应,所以监听WebView加载后重新设置高度就可以了。
解决方案一为WebView重新设置高度:

WebSettings webSettings = webView.getSettings();  
        webSettings.setJavaScriptEnabled(true);//设置能够解析Javascript  
        webSettings.setDomStorageEnabled(true);//设置适应Html5的一些方法  
        webView.setWebChromeClient(new WebChromeClient());  
        webView.setWebViewClient(new WebViewClient(){  
            @Override  
            public boolean shouldOverrideUrlLoading(WebView view, String url) {  
//                return super.shouldOverrideUrlLoading(view, url);  
                webView.loadUrl(url);  
                return true;  
            }  
  
            @Override  
            public void onPageFinished(WebView view, String url) {  
//                super.onPageFinished(view, url);  
                progress.setVisibility(View.GONE);  
               //重新为WebView设置高度
                LinearLayout.LayoutParams  params = (LinearLayout.LayoutParams) webView.getLayoutParams();  
                params.width = getScreenWidth();  
                params.height = getScreenHeight()-SizeUtil.dip2px(ScreenShotByH5Activity.this,150);  
                webView.setLayoutParams(params);  
            }  
  
            @Override  
            public void onPageStarted(WebView view, String url, Bitmap favicon) {  
                super.onPageStarted(view, url, favicon);  
                progress.setVisibility(View.VISIBLE);  
            }  
  
            @Override  
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {  
                super.onReceivedError(view, request, error);  
                progress.setVisibility(View.GONE);  
                showToastMessage("加載失敗");  
            }  
        });  
        webView.getSettings().setJavaScriptEnabled(true);  
        webView.loadUrl(url);  

解决ScrollView和WebView的滑动冲突(该方法是让ScrollView和WebView都单独滑动效果不理想)

webView.setOnTouchListener(new View.OnTouchListener() {  
           @Override  
           public boolean onTouch(View v, MotionEvent ev) {  
               ((WebView)v).requestDisallowInterceptTouchEvent(true);  
               return false;  
           }  
       });  

解决方案二:不让WebView滑动,只滑动ScrollView

  

为ScrollView设置android:descendantFocusability="blocksDescendants"属性

你可能感兴趣的:(解决ScrollView嵌套WebView后导致网页加载无法显示问题)