如何以编程方式滚动 Android WebView [英] How to Programmatically Scroll Android WebView

查看:26
本文介绍了如何以编程方式滚动 Android WebView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 确实有 scrollTo()getScrollX()getScrollY() 方法作为你'd 期待.它们在文档中有点隐藏,因为它们继承自 View(通过 AbsoluteLayout -> ViewGroup -> View).与有些繁琐的 JavaScript 界面相比,这显然是一种更好的方法来操纵 WebView 的滚动位置.

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.

这篇关于如何以编程方式滚动 Android WebView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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