package com.cheng.blzb.widget

import android.content.Context
import android.util.DisplayMetrics
import androidx.recyclerview.widget.LinearSmoothScroller


class LinearTopSmoothScroller(val context: Context, needFast: Boolean) : LinearSmoothScroller(context) {
    /**
     * speed 值越大滚动越慢
     */
    private var speed = 0.03f

    /**
     * @param context  context
     * @param needFast 是否需要快速滑动
     */
    init {
        if (needFast) {
            setScrollFast()
        } else {
            setScrollSlowly()
        }
    }

    override fun getVerticalSnapPreference(): Int {
        return SNAP_TO_START
    }

    override fun calculateSpeedPerPixel(displayMetrics: DisplayMetrics): Float {
        return speed / displayMetrics.density
    }

    /**
     * 缓慢滚动
     */
    fun setScrollSlowly() {
        try {
            // 建议不同分辨率设备上的滑动速度相同
            speed = context.resources.displayMetrics.density * 0.3f
        } catch (e: Exception) {
            // do nothing
        }
    }

    /**
     * 快速滚动
     */
    fun setScrollFast() {
        try {
            speed = context.resources.displayMetrics.density * 0.03f
        } catch (e: Exception) {
            // do nothing
        }
    }
}
