Advertisement
truongngoclinh

AutoRotation

Jul 14th, 2020
1,613
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 0.89 KB | None | 0 0
  1. fun autoRotate(pager: ViewPager2, data: BannerRow, childCount: Int) {
  2.         timer = Timer()
  3.         timer.scheduleAtFixedRate(object : TimerTask() {
  4.             override fun run() {
  5.                 var next = pager.currentItem
  6.                 if (!data.shuffle) {
  7.                     if (next >= childCount - 1) {
  8.                         next = 0
  9.                     } else {
  10.                         next += 1
  11.                     }
  12.                 } else {
  13.                     while (true) {
  14.                         val random = Random().nextInt(childCount - 1)
  15.                         if (random != pager.currentItem) {
  16.                             next = random
  17.                             break
  18.                         }
  19.                     }
  20.                 }
  21.                 pager.currentItem = next
  22.             }
  23.         }, 3000, data.autoScrollRateInSeconds.toLong() * 1000)
  24.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement