的WebView - setInitialScale prevent scrollTo滚动网页视图 [英] Webview - setInitialScale prevent scrollTo to scroll the Webview

查看:206
本文介绍了的WebView - setInitialScale prevent scrollTo滚动网页视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用web视图与本地HTML文件的制定严重的书籍的应用程序。

I'm developing an application for serious of books by using Webview with local html files.

我想允许用户返回到他具有相同的字体大小,他选择读出的最后位置。

I want to allow the user to return to the last location that he read with the same font size that he choose.


  • 字体大小 - 我用的getScale存储选择缩放,当用户关闭该应用程序,而当用户打开我使用setInitialScale与存储的比例加载它的应用程序。它的工作原理不错。

  • 位置 - 我用getScrollY存储滚动,当用户关闭该应用程序,而当用户打开我使用scrollTo与存储的滚动加载它的应用程序。它的工作原理不错。

问题时,我同时使用的。字体大小正确装载,但滚动不发生,我看到的HTML文件的顶部。
我试图把setInitialScale和scrollTo在不同的位置在code,但没有解决问题。

The problem is when I'm using both. The font size loaded correctly, but the scrolling is not happen and I see the top of the html file. I tried to put setInitialScale and scrollTo in different locations in the code but nothing solve the issue.

任何想法,我怎么能实现呢?

Any idea how can I implement it?

也许web视图具有允许做其他方式的任何方法?

Maybe webview has any method that allow doing it in other way?

推荐答案

您可能正在运行到某种种族 - 你不能web视图之前调用 scrollTo 加载完成,并已经有了布局传递。

You're probably running into some sort of race - you can't call scrollTo before the WebView has finished loading and has had a layout pass.

一个办法是重写 WebView.onSizeChanged ,等待的WebView来调整到正确的高度滚动前:

One approach would be to override WebView.onSizeChanged and wait for the WebView to resize to the 'right' height before scrolling:

@Override
void onSizeChanged(int w, int h, int ow, int oh) {
    super.onSizeChanged(w, h, ow, oh);
    if (h >= savedScrollOffset)
      scrollBy(0, savedScrollOffset);
}

另一种选择,如果你控制了HTML和JavaScript的使用,将是保存和恢复使用JavaScript滚动偏移量。你可以使用localStorage的保存滚动偏移量,然后加载页面时,它从localStorage的恢复。

Another option, if you control the HTML and use JavaScript, would be to save and restore the scroll offset using JavaScript. You could use LocalStorage to save the scroll offset and then restore it from LocalStorage when the page is loaded.

这篇关于的WebView - setInitialScale prevent scrollTo滚动网页视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆