Skip to content

Instantly share code, notes, and snippets.

@AndSky90
Created November 16, 2019 13:52
Show Gist options
  • Save AndSky90/8792f0fe7d97b1b704506a1268e36080 to your computer and use it in GitHub Desktop.
Save AndSky90/8792f0fe7d97b1b704506a1268e36080 to your computer and use it in GitHub Desktop.
RecyclerView smoothScroller
fun RecyclerView.smoothSnapToPosition(position: Int) {
val smoothScroller = object : LinearSmoothScroller(this.context) {
override fun getVerticalSnapPreference(): Int {
return SNAP_TO_START
}
override fun getHorizontalSnapPreference(): Int {
return SNAP_TO_START
}
override fun calculateSpeedPerPixel(displayMetrics: DisplayMetrics?): Float {
return if (displayMetrics != null)
10F / displayMetrics.densityDpi
else
super.calculateSpeedPerPixel(displayMetrics)
}
}
smoothScroller.targetPosition = position
layoutManager?.startSmoothScroll(smoothScroller)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment