android webview中的水平分页,如ios

时间:2017-01-30 11:06:51

标签: javascript android ios webview uiwebview

func setupScrollDirection() {
        switch readerConfig.scrollDirection {
        case .vertical, .defaultVertical, .horizontalWithVerticalContent:
            scrollView.isPagingEnabled = false
            paginationMode = .unpaginated
            scrollView.bounces = true
            break
        case .horizontal:
            scrollView.isPagingEnabled = true
            paginationMode = .leftToRight
            paginationBreakingMode = .page
            scrollView.bounces = false
            break
        }
    }

是上述代码的替代品,Iam尝试水平使用webview。我尝试过像这样的javaScript

 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: "
                        + (webView.getMeasuredHeight() / getResources().getDisplayMetrics().density)
                        + "px; -webkit-column-gap: 0px; -webkit-column-width: "
                        + webView.getMeasuredWidth() + "px;')";*/
                float height=webView.getMeasuredHeight() / getResources().getDisplayMetrics().density;
                    String insertRule1 = "addCSSRule('html', 'margin:0px; padding:0px; height: "
                        + (height)
                        + "px; -webkit-column-gap: 0px; -webkit-column-width: "
                        + webView.getMeasuredWidth() + "px;')";


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

但是webview在下方有一个空白区域,滚动也没有分页。

1 个答案:

答案 0 :(得分:0)

可以使用javascript:

向您的身体添加style="overflow-x: auto; overflow-y: -webkit-paged-x;"
        String js = "javascript:function initialize() { " +
                "var d = document.getElementsByTagName('body')[0];" +
                "d.style.overflow = '-webkit-paged-x';" +
                "}";
        webView.loadUrl(js);
        webView.loadUrl("javascript:initialize()");

并在您的网络视图中覆盖onOverScrolled,如下所示:

@Override
protected void onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY) {
    scrollY = 0; // disables vertical scroll
    super.onOverScrolled(scrollX, scrollY, clampedX, clampedY);
}

将导致水平滚动而没有分页。 还没有分页的解决方案。