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?
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();
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.
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~
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With