WebView OverScroll

4.6k Views Asked by At

I'm trying to make a webView "bounce" when pulled after scroll reached the max (kind of like the "pull-to-refresh" effect).

I have my custom view extending WebView, and overriding the method

 @Override
protected boolean overScrollBy(final int deltaX, final int deltaY, final int scrollX, final int scrollY, final int scrollRangeX, final int scrollRangeY, final int maxOverScrollX, final int maxOverScrollY, final boolean isTouchEvent)
{
    VerticalOverScrollController.Result result = overscrollController.calcVerticalOverScroll(deltaY, scrollY);
    Log.d("overScrollBy", "scrollY " + result.scrollY + " overScrollY " + result.overScrollY);

    return super.overScrollBy(deltaX, deltaY, scrollX, result.getScrollY(), scrollRangeX, scrollRangeY, maxOverScrollX, result.getOverScrollY(), isTouchEvent);
}

and calcVerticalOverScroll is

 public Result calcVerticalOverScroll(final int deltaY, final int scrollY)
{
    Result result = new Result();

    if (scrollY <= 0 && deltaY < 0)//if at top and pulling down...
    {
        result.scrollY = (maxTopOverScroll > 0) ? scrollY : 0;
        result.overScrollY = maxTopOverScroll;
    } else
    {
        result.scrollY = (maxBottomOverScroll > 0) ? scrollY : 0;
        result.overScrollY = maxBottomOverScroll;
    }

    return result;
}

and Result is just

  public static class Result
{
    int scrollY;
    int overScrollY;

    ... getters()
}

The thing is, this works perfectly on any view (including webview) before KitKat.

After KitKat this works perfectly on any view except WebView, where the log for scrollY is always 0.

Any ideas on what could have changed on this version of the WebView?

If this does not fly, any ideas on how to achieve the overScroll effect on a WebView correctly?

Thanks in Advance.

2

There are 2 best solutions below

4
On

Perhaps this project may help you out. It doesn't currently support WebViews but is very easily extensible, and could solve your problem with just a few lines of code -- i.e. by implementing a proprietary over-scroll adapter to fit Web-views. Should be something like this:

// First, define an adapter class (this could work as-is, haven't tried it):

public class WebViewOverScrollDecorAdapter implements IOverScrollDecoratorAdapter {

    protected final WebView mWebView;

    public WebViewOverScrollDecorAdapter(WebView view) {
        mWebView = view;
    }

    @Override
    public View getView() {
        return mWebView;
    }

    @Override
    public boolean isInAbsoluteStart() {
        return !mWebView.canScrollVertically(-1);
    }

    @Override
    public boolean isInAbsoluteEnd() {
        return !mWebView.canScrollVertically(1);
    }
}

// Then, apply it over the web-view:

WebView webView = ...;
new VerticalOverScrollBounceEffectDecorator(new WebViewOverScrollDecorAdapter(webView));

If you manage to work it out please consider contributing your code to the project on GitHub.

0
On

Android 4.4 KitKat (Android 19 )introduces a new version of WebView that is based on Chromium. Here in this chromium browser they have removed many features from webview.

Some of these bugs have already been reported to Google. Check the links : Restore scroll position feature doesn't work on KitKat

From the links, this is also one of the bug reported

On Android 4.4 KitKat, probably related to the Chromium WebView, the scroll position of the previous article is not restored when you go back.

This is not an exact answer for this question, but just pointing to the things. But there must be a workaround for doing this in new webview. Let me check it and update my answer!!