Skip to content

Instantly share code, notes, and snippets.

@sugoi-wada
Created January 18, 2017 10:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sugoi-wada/b7a28573d019aff0c50422dad27e4a1d to your computer and use it in GitHub Desktop.
Save sugoi-wada/b7a28573d019aff0c50422dad27e4a1d to your computer and use it in GitHub Desktop.
import android.content.Context
import android.graphics.PointF
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.LinearSmoothScroller
import android.support.v7.widget.RecyclerView
class SmoothScrollableLinearLayoutManager @JvmOverloads constructor(context: Context, orientation: Int = LinearLayoutManager.VERTICAL, reverseLayout: Boolean = false) : LinearLayoutManager(context, orientation, reverseLayout) {
override fun smoothScrollToPosition(recyclerView: RecyclerView, state: RecyclerView.State, position: Int) {
startSmoothScroll(TopSnappedSmoothScroller(recyclerView.context).apply {
targetPosition = position
})
}
private inner class TopSnappedSmoothScroller(context: Context) : LinearSmoothScroller(context) {
override fun computeScrollVectorForPosition(targetPosition: Int): PointF {
return this@SmoothScrollableLinearLayoutManager.computeScrollVectorForPosition(targetPosition)
}
override fun getVerticalSnapPreference(): Int = SNAP_TO_START
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment