WebView getScrollY () always returns 0

I am trying to use the webview scroll position to determine if SwipeRefreshLayout can be updated, with the exception of some websites, for example. https://jobs.lever.co/memebox , getScrollY () always returns 0. Is there a way to ensure that I always get the correct scroll position?

+6
source share
4 answers

Your site has a fixed header. I assume the page itself does not scroll; container inside. WebView cannot check every scrollable container on the page, so it sees that the top-level container is not scrollable and assumes that all this is fixed.

If all you need is a pull-to-refresh, I would recommend adding an update button in addition to SwipeRefreshLayout .

+1
source

Maybe you can try adding this to your own webview.

just say scroll

  @Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()){ case MotionEvent.ACTION_DOWN: if(this.getScrollY() <= 0){ this.scrollTo(0,1); } break; case MotionEvent.ACTION_UP: break; } return super.onTouchEvent(event); } 

and then override onScrollChanged

  @Override protected void onScrollChanged(int l, int t, int oldl, int oldt) { super.onScrollChanged(l, t, oldl, oldt);; if (iWebViewScrollListener != null && t == 0) { iWebViewScrollListener .onTop(); } else if (mIWebViewScroll != null && t != 0) { iWebViewScrollListener .notOnTop(); } } 

add a call to the top listener while scrolling. When onTop() use setEnabled(true) for SwipeRefreshLayout, else setEnabled(false)

+2
source

Check out your web layout. position: relative; in your CSS may be the source of your problem. Relative positioning causes scrolling issues in WebView.

0
source

This is a bit outdated, but this problem still exists. For everyone who wondered, I solved this by doing the following.

In the GestureListener :

 private class GestureListener extends GestureDetector.SimpleOnGestureListener { @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { if(distanceY>0) webView.scrollBy(0,1); else webView.scrollBy(0,-1); } } 

Now you can correctly enable / disable swipeRefreshLayout using getScrollY , as shown below:

 swipeLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { ViewTreeObserver observer = swipeLayout.getViewTreeObserver(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { observer.removeOnGlobalLayoutListener(this); } else { observer.removeGlobalOnLayoutListener(this); } observer.addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() { @Override public void onScrollChanged() { if (webView.getScrollY()==0) { swipeLayout.setEnabled(true); } else { swipeLayout.setEnabled(false); } } }); } }); 
0
source

Source: https://habr.com/ru/post/983990/


All Articles