Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save petitJAM/0ac31d7ccbe03585896a58497253176b to your computer and use it in GitHub Desktop.
Save petitJAM/0ac31d7ccbe03585896a58497253176b to your computer and use it in GitHub Desktop.
/**
* The [swipeRefreshLayout] will "steal" the swipe gesture from the [nestedWebView]
* when the user tries to scroll up on the web page. This class adds a
* [ViewTreeObserver.OnScrollChangedListener] to [swipeRefreshLayout] to disable
* refreshing while [nestedWebView] is not scrolled all the way to the top.
*/
class NestedWebViewScrollingSwipeRefreshLayoutFixer(
private val swipeRefreshLayout: SwipeRefreshLayout,
private val nestedWebView: WebView)
: LifecycleObserver {
private val scrollChangedListener = ViewTreeObserver.OnScrollChangedListener {
swipeRefreshLayout.isEnabled = nestedWebView.scrollY == 0
}
@OnLifecycleEvent(Lifecycle.Event.ON_START)
fun onStart() {
swipeRefreshLayout.viewTreeObserver.addOnScrollChangedListener(scrollChangedListener)
}
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
fun onStop() {
swipeRefreshLayout.viewTreeObserver.removeOnScrollChangedListener(scrollChangedListener)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment