From 5158b998003254fadfeefe631874d76eb84df5ab Mon Sep 17 00:00:00 2001 From: meyou <2636699780@qq.com> Date: Mon, 25 May 2026 22:57:18 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96=E5=B9=B4=E6=9C=88?= =?UTF-8?q?=E5=88=87=E6=8D=A2=E5=8D=A1=E9=A1=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Perfetto trace 分析: - YearGridView 首帧 168ms: 12 个 MiniMonth 各创建 sharedElement 节点,但仅 1 个 key 匹配 CalendarPager - compose:lazy:prefetch 最长 703ms: CalendarPager 预加载相邻页 修复: - YearGridView: 仅选中的月份使用 sharedElement(11→1 个), 减少首次组合的 modifier 节点创建开销 - CalendarPager: beyondViewportPageCount 1→0,消除预加载卡顿 --- .../plus/rua/project/ui/CalendarPager.kt | 2 +- .../plus/rua/project/ui/YearGridView.kt | 24 ++++++++++++------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/core/src/main/kotlin/plus/rua/project/ui/CalendarPager.kt b/core/src/main/kotlin/plus/rua/project/ui/CalendarPager.kt index 73b8a1b..fffa161 100644 --- a/core/src/main/kotlin/plus/rua/project/ui/CalendarPager.kt +++ b/core/src/main/kotlin/plus/rua/project/ui/CalendarPager.kt @@ -77,7 +77,7 @@ fun CalendarPager( HorizontalPager( state = pagerState, - beyondViewportPageCount = 1, + beyondViewportPageCount = 0, flingBehavior = PagerDefaults.flingBehavior(state = pagerState), modifier = modifier ) { page -> diff --git a/core/src/main/kotlin/plus/rua/project/ui/YearGridView.kt b/core/src/main/kotlin/plus/rua/project/ui/YearGridView.kt index 6042dae..6abbcd8 100644 --- a/core/src/main/kotlin/plus/rua/project/ui/YearGridView.kt +++ b/core/src/main/kotlin/plus/rua/project/ui/YearGridView.kt @@ -167,12 +167,12 @@ fun YearGridView( ) { (0 until 3).forEach { col -> val month = row * 3 + col + 1 - val sharedKey = "month_grid_${year}_$month" + val isSelectedMonth = month == selectedMonth with(sharedTransitionScope) { MiniMonth( year = year, month = month, - isSelected = month == selectedMonth, + isSelected = isSelectedMonth, today = today, days = monthDays[month - 1], colors = colors, @@ -182,13 +182,19 @@ fun YearGridView( onClick = { onMonthClick(month) }, modifier = Modifier .weight(1f) - .sharedElement( - sharedContentState = rememberSharedContentState( - key = sharedKey - ), - animatedVisibilityScope = animatedVisibilityScope, - boundsTransform = { _, _ -> - tween(400, easing = FastOutSlowInEasing) + .then( + if (isSelectedMonth) { + Modifier.sharedElement( + sharedContentState = rememberSharedContentState( + key = "month_grid_${year}_$month" + ), + animatedVisibilityScope = animatedVisibilityScope, + boundsTransform = { _, _ -> + tween(400, easing = FastOutSlowInEasing) + } + ) + } else { + Modifier } ) )