How to detect webview scrolling in android?

Mine is a native application in which I want to show / hide the button if the user scrolls to the end of the webview. I looked at the answer here and got the whole idea of ​​registering a callback through the interface. My main problem is that I cannot get my calculations right inside the onScrollChanged method. I tried combining things like getHeight (), getContentHeight (), top, etc., but it looks like it's too early. I tried with a simple page like google with relatively less content as well as a news page.

These logics work just fine for a normal scroll view. Since the web page contains a lot of content, this can lead to calculation errors. Insert example code: does not work.

@Override protected void onScrollChanged(int left, int top, int oldLeft, int oldTop) { if ( mOnWebViewBottomReachedListener != null ) { //if ( (getContentHeight() - (top + getHeight())) <= mMinDistance ) int diff = (getBottom() - (getHeight() + getScrollY())); Log.e("values ", diff+" o"); if ((int) Math.floor(getContentHeight() * getScaleY()) == top) mOnWebViewBottomReachedListener.onBottomReached(this); } super.onScrollChanged(left, top, oldLeft, oldTop); } 

Need help with this. Thanks.

+7
android webview
source share
4 answers
 @Override protected void onScrollChanged(int l, int t, int oldl, int oldt) { int height = (int) Math.floor(this.getContentHeight() * this.getScale()); int webViewHeight = this.getMeasuredHeight(); if(this.getScrollY() + webViewHeight >= height){ Log.i("THE END", "reached"); } super.onScrollChanged(l, t, oldl, oldt); } 

This logic works great for web browsing.

+7
source share

First, expand your webview from this class. stack overflow.squite

  <com.YourPackageName.ObservableWebView android:id="@+id/webView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" /> 

Finally, override the onScroll method as shown below:

 webView.setOnScrollChangedCallback(new OnScrollChangedCallback() { @Override public void onScroll(int l, int t) { int tek = (int) Math.floor(webView.getContentHeight() * webView.getScale()); if(tek - webView.getScrollY() == webView.getHeight()) Toast.makeText(getActivity(), "End", Toast.LENGTH_SHORT).show(); } }); 
+4
source share

After many Google searches, all the solutions found calculated the height and determined scrollOfset, but most of them are not reliable in most cases, which leads to the problems described here. Weird miscalculation when trying to determine whether the WebView scrolls to the end

I found a solution that works 100% is to define it based on overScroll

 webView.setOnOverScrolledCallback(new WebViewCustom.OnOverScrolledCallback() { @Override public void onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY) { if(clampedY) if(scrollY == 0) { //top } else { //bottom } } }); 
+2
source share

This combination of three methods works fine for me

  • Create two boolean variables two detect topReached or bottomReached.

  • use computeVerticalScrollRange () to get the vertical scroll range. computeVerticalScrollRange () will help you when the content height is less (in this case, onScrollChanged is not called).

  • using onScrollChanged to detect scroll occurs. The happing scroll means that the height of the content is greater than the measured height of the webView. Now detect the lower end using newt or detecting the upper end using newt.
 private boolean topReached = false, bottomReached = false; @Override protected int computeVerticalScrollRange() { int readerViewHeight = getMeasuredHeight(); int verticalScrollRange = super.computeVerticalScrollRange(); if (readerViewHeight >= verticalScrollRange) { topReached = true; bottomReached = true; } return verticalScrollRange; } @Override protected void onScrollChanged(int newLeft, int newTop, int oldLeft, int oldTop) { Logger.i(TAG, "onScrollChanged"); topReached = false; bottomReached = false; Log.i(TAG, "onScrollChanged newLeft : " + newLeft); Log.i(TAG, "onScrollChanged newTop : " + newTop); Log.i(TAG, "onScrollChanged oldLeft : " + oldLeft); Log.i(TAG, "onScrollChanged oldTop : " + oldTop); int readerViewHeight = getMeasuredHeight(); int contentHeight = getContentHeight(); if (newTop == 0) { Log.d(TAG, "onScrollChanged : Top End"); topReached = true; } else if (newTop + readerViewHeight >= contentHeight) { Log.d(TAG, "onScrollChanged : Bottom End"); bottomReached = true; } super.onScrollChanged(newLeft, newTop, oldLeft, oldTop); } 
0
source share

All Articles