Created
May 30, 2018 22:53
-
-
Save petitJAM/0ac31d7ccbe03585896a58497253176b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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