Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android WebView smooth scroll

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?

like image 767
SweSnow Avatar asked Nov 19 '12 21:11

SweSnow


3 Answers

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();
like image 80
Keyur Lakhani Avatar answered Oct 30 '22 16:10

Keyur Lakhani


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.

like image 1
MadDeveloper Avatar answered Oct 30 '22 17:10

MadDeveloper


 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~

like image 1
杨玉飞 Avatar answered Oct 30 '22 16:10

杨玉飞