Created
November 16, 2019 13:52
-
-
Save AndSky90/8792f0fe7d97b1b704506a1268e36080 to your computer and use it in GitHub Desktop.
RecyclerView smoothScroller
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
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