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?
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
I've found a better answer to this issue. It turns out that
WebView
does havescrollTo()
,getScrollX()
andgetScrollY()
methods as you'd expect. They're a bit hidden in the documentation because they're inherited fromView
(viaAbsoluteLayout
->ViewGroup
->View
). This is obviously a better way to manipulate theWebView
's scroll position than the somewhat cumbersome JavaScript interface.It turns out that the
window.scrollTo()
DOES work, you just can't add your own method namedscrollTo()
. For some reason my ownscrollTo()
method was being invoked when I calledwindow.scrollTo()
.So, in summary, to scroll a WebView to a particular DOM element, write a
JavaScript
function to do the scrolling:and then from your Android app (Java code), tell your WebView to load a URL:
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
orwindow.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.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.
Consider your situation and choose a proper solution. Also be noticed that fling and scroll are different things.