Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebView getScrollY() always returns 0

Im trying to use the webview's scroll position to determine whether SwipeRefreshLayout should be able to refresh, except for some websites e.g. https://jobs.lever.co/memebox, getScrollY() always returns 0. Is there a way to ensure I will always get the correct scroll position?

like image 810
Tim Mutton Avatar asked Mar 18 '15 22:03

Tim Mutton


2 Answers

Maybe you can try to add this to your custom webview

just tell it's scrolling

 @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 an top listener call back when scrolling.When is onTop() use setEnabled(true) for SwipeRefreshLayout,else setEnabled(false)

like image 50
Kevin Wu Avatar answered Oct 06 '22 01:10

Kevin Wu


The site you linked has a fixed header. My guess is that the page itself doesn't scroll; a container inside it does. The WebView can't inspect every scrollable container on the page, so it sees that the top-level container doesn't scroll and assumes that the entire thing is fixed.

If all you need this for is pull-to-refresh, I'd recommend adding a refresh button in addition to the SwipeRefreshLayout.

like image 23
Jarett Millard Avatar answered Oct 06 '22 01:10

Jarett Millard