Reputation: 4532
I have a NestedScrollView
that I'm observing its scroll events using the following:
val scrollListener = NestedScrollView.OnScrollChangeListener { _, _, scrollY, _, _ ->
Log.d(TAG, "scrollListener: Scrolled! $scrollY")
setNavBarVisibilityFor(offset = scrollY)
}
scrollView.setOnScrollChangeListener(scrollListener)
However, depending on an asynchronous event, I may, or may not, need to call another method in that scroll listener. In 80% of the cases I won't be needing the extra call, so I don't want to add a bool to check every time the scroll listener gets fired, and decide on the method call based on that.
I'm trying to remove the attached scroll listener from my NestedScrollView
and create and attach a new one:
val extraScrollListener = NestedScrollView.OnScrollChangeListener { _, _, scrollY, _, _ ->
Log.d(TAG, "extraScrollListener: Scrolled! $scrollY")
setNavBarVisibilityFor(offset = scrollY)
anotherMethodThatNeedsToBeRun(offset = scrollY)
}
//TODO: Remove the original scrollListener from the scrollView
scrollView.setOnScrollChangeListener(extraScrollListener)
How can I remove the original NestedScrollView.OnScrollChangeListener
from my scrollView before attaching the new one?
Upvotes: 3
Views: 1698
Reputation: 61
In Kotlin, you will get ambiguity error. So, in that case you need to use this line of code:
mScrollView.setOnScrollChangeListener(null as NestedScrollView.OnScrollChangeListener?)
Upvotes: 4