Created
October 24, 2019 10:09
-
-
Save dragoncodes/814736f0c3376a697f2419f2730930f3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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