美文网首页
RecyclerView 添加Webview为headerVie

RecyclerView 添加Webview为headerVie

作者: ysnows | 来源:发表于2017-06-24 14:17 被阅读277次

    设置如下代码即可解决webview的大小问题:

     private void setupWebView() {
            webView.getSettings().setJavaScriptEnabled(true);
            webView.setWebViewClient(new WebViewClient() {
                @Override
                public void onPageFinished(WebView view, String url) {
                    webView.loadUrl("javascript:MyApp.resize(document.body.getBoundingClientRect().height)");
                    super.onPageFinished(view, url);
                }
            });
            webView.addJavascriptInterface(this, "MyApp");
        }
    
        @JavascriptInterface
        public void resize(final float height) {
            ((Activity) getContext()).runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    webView.setLayoutParams(new LinearLayout.LayoutParams(getResources().getDisplayMetrics().widthPixels, (int) (height * getResources().getDisplayMetrics().density)));
                }
            });
        }
    
    

    相关文章

      网友评论

          本文标题:RecyclerView 添加Webview为headerVie

          本文链接:https://www.haomeiwen.com/subject/umrkcxtx.html