Skip to content

Instantly share code, notes, and snippets.

@dragoncodes
Created October 24, 2019 10:09
Show Gist options
  • Save dragoncodes/814736f0c3376a697f2419f2730930f3 to your computer and use it in GitHub Desktop.
Save dragoncodes/814736f0c3376a697f2419f2730930f3 to your computer and use it in GitHub Desktop.
suspend fun RecyclerView.awaitScrollToPositionWithOffset(position: Int, offset: Int) = suspendCoroutine<Unit> {
val layoutManager = layoutManager as? LinearLayoutManager ?: return@suspendCoroutine it.resume(Unit)
addOnScrollListener(object : RecyclerView.OnScrollListener()
{
override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int)
{
super.onScrollStateChanged(recyclerView, newState)
if (newState == RecyclerView.SCROLL_STATE_IDLE)
{
removeOnScrollListener(this)
it.resume(Unit)
}
}
})
layoutManager.scrollToPositionWithOffset(position, offset)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment