I'm trying to control the speed of the scroll within the RecyclerView. I've tried various answers on StackOverflow but none appear to be working.
From this answer: https://stackoverflow.com/a/36784136/5410011 I've implemented my own custom scroller like so:
class SpeedyLinearLayoutManager(private val context: Context, orientation: Int, reverse: Boolean) : LinearLayoutManager(context, orientation, reverse) {
private val MILLISECONDS_PER_INCH = 1000f //default is 25f (bigger = slower)
override fun smoothScrollToPosition(recyclerView: RecyclerView, state: RecyclerView.State, position: Int) {
val smoothScroller: LinearSmoothScroller = object: LinearSmoothScroller(context) {
override fun calculateSpeedPerPixel(displayMetrics: DisplayMetrics): Float {
Timber.d("Testing")
return MILLISECONDS_PER_INCH / displayMetrics.densityDpi
}
}
smoothScroller.targetPosition = position
startSmoothScroll(smoothScroller)
}
}
As you can see I've added a log of "Testing" to see if it's being called but unfortunately its not logged.
Within my fragment I use this SpeedyLinearLayoutManager
like so:
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
binding = FragmentSettingsBinding.inflate(inflater, container, false)
binding!!.rvMainOptionList.layoutManager = SpeedyLinearLayoutManager(
context!!,
LinearLayoutManager.HORIZONTAL,
false
)
val snapHelper: LinearSnapHelper = LinearSnapHelper()
snapHelper.attachToRecyclerView(binding!!.rvMainOptionList)
return binding!!.root
}
I'm trying to reduce the speed of the scroll, what I want to achieve is regardless of the direction the user scrolls (left or right) and regardless the speed of the gesture I want to just move 1 item in the RecyclerView.
Does anyone know what I'm doing wrong and how I may acheive what I want?