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:
parent
80ab328906
commit
5158b99800
@ -77,7 +77,7 @@ fun CalendarPager(
|
|||||||
|
|
||||||
HorizontalPager(
|
HorizontalPager(
|
||||||
state = pagerState,
|
state = pagerState,
|
||||||
beyondViewportPageCount = 1,
|
beyondViewportPageCount = 0,
|
||||||
flingBehavior = PagerDefaults.flingBehavior(state = pagerState),
|
flingBehavior = PagerDefaults.flingBehavior(state = pagerState),
|
||||||
modifier = modifier
|
modifier = modifier
|
||||||
) { page ->
|
) { page ->
|
||||||
|
|||||||
@ -167,12 +167,12 @@ fun YearGridView(
|
|||||||
) {
|
) {
|
||||||
(0 until 3).forEach { col ->
|
(0 until 3).forEach { col ->
|
||||||
val month = row * 3 + col + 1
|
val month = row * 3 + col + 1
|
||||||
val sharedKey = "month_grid_${year}_$month"
|
val isSelectedMonth = month == selectedMonth
|
||||||
with(sharedTransitionScope) {
|
with(sharedTransitionScope) {
|
||||||
MiniMonth(
|
MiniMonth(
|
||||||
year = year,
|
year = year,
|
||||||
month = month,
|
month = month,
|
||||||
isSelected = month == selectedMonth,
|
isSelected = isSelectedMonth,
|
||||||
today = today,
|
today = today,
|
||||||
days = monthDays[month - 1],
|
days = monthDays[month - 1],
|
||||||
colors = colors,
|
colors = colors,
|
||||||
@ -182,13 +182,19 @@ fun YearGridView(
|
|||||||
onClick = { onMonthClick(month) },
|
onClick = { onMonthClick(month) },
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.weight(1f)
|
.weight(1f)
|
||||||
.sharedElement(
|
.then(
|
||||||
sharedContentState = rememberSharedContentState(
|
if (isSelectedMonth) {
|
||||||
key = sharedKey
|
Modifier.sharedElement(
|
||||||
),
|
sharedContentState = rememberSharedContentState(
|
||||||
animatedVisibilityScope = animatedVisibilityScope,
|
key = "month_grid_${year}_$month"
|
||||||
boundsTransform = { _, _ ->
|
),
|
||||||
tween(400, easing = FastOutSlowInEasing)
|
animatedVisibilityScope = animatedVisibilityScope,
|
||||||
|
boundsTransform = { _, _ ->
|
||||||
|
tween(400, easing = FastOutSlowInEasing)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
Modifier
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user