Webview作为水平寻呼机-Android

时间:2019-05-09 05:38:17

标签: android webview

我尝试通过水平滑动(而不是垂直滚动)在webview中显示大型html内容。我用以下代码做到了

 private  void injectJavascript() {
    String js = "function initialize(){\n   " +
            " var d = document.getElementsByTagName('body')[0];\n  " +
            " var ourH = window.innerHeight -20;\n   " + // 20 px is a padding gap
            " var ourW = window.innerWidth -20;\n    " +
            " var fullH = d.offsetHeight;\n   " +
            " var pageCount = Math.floor(fullH/ourH)+1;\n   " +
            " var currentPage = 0;\n   " +
            " var newW = pageCount* window.innerWidth -20 ;\n   " +
            " d.style.height = ourH+'px';\n   " +
            " d.style.width = newW+'px';\n   " +
            " d.style.margin = 0;\n   " +
            " d.style.webkitColumnGap = '20px';\n   " +
            " d.style.webkitColumnCount = pageCount;\n   " +
            " document.head.innerHTML = document.head.innerHTML +" +
            " '<meta name=\"viewport\" content=\"height=device-height, user-scalable=no\" />';   " +
            " return pageCount;\n}";
    this.loadUrl("javascript:" + js);
    this.loadUrl("javascript:alert(initialize())");
}

有时它给出错误的页数。例如:返回8作为页数,但内容需要9。我也尝试过 Math.ceil()

我需要使用Webview的 viewpager显示。在SO和某些网站中尝试所有给定的解决方案后,我将发布此问题。请帮助我!

我也尝试了viewpager的Page Splitting(SO中给出的PageSplitter代码)概念。它不是在精确地分割单词。所以我尝试使用webview。

1 个答案:

答案 0 :(得分:0)

我使用了很长时间,检查是否仍在工作

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


    final WebView myWebView = (WebView) view;

    String varMySheet = "var mySheet = document.styleSheets[0];";

    String addCSSRule = "function addCSSRule(selector, newRule) {"
            + "ruleIndex = mySheet.cssRules.length;"
            + "mySheet.insertRule(selector + '{' + newRule + ';}', ruleIndex);"

            + "}";

    String insertRule1 = "addCSSRule('html', 'padding: 0px; height: "
            + (myWebView.getMeasuredHeight() / mContext.getResources().getDisplayMetrics().density)
            + "px; -webkit-column-gap: 0px; -webkit-column-width: "
            + myWebView.getMeasuredWidth() + "px;')";


    myWebView.loadUrl("javascript:" + varMySheet);
    myWebView.loadUrl("javascript:" + addCSSRule);
    myWebView.loadUrl("javascript:" + insertRule1);


    view.setVisibility(View.VISIBLE);
    super.onPageFinished(view, url);
    //without delay gives wrong count
    CountDownTimer test=  new CountDownTimer(3000,1000) {

        @Override
        public void onTick(long millisUntilFinished) {


        }

        @Override
        public void onFinish() {

            int widthis =  webView.getContentWidth();

            TotalScrollWidth=widthis;
                    Log.i("WidthTotoal Scroll", ""+widthis);

                    TotalPages= (TotalScrollWidth/ScreenWidth)-1;
                //  Toast.makeText(getApplicationContext(), "Total Pages are" +TotalPages, 100).show();
                     progressPages.setMax(TotalPages);

                    progressBar.setVisibility(View.GONE);
        }
    };

    test.start();

}

我知道其中包含一些不良代码,请耐心等待。