LoginSignup
4
2

More than 5 years have passed since last update.

【Android】smoothScrollToPositionの移動速度を変えたい

Last updated at Posted at 2018-02-02

アイテムを移動させる時に移動速度も変えたい

SpeedyGridLayoutManager.kt
class SpeedyGridLayoutManager : GridLayoutManager {
    var MILLIS = 155f

    constructor(context: Context, spanCount: Int, orientation: Int, reverseLayout: Boolean) : super(context, spanCount, orientation, reverseLayout) {}

    override fun smoothScrollToPosition(recyclerView: RecyclerView, state: RecyclerView.State?, position: Int) {

        val linearSmoothScroller = object: LinearSmoothScroller(recyclerView.context) {

            override fun computeScrollVectorForPosition(targetPosition: Int): PointF? {
                return this@SpeedyGridLayoutManager.computeScrollVectorForPosition(targetPosition)
            }

            override fun calculateSpeedPerPixel(displayMetrics: DisplayMetrics): Float {
                return MILLIS / displayMetrics.densityDpi
            }
        }

        linearSmoothScroller.targetPosition = position
        startSmoothScroll(linearSmoothScroller)
    }
}

MILLSを変えれば移動速度も変わります。
あとは、#setLayoutManagerしてあげれば使えます。

4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2