如何以编程方式滚动 Android WebView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2238938/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:06:58  来源:igfitidea点击:

How to Programmatically Scroll Android WebView

androidscrollwebview

提问by Steve Liddle

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 scroll requests. I've tried calling JavaScript that uses window.scrollTo(...), but the WebView doesn't respond. On the Java side, I've tried calling the WebView.flingScroll(...)method. WebView will respond to flingScroll, but what I need is a scrollTo(...)capability. Any ideas?

我正在尝试以编程方式将 WebView 滚动到 DOM 树中特定元素的位置。但到目前为止,我还无法让 WebView 响应滚动请求。我试过调用使用 的 JavaScript window.scrollTo(...),但 WebView 没有响应。在 Java 方面,我尝试调用该WebView.flingScroll(...)方法。WebView 将响应flingScroll,但我需要的是一种scrollTo(...)能力。有任何想法吗?

回答by Steve Liddle

I've found a better answer to this issue. It turns out that WebViewdoes 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.

我找到了一个更好的答案来解决这个问题。事实证明,WebView确实有scrollTo(),getScrollX()getScrollY()您期望的方法。它们在文档中有点隐藏,因为它们是从View(via AbsoluteLayout-> ViewGroup-> View)继承而来的。WebView与有些繁琐的 JavaScript 界面相比,这显然是一种更好的方式来操纵的滚动位置。

回答by Steve Liddle

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().

事实证明,window.scrollTo()确实有效,您只是无法添加自己的名为scrollTo(). 出于某种原因,scrollTo()当我调用window.scrollTo().

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

因此,总而言之,要将 WebView 滚动到特定的 DOM 元素,请编写一个JavaScript函数来进行滚动:

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:

然后从您的 Android 应用程序(Java 代码)中,告诉您的 WebView 加载一个 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.pageYOffsetor 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.

在DOM窗口对象不正确地报告的WebView的当前滚动位置(见window.pageXOffsetwindow.pageYOffset或者window.scrollXwindow.scrollY)如果你只是想知道的WebView的当前滚动位置,写一些JavaScript调用Java代码,并沿X / Y偏移传递.

回答by York

You cannot do that using Java Code.

您不能使用 Java Code 做到这一点

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

对于所有陷入这个问题的人,我有一些关于在 android 中滚动 webview 的结论。

  • 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)

  • 您不能使用 Java 代码滚动 web 视图或获取滚动位置。
  • 你只能得到一个webview的layoutHeight、measuredHeight、contentHeight,仅此而已。这些都是允许您获取和设置滚动状态的 android api。
  • 但是,您可以覆盖 onScorllChanged 为滚动 webview 事件(或 fling 事件)添加自定义侦听器,您将收到原点位置和新位置的通知。
  • 好吧,你可以使用JavaScript代码滚动到一个位置,如下所示,但性能真的很差。如果您想经常滚动它,请三思。

    window.scrollTo(100,500)

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

考虑您的情况并选择合适的解决方案。还要注意,fling 和 scroll 是不同的东西。

回答by irshst

    @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);


    }