0

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?

KTOV
  • 559
  • 3
  • 14
  • 39
  • I wonder if the _LinearSnapHelper_ is interfering. Maybe try it without the snap helper to see if your scroller is used. – Cheticamp Aug 29 '22 at 17:14
  • @Cheticamp Yeah I thought that too, but regardless with or without the LinearSnapHelper the friggin log isn't logged :( – KTOV Aug 29 '22 at 23:13
  • So, are you explicitly calling `smoothScrollToPosition()` or are you expecting this code to be executed when the user scrolls? 2nd question: Do you want to disable flings and force the maximum right/left scroll motion to be one item? – Cheticamp Aug 30 '22 at 00:25
  • @Cheticamp Yeah so I'm expecting that method to be called when the user scrolls. And yeah, I'm wanting to disable the flings and force the max and right and left scroll to only motion by one item – KTOV Aug 30 '22 at 08:54
  • I don't believe that `smoothScrollToPosition()` is used internally by the layout manager or _RecyclerView_. (No proof, just a cursory check.) You may be able to use just the snap helper. Take a look [here](https://stackoverflow.com/a/39036351/6287910). – Cheticamp Aug 30 '22 at 11:01
  • It's an interesting approach, but I suppose it doesn't handle whilst the user is scrolling. It sounds like I need to disable scrolling on RecyclerView and handle gestures. If the user swipes right or left then smooth scroll in that direction by 1 item? @Cheticamp – KTOV Aug 30 '22 at 11:17
  • I would give it a try since it's simple enough to do. – Cheticamp Aug 30 '22 at 11:21
  • @Cheticamp I've given what you suggested a try. It appears it only happens when scrolling is settled by the user. I think what I suggested may be the better thing to do, need to figure out how to do that now lol – KTOV Aug 30 '22 at 11:26

0 Answers0