Skip to content

Instantly share code, notes, and snippets.

@lukelorusso
Created July 26, 2020 09:17
Show Gist options
  • Save lukelorusso/44c0449d78666543367783e142092031 to your computer and use it in GitHub Desktop.
Save lukelorusso/44c0449d78666543367783e142092031 to your computer and use it in GitHub Desktop.
Second try: somehow, this is not enough...
fun RecyclerView.bindScrollTo(vararg destinations: RecyclerView) {
object : RecyclerView.OnScrollListener() {
override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
super.onScrollStateChanged(recyclerView, newState)
}
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
// when you scroll a RecyclerView of some [dx, dy] you can do it
super.onScrolled(recyclerView, dx, dy)
// and then, ONLY if this RecyclerView is NOT idle, you can propagate that scroll
// (this is to avoid infinite multiplications of [dx, dy] when calling scrollBy)
if (recyclerView.scrollState != RecyclerView.SCROLL_STATE_IDLE)
destinations.forEach { destinationRecyclerView ->
destinationRecyclerView.scrollBy(dx, dy)
}
// somehow, this is not enough...
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment