Skip to content

Instantly share code, notes, and snippets.

@BalazsErik
Created April 29, 2020 06:08
inline fun ViewPager2.onPageSelected(lifecycleOwner: LifecycleOwner,
crossinline listener: (position: Int) -> Unit) {
object : OnPageChangeCallback() {
override fun onPageSelected(position: Int) = listener(position)
}.let {
LifecycleEventDispatcher(lifecycleOwner,
onStart = { registerOnPageChangeCallback(it) },
onStop = { unregisterOnPageChangeCallback(it) })
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment