Skip to content

Instantly share code, notes, and snippets.

@dadouf
Created December 11, 2020 10:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dadouf/9aa63e77b6db486ab6176faba3a5a463 to your computer and use it in GitHub Desktop.
Save dadouf/9aa63e77b6db486ab6176faba3a5a463 to your computer and use it in GitHub Desktop.
private fun initRecyclerViewPosition(position: Int) {
// This initial scroll will be slightly off because it doesn't
// respect the SnapHelper. Do it anyway so that the target view
// is laid out, then adjust onPreDraw.
layoutManager.scrollToPosition(position)
recyclerView.doOnPreDraw {
val targetView = layoutManager.findViewByPosition(position)
?: return@doOnPreDraw
val distanceToFinalSnap = snapHelper.calculateDistanceToFinalSnap(layoutManager, targetView)
?: return@doOnPreDraw
layoutManager.scrollToPositionWithOffset(position, -distanceToFinalSnap[0])
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment