Reputation: 1608
My app has a sequence of html files that i want to show to the user one by one. When the user is scrolling to the bottom of the page, i want to load the next page and when he is scrolling upwards, i want to load the previous page. For this i need to know when user is scrolling to bottommost (or upmost). So i overrided onOverScrolled()
and onScrollChanged()
, one at a time.
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt)
{
super.onScrollChanged(l, t, oldl, oldt);
int height = (int) Math.floor(this.getContentHeight() * this.getScale());
int webViewHeight = this.getMeasuredHeight();
if(this.getScrollY() + webViewHeight >= height)
{
loadNextPage();
}
}
@Override
protected void onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY)
{
super.onOverScrolled(scrollX, scrollY, clampedX, clampedY);
if(clampedY)
{
if (!(canScrollVertically(SCROLL_DOWN)))
loadNextPage();
else if (!(canScrollVertically(SCROLL_UP)))
loadPrePage();
}
}
Both codes work but the problem is sometimes the code gets called multiple times. Suppose i am in page 1, scrolling to the bottom, then instead of page 2, page 8 will show up ( the number is different, sometimes i get 2 pages forward, sometimes 5 pages ...)
How can i deal with this?
Upvotes: 1
Views: 773
Reputation: 15379
As the first line in loadNextPage
or loadPrePare
, set a flag indicating the webview is reloading. Do not process any scroll notifications while this flag is set.
When the webview finishes loading, clear the flag. You can get notified that the webview has finished loading by using the WebViewClient.onPageFinished
method.
Upvotes: 3