我还没有用 RecyclerView 完成它,但我设法在 ViewPager 上做了类似的事情。基本上,您可以使用带有 CycleInterpolator 的 ValueAnimator 将 RecyclerView 每帧滚动一点点然后返回。由于它的滚动位置随着每一帧而更新,我认为这不应该与捕捉发生冲突:
val startX = recyclerView.getScrollX().toFloat()
val endX = startX + Utils.dpToPx(context, 100) // the amount you want to scroll in pixels
val animator = ValueAnimator.ofFloat(1f, 0f)
animator.cancel()
animator.interpolator = CycleInterpolator(0.5f)
animator.startDelay = 600
animator.duration = 600
animator.addUpdateListener { animation ->
val alpha = animation.animatedValue as Float
recyclerView.scrollTo((startX * alpha + (1f - alpha) * endX).toInt(), 0)
}
animator.start()
这适用于 ViewPager,也应该适用于 RecyclerView,因为它具有相同的滚动方法可用......