From 9dfc56210e00c5d17ec22716b67585c340998efd Mon Sep 17 00:00:00 2001 From: HapeLee <1321903405@qq.com> Date: Fri, 6 Mar 2026 01:14:46 +0800 Subject: [PATCH] =?UTF-8?q?[=E4=BC=98=E5=8C=96]=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E7=9B=AE=E5=BD=95=E5=88=B7=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/legado/app/ui/book/toc/TocScreen.kt | 21 +++++-------------- 1 file changed, 5 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/book/toc/TocScreen.kt b/app/src/main/java/io/legado/app/ui/book/toc/TocScreen.kt index ed6ea8fbd..18f06a54e 100644 --- a/app/src/main/java/io/legado/app/ui/book/toc/TocScreen.kt +++ b/app/src/main/java/io/legado/app/ui/book/toc/TocScreen.kt @@ -110,8 +110,6 @@ import io.legado.app.ui.widget.components.lazylist.FastScrollLazyColumn import io.legado.app.ui.widget.components.menuItem.RoundDropdownMenu import io.legado.app.ui.widget.components.menuItem.RoundDropdownMenuItem import io.legado.app.ui.widget.components.topbar.DynamicTopAppBar -import kotlinx.coroutines.flow.filter -import kotlinx.coroutines.flow.first import kotlinx.coroutines.launch import org.koin.androidx.compose.koinViewModel import java.text.SimpleDateFormat @@ -231,22 +229,13 @@ fun TocScreen( var hasAutoScrolled by rememberSaveable { mutableStateOf(false) } - LaunchedEffect(Unit) { - if (!hasAutoScrolled) { - // 等待列表包含当前阅读章节后自动定位,避免在无 isDur 项时提前退出 - // 使用 animateScrollToItem 以便用户看到滚动动画,避免误操作 - val items = snapshotFlow { state.items } - .filter { list -> list.any { it.isDur } } - .first() - val targetIndex = items.indexOfFirst { it.isDur } + LaunchedEffect(state.items) { + if (!hasAutoScrolled && state.items.isNotEmpty()) { + val targetIndex = state.items.indexOfFirst { it.isDur } if (targetIndex != -1) { - // 等待视口尺寸测量完成,确保 offset 计算正确 - snapshotFlow { listState.layoutInfo.viewportEndOffset } - .filter { it > 0 } - .first() - listState.animateScrollToItem( + listState.scrollToItem( index = targetIndex, - scrollOffset = -(listState.layoutInfo.viewportEndOffset / 4) + scrollOffset = -offset ) hasAutoScrolled = true }