disabling user input whilst the automatic animation is taking place, fixes crashes when user input is attempted at the same time

This commit is contained in:
Adam Brown 2022-01-07 15:28:05 +00:00
parent bdb41b253d
commit a0bda02824

View file

@ -27,7 +27,7 @@ fun ViewPager2.setCurrentItem(
item: Int,
duration: Long,
interpolator: TimeInterpolator = AccelerateDecelerateInterpolator(),
pagePxWidth: Int = width // Default value taken from getWidth() from ViewPager2 view
pagePxWidth: Int = width,
) {
val pxToDrag: Int = pagePxWidth * (item - currentItem)
val animator = ValueAnimator.ofInt(0, pxToDrag)
@ -47,10 +47,12 @@ fun ViewPager2.setCurrentItem(
}
animator.addListener(object : Animator.AnimatorListener {
override fun onAnimationStart(animation: Animator?) {
isUserInputEnabled = false
beginFakeDrag()
}
override fun onAnimationEnd(animation: Animator?) {
isUserInputEnabled = true
endFakeDrag()
}