How to Programmatically Scroll Android WebView

前端 未结 5 2014
眼角桃花
眼角桃花 2020-11-29 23:18

I\'m trying to programmatically scroll a WebView to the location of a particular element in the DOM tree. But so far I haven\'t been able to get the WebView to respond to s

相关标签:
5条回答
  • 2020-11-29 23:40

    It turns out that the window.scrollTo() DOES work, you just can't add your own method named scrollTo(). For some reason my own scrollTo() method was being invoked when I called window.scrollTo().

    So, in summary, to scroll a WebView to a particular DOM element, write a JavaScript function to do the scrolling:

    function scrollToElement(id) {
        var elem = document.getElementById(id);
        var x = 0;
        var y = 0;
    
        while (elem != null) {
            x += elem.offsetLeft;
            y += elem.offsetTop;
            elem = elem.offsetParent;
        }
        window.scrollTo(x, y);
    }
    

    and then from your Android app (Java code), tell your WebView to load a URL:

    webView.loadUrl("javascript:scrollToElement('" + elemId + "')");
    

    There are some issues with this approach, such as the scroll will not be nicely animated, but the general mechanism works.

    The DOM window object does report the current scroll position of the WebView correctly (see window.pageXOffset, window.pageYOffset or window.scrollX, window.scrollY). If you just want to know the current scroll position of the WebView, write some JavaScript to call your Java code and pass along the X/Y offsets.

    0 讨论(0)
  • 2020-11-29 23:54
        @Override
            public void onPageFinished(final WebView view, String url) {
    
                super.onPageFinished(view, url);
    
                    new Handler().postDelayed(new Runnable() {
    
                            @Override
                            public void run() {
                                    if (app.scrollCache.containsKey(app.srctid))
                {
                                app.scrollSpot=app.scrollCache.get(app.srctid); 
                                view.scrollTo(0,(int)app.scrollSpot);
                                }
                            pageFinished=true;
                            }
                        }, 200);
    
    
        }
    
    0 讨论(0)
  • 2020-11-29 23:55

    I've found a better answer to this issue. It turns out that WebView does have scrollTo(), getScrollX() and getScrollY() methods as you'd expect. They're a bit hidden in the documentation because they're inherited from View (via AbsoluteLayout -> ViewGroup -> View). This is obviously a better way to manipulate the WebView's scroll position than the somewhat cumbersome JavaScript interface.

    0 讨论(0)
  • 2020-11-29 23:55

    The following one-line solution ensures that the selected element is brought into view, if it was not. This may be enough in some situations.

    webView.loadUrl("javascript:document.getElementById('"+aID+"').scrollIntoView()");
    
    0 讨论(0)
  • 2020-11-30 00:02

    You cannot do that using Java Code.

    For alls who are stuck at this problem, I have some conclusion about scrolling a webview in android.

    • You cannot scroll a webview or get scrolling position using Java Code.
    • You can only get layoutHeight, measuredHeight, contentHeight of a webview, nothing more. These are all the android api allowing you do in getting and setting scroll status.
    • But, you can override the onScorllChanged to add a self-define listener for scrolling webview event(or fling event), you will be notified the origin position and the new position.
    • Well, you can scroll to a position using JavaScript code, like the following, but the performance is really poor. If you want to scroll it frequently, please give it a second thought.

      window.scrollTo(100,500)

    Consider your situation and choose a proper solution. Also be noticed that fling and scroll are different things.

    0 讨论(0)
提交回复
热议问题