Skip to content Skip to sidebar Skip to footer

Weird Miscalculation When Trying To Detect If Webview Scrolled To Bottom

I have a problem detecting when WebView is scrolled to bottom. I use following code: @Override protected void onScrollChanged(int x, int y, int oldX, int oldY) { super.onScroll

Solution 1:

The calculation you have in there seems correct. It would be better expressed as:

@OverrideprotectedvoidonScrollChanged(int x, int y, int oldX, int oldY) {
    super.onScrollChanged(x, y, oldX, oldY);
    finalintDIRECTION_UP=1;
    if (!canScrollVertically(DIRECTION_UP)) {
        // Trigger listener.
    }
}

There are a couple of cases where you might not be able to detect that you're at the bottom of the page this way:

  • If the page is handling the scrolling using touch handlers and the pageScale() is greater than 1.0. In this case 'bottom' is detected in the CSS coordinate space and, due to rounding, may not always map correctly to the android.view.View coordinate space (although this should be fixed in KitKat).
  • You or some library you're using is overriding WebView.scrollTo/overScrollBy/etc.. to not allow the scroll to go all the way to the bottom.

As a last resort workaround you could override onOverScrolled:

@OverrideprotectedvoidonOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY) {
    if (scrollY > 0 && clampedY && !startedOverScrollingY) {
        startedOverScrollingY = true;
        // Trigger listener.
    }
    super.onOverScrolled(scrollX, scrollY, clampedX, clampedY);
}

@OverrideprotectedvoidonScrollChanged(int x, int y, int oldX, int oldY) {
    super.onScrollChanged(x, y, oldX, oldY);
    if (y < oldY) startedOverScrollingY = false;
}

If this reproduces on the newest Android release, could you file a bug report. Ideally create a minimalistic application that reproduces the issue with a blank (or trivial) page.

Solution 2:

Got the exact result with following:

@OverrideprotectedvoidonScrollChanged(int x, int y, int oldX, int oldY) {
    super.onScrollChanged(x, y, oldX, oldY);

    if (y == computeVerticalScrollRange() - getHeight()) {
        // Trigger listener
    }
}

Post a Comment for "Weird Miscalculation When Trying To Detect If Webview Scrolled To Bottom"