Viewpager2 Extenstion Method로 처리한다.
pagePxWidth or pagePxHeight은 보통 기본값으로 처리한다.
한 화면 스크롤 할 경우 그냥 사용하면 됨.
viewpager2에서 scoll 방향이 가로 세로일 경우 나뉘어지기 때문에 orientation으로 구분해서 처리한다.
fun ViewPager2.setCurrentItem(
item: Int,
duration: Long,
interpolator: TimeInterpolator = AccelerateDecelerateInterpolator(),
pagePxWidth: Int = width, // Default value taken from getWidth() from ViewPager2 view
pagePxHeight:Int = height
) {
val pxToDrag: Int = if (orientation == ViewPager2.ORIENTATION_HORIZONTAL)
{
pagePxWidth * (item - currentItem)
}
else
{
pagePxHeight * (item - currentItem)
}
val animator = ValueAnimator.ofInt(0, pxToDrag)
var previousValue = 0
animator.addUpdateListener { valueAnimator ->
val currentValue = valueAnimator.animatedValue as Int
val currentPxToDrag = (currentValue - previousValue).toFloat()
fakeDragBy(-currentPxToDrag)
previousValue = currentValue
}
animator.addListener(object : Animator.AnimatorListener {
override fun onAnimationStart(animation: Animator?) { beginFakeDrag() }
override fun onAnimationEnd(animation: Animator?) { endFakeDrag() }
override fun onAnimationCancel(animation: Animator?) { /* Ignored */ }
override fun onAnimationRepeat(animation: Animator?) { /* Ignored */ }
})
animator.interpolator = interpolator
animator.duration = duration
animator.start()
}
포지션 & 듀레이션만 적용시켜주면됨.
setCurrentItem(ticker.currentItem+1,500)
참조링크 : stackoverflow.com/questions/57505875/change-viewpager2-scroll-speed-when-sliding-programmatically
'Development > Android' 카테고리의 다른 글
Fragment BackStack clear (0) | 2020.01.14 |
---|---|
Android Tool bar change icon (0) | 2019.07.26 |
Android Studio 에서 JSON Kotlin Class 쉽게 만들기 (0) | 2019.05.31 |
Kotlin Higher-Order Functions (0) | 2019.05.21 |
Android GPS Turn On (0) | 2019.02.21 |