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.
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:
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.
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