Skip to content

Commit

Permalink
PegUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
goku143u committed Jul 20, 2024
1 parent 60ddaca commit 2681ef8
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 8 deletions.
Binary file modified .gradle/file-system.probe
Binary file not shown.
25 changes: 20 additions & 5 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,32 @@ fun ViewPager2.addCarouselEffect(enableZoom: Boolean = true) {
*/

fun ViewPager2.startAutoScroll(autoScrollHandler: Handler, intervalMillis: Long = 3000) {
var isUserInteracting = false

val autoScrollRunnable = object : Runnable {
override fun run() {
val currentItem = currentItem
val nextItem = if (currentItem == adapter?.itemCount?.minus(1)) 0 else currentItem + 1
this@startAutoScroll.currentItem = nextItem
if (!isUserInteracting) {
val currentItem = currentItem
val nextItem = if (currentItem == adapter?.itemCount?.minus(1)) 0 else currentItem + 1
this@startAutoScroll.currentItem = nextItem
}
autoScrollHandler.postDelayed(this, intervalMillis)
}
}

autoScrollHandler.postDelayed(autoScrollRunnable, intervalMillis)

registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
override fun onPageScrollStateChanged(state: Int) {
super.onPageScrollStateChanged(state)
isUserInteracting = state == ViewPager2.SCROLL_STATE_DRAGGING || state == ViewPager2.SCROLL_STATE_SETTLING

when (isUserInteracting) {
true -> autoScrollHandler.removeCallbacks(autoScrollRunnable)
false -> autoScrollHandler.postDelayed(autoScrollRunnable, intervalMillis)
}
}
})
}

fun ViewPager2.stopAutoScroll(handler: Handler) {
Expand Down

0 comments on commit 2681ef8

Please sign in to comment.