This commit is contained in:
Horis
2025-03-21 17:37:23 +08:00
parent 03d05f5df5
commit b9742e7c2a
3 changed files with 26 additions and 28 deletions

View File

@@ -119,7 +119,7 @@ class ReadMangaActivity : VMBaseActivity<ActivityMangaBinding, ReadMangaViewMode
private var justInitData: Boolean = false
private var syncDialog: AlertDialog? = null
private val mScrollTimer by lazy {
ScrollTimer(lifecycleScope, this).apply {
ScrollTimer(this, binding.mRecyclerManga).apply {
setSpeed(AppConfig.mangaAutoPageSpeed)
}
}

View File

@@ -1,46 +1,44 @@
package io.legado.app.ui.book.manga.recyclerview
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.SCROLL_STATE_IDLE
class ScrollTimer(
private val coroutineScope: CoroutineScope,
private val callback: ScrollCallback
) {
private var job: Job? = null
private var delayMs: Long = 20L
private val callback: ScrollCallback,
private val recyclerView: RecyclerView
) : RecyclerView.OnScrollListener() {
private var distance = 1
var isEnabled: Boolean = false
set(value) {
if (field != value) {
field = value
restartJob()
if (value) {
recyclerView.addOnScrollListener(this)
startScroll()
} else {
recyclerView.removeOnScrollListener(this)
}
}
}
override fun onScrollStateChanged(
recyclerView: RecyclerView,
newState: Int
) {
if (newState == SCROLL_STATE_IDLE) {
startScroll()
}
}
fun setSpeed(distance: Int) {
this.distance = distance
restartJob()
}
private fun restartJob() {
job?.cancel()
if (!isEnabled || delayMs == 0L) {
job = null
return
}
job = coroutineScope.launch {
while (isActive) {
callback.scrollBy(distance)
delay(delayMs)
}
}
private fun startScroll() {
callback.scrollBy(distance)
}
interface ScrollCallback{
fun scrollBy(distance:Int)
interface ScrollCallback {
fun scrollBy(distance: Int)
}
}

View File

@@ -233,7 +233,7 @@ class MangaMenu @JvmOverloads constructor(
interface CallBack {
fun openBookInfoActivity()
fun upSystemUiVisibility(menuIsVisible: Boolean)
fun skipToPage(pos: Int)
fun skipToPage(index: Int)
}
}