Skip to content

Instantly share code, notes, and snippets.

@BalazsErik
Created April 29, 2020 06:08
Show Gist options
  • Save BalazsErik/6bcd2019befe42b2df028caef94124c8 to your computer and use it in GitHub Desktop.
Save BalazsErik/6bcd2019befe42b2df028caef94124c8 to your computer and use it in GitHub Desktop.
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