SwipeRefreshLayout + WebView when scroll position is at top
android, swiperefreshlayout, webview
Solution
I've managed to solve it without having to extend anything. Have a look at this snippet (Fragment-specific):
private ViewTreeObserver.OnScrollChangedListener mOnScrollChangedListener;
@Override
public void onStart() {
super.onStart();
swipeLayout.getViewTreeObserver().addOnScrollChangedListener(mOnScrollChangedListener =
new ViewTreeObserver.OnScrollChangedListener() {
@Override
public void onScrollChanged() {
if (mWebView.getScrollY() == 0)
swipeLayout.setEnabled(true);
else
swipeLayout.setEnabled(false);
}
});
}
@Override
public void onStop() {
swipeLayout.getViewTreeObserver().removeOnScrollChangedListener(mOnScrollChangedListener);
super.onStop();
}
For a broader context, have a look at my answer to Android - SwipeRefreshLayout with empty textview.
Problem
I'm trying to use SwipeRefreshLayout with WebView. I'm facing the problem where in the middle of page, when user scrolls down, unwanted refresh kicks in. How do I make the refresh event only happen when webview's scroll position is at the top. (ie, he's looking at the top portion of the page)?