Skip to content

Instantly share code, notes, and snippets.

@nesterchung
Created June 6, 2018 06:21
Show Gist options
  • Save nesterchung/829c17cde428caaff47a376401f0dae2 to your computer and use it in GitHub Desktop.
Save nesterchung/829c17cde428caaff47a376401f0dae2 to your computer and use it in GitHub Desktop.
smooth scroll to center of item
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