Fast taps (clicks) on RecyclerView opens multiple Fragments

前端 未结 10 939
渐次进展
渐次进展 2021-02-02 13:37

I have implemented onClick listener to my ViewHolder for my RecyclerView

But when I perform very fast double taps or mouse clicks, it performs the task (opens a seperate

10条回答
  •  无人共我
    2021-02-02 14:09

    If you are using Kotlin you can go with this based on Money's answer

    class CodeThrottle {
        companion object {
            const val MIN_INTERVAL = 300
        }
        private var lastEventTime = System.currentTimeMillis()
    
        fun throttle(code: () -> Unit) {
            val eventTime = System.currentTimeMillis()
            if (eventTime - lastEventTime > MIN_INTERVAL) {
                lastEventTime = eventTime
                code()
            }
        }
    }
    

    Create this object in your view holder

        private val codeThrottle = CodeThrottle()
    

    Then do the following in your bind

    name.setOnClickListener { codeThrottle.throttle { listener.onCustomerClicked(customer, false) } }
    

    Putting whatever code you need called in place of

    listener.onCustomerClicked(customer, false) 
    

提交回复
热议问题