10

I'm trying to scroll a webview programaticly but I'm having some problems. webView.setScrollY() doesn't give me an animation an webView.flingScroll() seems to behave diffrently depending on how long the page is. What is the best way to do this?

SweSnow
  • 17,504
  • 10
  • 36
  • 49

3 Answers3

15

You can scroll using ObjectAnimator by providing current position of webview like this API 11+

ObjectAnimator anim = ObjectAnimator.ofInt(webView, "scrollY", webView.getScrollPositionY(), 0);
anim.setDuration(500).start();
Keyur Lakhani
  • 4,321
  • 1
  • 24
  • 35
  • 8
    Great answer, worked for me. Only thing is that there is no method like getScrollPositionY(), you should use webView.getScrollY() – Rosenpin Feb 20 '17 at 19:00
1

You can use webView.scrollTo(x, y) method. However, this will scroll instantly.

There is no method available for WebView to scroll with animation. If you really have to do it, put the WebView into ScrollView and then you can use e.g. smoothScrollBy etc.

MadDeveloper
  • 796
  • 11
  • 20
  • Nesting scrollable views is not the best idea, gives you all kind of trouble if you don't handle it correctly – joe1806772 Apr 13 '17 at 00:18
1
 private final Property<WebView, Integer> WEBVIEW_SCROLL = new Property<WebView, Integer>(Integer.class, "") {
    @Override
    public Integer get(WebView object) {
        return object.getScrollY();
    }

    @Override
    public void set(WebView object, Integer value) {
        object.scrollTo(object.getScrollX(), value);
    }
};
ObjectAnimator.ofInt(mWebView, WEBVIEW_SCROLL, targetY).setDuration(duration).start();

It's work for me~,you can try this~

杨玉飞
  • 11
  • 2