perf: 优化年月切换卡顿

Perfetto trace 分析:
- YearGridView 首帧 168ms: 12 个 MiniMonth 各创建 sharedElement
  节点,但仅 1 个 key 匹配 CalendarPager
- compose:lazy:prefetch 最长 703ms: CalendarPager 预加载相邻页

修复:
- YearGridView: 仅选中的月份使用 sharedElement(11→1 个),
  减少首次组合的 modifier 节点创建开销
- CalendarPager: beyondViewportPageCount 1→0,消除预加载卡顿
This commit is contained in:
meyou 2026-05-25 22:57:18 +08:00
parent 80ab328906
commit 5158b99800
No known key found for this signature in database
2 changed files with 16 additions and 10 deletions

View File

@ -77,7 +77,7 @@ fun CalendarPager(
HorizontalPager(
state = pagerState,
beyondViewportPageCount = 1,
beyondViewportPageCount = 0,
flingBehavior = PagerDefaults.flingBehavior(state = pagerState),
modifier = modifier
) { page ->

View File

@ -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
}
)
)