Open marinsagovac opened 2 years ago
I ended up successfully using ChronoUnit
to keep a valid time of each slide image that should be update.
private fun scheduleTimer(period: Long) {
val handler = Handler()
val update = Runnable {
if (currentPage == imageCount) {
currentPage = 0
}
viewPager!!.setCurrentItem(currentPage++, false)
}
var then: LocalDateTime = LocalDateTime.now()
swipeTimer = Timer()
swipeTimer.schedule(object : java.util.TimerTask() {
@RequiresApi(Build.VERSION_CODES.O)
override fun run() {
if (currentPage == 0) {
currentPage++;
}
if (currentPage > 0) {
val vs = currentPage - 1;
var periodCurrentTv = tvperiod[vs]
if (ChronoUnit.SECONDS.between(then, LocalDateTime.now()) >= periodCurrentTv/1000) {
Log.d("timer20secs", (periodCurrentTv.toLong()).toString())
handler.post(update)
then = LocalDateTime.now()
};
}
}
}, 0, 100) // period
}
Result:
/timer20secs: 1000
D/timer20secs: 5000
D/timer20secs: 1000
D/timer20secs: 5000
D/timer20secs: 1000
D/timer20secs: 5000
D/timer20secs: 1000
D/timer20secs: 5000
D/timer20secs: 1000
D/timer20secs: 5000
D/timer20secs: 1000
D/timer20secs: 5000
D/timer20secs: 1000
D/timer20secs: 5000
D/timer20secs: 1000
D/timer20secs: 5000
D/timer20secs: 1000
D/timer20secs: 5000
D/timer20secs: 1000
Now, there aren't duplicate updates. If you have some suggestion to improve this other than using Thread
you can also comment here.
I managed and change code due of my requirements that each slideshow have "own" time delay, for example:
Problem is that delay overrides times and of Thread issues that I am not sure how to control and wait until next delay is finished. I also want to avoid using
Thread
function.Here is sample of issue, that two slides overrides due of
period
member method:Then I tried like this:
How can I make to
currentPage
to wait a specific delay than go to nextcurrentPage
without overrides and using schedule periods? Any other solutions for that?