Created
December 11, 2020 10:30
-
-
Save dadouf/9aa63e77b6db486ab6176faba3a5a463 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
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