当 RecyclerView 滚动时如何忽略 onTouch 事件

问题描述

ViewHolder 中,单击 RecyclerView 项时我正在执行此缩放动画

        override fun onTouch(v: View?,event: MotionEvent?): Boolean {
            if (event != null && v != null) {
                when (event.action) {
                    MotionEvent.ACTION_DOWN -> {
                        v.scaleDown()
                    }
                    MotionEvent.ACTION_CANCEL -> {
                        v.scaleUp()
                    }
                    MotionEvent.ACTION_UP -> {
                        v.performClick()
                        v.scaleUp()
                    }
                }
                return true 
            } else {
                return false
            }
        }

        private fun View.scaleUp() {
            val scaleXAnimation = ObjectAnimator.ofFloat(this,"scaleX",scaleX,maxScale)
            val scaleYAnimation = ObjectAnimator.ofFloat(this,"scaleY",scaleY,maxScale)

            val animset = AnimatorSet().also {
                it.playTogether(scaleXAnimation,scaleYAnimation)
                it.duration = scaleAnimationDuration
                it.interpolator = LinearInterpolator()
                it.start()
            }
        }

Video

但是这个实现也允许在滚动时缩放视图。

recycler-view 滚动时,有没有办法阻止触摸事件?

或者有什么其他方式可以在点击项目时实现按比例缩小的动画?

链接

解决方法

您可以使用 RecyclerView 的滚动状态,它们是:

可以在以下位置找到有关状态的更多详细信息: https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView

状态 1:

SCROLL_STATE_DRAGGING :
The RecyclerView is currently being dragged by outside input such as user touch input.

状态 2:

SCROLL_STATE_IDLE
The RecyclerView is not currently scrolling.

状态 3:

SCROLL_STATE_SETTLING
The RecyclerView is currently animating to a final position while not under outside control.

使用这些滚动状态并基于它做你的动画。 我能想到两种方法:

方法一:

 Either perform your animation only when the Scroll State is at Idle as per State 2 

方法二:

Set focus of recyclerview to off state when the RecyclerView is being dragged as per State 1