4

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.onScrollChanged(x, y, oldX, oldY);

    int bottom = ((int) Math.floor(getContentHeight() * getScale()) - getHeight());
    MyLogger.log(y + "____" + bottom);

    if (y == bottom) {
        // Trigger listener
    }
}

Logging gives me following output:

162____164

So somehow there is 2 pixel miscalculation here and I have no idea where is that coming from.

Thanks.

Niko
  • 8,093
  • 5
  • 49
  • 85

2 Answers2

12

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

@Override
protected void onScrollChanged(int x, int y, int oldX, int oldY) {
    super.onScrollChanged(x, y, oldX, oldY);
    final int DIRECTION_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:

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

@Override
protected void onScrollChanged(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.

marcin.kosiba
  • 3,221
  • 14
  • 19
  • Good answer, although I had to change the formula a bit in onScrollChanged – Niko Dec 12 '13 at 05:46
  • @Niko - thanks for pointing that out! I updated the answer to use canScrollVertically which I think is even better since that's a public method. – marcin.kosiba Dec 12 '13 at 11:10
  • Yep that is nice if you are developing for API level 14 or higher :-) – Niko Dec 12 '13 at 11:18
  • Niko - the support library provides this method (http://developer.android.com/reference/android/support/v4/view/ViewCompat.html) and worst case you could just copy the canScrollVertically implementation over from View.java (https://github.com/android/platform_frameworks_base/blob/master/core/java/android/view/View.java) – marcin.kosiba Dec 12 '13 at 11:46
  • 1
    Perfect answer. There are so many other solutions, with scale and floor calculations, but this first one with usage of `canScrollVertically` method is the only one working for me – diesersamat Feb 25 '20 at 10:16
0

Got the exact result with following:

@Override
protected void onScrollChanged(int x, int y, int oldX, int oldY) {
    super.onScrollChanged(x, y, oldX, oldY);

    if (y == computeVerticalScrollRange() - getHeight()) {
        // Trigger listener
    }
}
Niko
  • 8,093
  • 5
  • 49
  • 85