How to Programmatically Scroll Android WebView

前端 未结 5 2040
眼角桃花
眼角桃花 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-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.

提交回复
热议问题