Created
June 6, 2018 06:21
-
-
Save nesterchung/829c17cde428caaff47a376401f0dae2 to your computer and use it in GitHub Desktop.
smooth scroll to center of item
This file contains hidden or 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
class CenterSnappedSmoothScroller(context: Context) : LinearSmoothScroller(context) { | |
override fun calculateDtToFit(viewStart: Int, viewEnd: Int, boxStart: Int, boxEnd: Int, snapPreference: Int): Int { | |
return center(super.calculateDtToFit(viewStart, viewEnd, boxStart, boxEnd, snapPreference), viewEnd - viewStart) | |
} | |
private fun center(dx: Int, viewWidth: Int): Int { | |
return if (dx < 0) { | |
dx + (viewWidth / 2) | |
} else { | |
dx - (viewWidth / 2) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment