Skip to content

Instantly share code, notes, and snippets.

@baggednismo
Last active September 15, 2020 13:38
Show Gist options
  • Save baggednismo/76888c6175630baa869bc2547290e5ec to your computer and use it in GitHub Desktop.
Save baggednismo/76888c6175630baa869bc2547290e5ec to your computer and use it in GitHub Desktop.
companion object {
private const val timerDelay: Long = 10 * 1000 // 10 seconds in milliseconds
}
override fun onCreate(savedInstanceState: Bundle?) {
...
pager.adapter = ViewPagerAdapter(updates)
...
pager.registerOnPageChangeCallback(object : OnPageChangeCallback() {
override fun onPageSelected(position: Int) {
super.onPageSelected(position)
val runnable = Runnable { pager.currentItem = position + 1 }
if (position < pager.adapter?.itemCount ?: 0) {
handler.postDelayed(runnable, timerDelay)
}
}
override fun onPageScrollStateChanged(state: Int) {
super.onPageScrollStateChanged(state)
/**
* The user swiped forward or back and we need to
* invalidate the previous handler.
*/
if (state == SCROLL_STATE_DRAGGING) handler.removeMessages(0)
}
})
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment