Getting document position in webview?

Simply put: I want to mark the current position (possibly several positions) in the html file displayed in the WebView, to restore it to a later version. Any little hint will help. :)

+5
source share
2 answers

If by "current position" you mean a point somewhere on this page, I don’t think this is possible ... at least not purely from Java. The entire WebKit API open to Java does not have a lot of everything related to page content.

If you have control over the contents of the page and there is a way to find out these positions using the built-in Javascript, you can:

# 1: WebView#addJavascriptInterface(), - recordPosition(), ,

# 2: Javascript, recordPosition() .

:

# 3a: Javascript Javascript (, )

# 3b: loadUrl("javascript:..."); WebView, ... Javascript, Javascript 2.

: Javascript , , Java-.

, , , (/ ), Javascript, .

+6

, , :

onSaveInstanceState() , , .

    int contentHeight = webView.getContentHeight();
    int scrollY = webView.getScrollY();

    float scrollPercent = ((float) scrollY / ((float) contentHeight));

. onCreate() ( WebClient.onPageFinished()) :

    int contentHeight = webView.getContentHeight(); 
    int y = (int) ((float) contentHeight * scrollPercent);
    webView.scrollTo(0, y);
  • float .
  • , .
  • . onPause(), webView.setInitialScale(scale), .
  • , - , scrollPercent > 0 .
+6