Use Horizontal Scroll in Android WebView

你。 提交于 2019-12-02 08:10:49

问题


I am currently using epublib-core to read epubs and display them in Android WebView with the following code -

webView.loadDataWithBaseURL(baseURL, new String(spineReferences.get(chapter/* <- int*/).getResource().getData()), "text/html", "utf-8", null);

But it's using Vertical Scroll whereas I want a HorizontalScroll. After searching the web, I found Monocle, but I don't know how to integrate Monocle with epublib and WebView. Any idea on how to use horizontal scroll?


回答1:


Atlast, I could enable Horizontal Scroll in the app (without any Page Transitions). Use this code to scroll horizontally -

Create custom WebViewClient -

public class CustomWebClient extends WebViewClient {
private Context mContext;

public CustomWebClient(Context context) {
    this.mContext = context;
}

@Override
public void onPageFinished(WebView view, String url) {
    super.onPageFinished(view, url);

    final MyWebView myWebView = (MyWebView) 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);

}
}

Edit -

I integrated epub lib with Monocle for nice effects, and here is the link to the whole source code - https://drive.google.com/drive/folders/0B8UizUpBrF1YX3UxcW5nLUVQMEk




回答2:


To get horizontal pagination add css attribute to html before loading to WebView

String html = getBookContent();  // load epub content to String

int density = getResources().getDisplayMetrics().density;
int width = (int) Math.floor(mWebView.getWidth() / density);
int height = (int) Math.floor(mWebView.getHeight() / density);

String style= "<meta name=\"viewport\" content=\"width=device-width, height=device-height, initial-scale=1\"/>\n" +
     "<style type=\"text/css\">body {height: %heightpx; \n" + 
     "-webkit-column-width: %widthpx; -webkit-column-gap:0;}</style></head>";

html = html.replace("</head>", style);
html = html.replace("%width", String.valueOf(width));
html = html.replace("%height", String.valueOf(height));

 mWebView.loadDataWithBaseURL(baseUrl, html, "text/html", "utf-8", null);


来源:https://stackoverflow.com/questions/36617345/use-horizontal-scroll-in-android-webview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!