Share
Embed
Transcript
60 slides
-
パターン2: Material3のTopAppBar方式 先に紹介したMaterial3のTopAppBar方式 LazyColumnは全画面表示で、ツールバーをその上に被せる ツールバー開閉は、TopAppBar自体の高さとLazyColumnのcontentPaddingを変更して 実現 Scaffold( ... content =
{ innerPadding -> LazyColumn( contentPadding = innerPadding, ... 22 -
パターン3: LazyColumnごとスクロール Collapsing Toolbar in Jetpack Compose | Problem, solutions
and alternatives で紹介さ れているアイデア LazyColumnは全画面表示で、ツールバーをその上に被せる ツールバーを開いている状態では、LazyColumnにy方向オフセットを指定して下に移動 しておく ツールバー開閉は、LazyColumnへのスクロール操作に対して、ツールバー内部と LazyColumnのy方向オフセットなどを変更して実現 24 -
3つのパターンの比較 LazyColumnに ContentPadding(top) Material3の TopAppBar LazyColumnごとス クロール 理解しやすさ 普通 簡単
少し難しい 実装難易度 普通 簡単 少し難しい recomposition 少ない 多い 少ない scrollToItem() 問題あり 問題なし 問題なし 27 -
使用例 // TopAppBar + LazyColumn の(ほぼ同じ)引数に対応 LazyColumnWithToolbar( title = {
}, navigationIcon = { }, actions = { }, ) { items(100) { index -> Text( text = "I'm item $index", style = MaterialTheme.typography.h3, modifier = Modifier .fillMaxWidth() .padding(16.dp), ) } } 33 -
@Composable fun LazyColumnWithToolbar( ... ) { // これの中身が重要 ToolbarWithScrollableContent( modifier
= modifier, toolbarState = toolbarState, title = title, navigationIcon = navigationIcon, actions = actions, stickyContent = stickyContent, isReachedTop = { listState.isReachedTop() }, ) { LazyColumn(...) } } 34 -
@Composable fun ToolbarWithScrollableContent(...) { ... Box(modifier = modifier .fillMaxSize() .nestedScroll(nestedScrollConnection))
{ CollapsingToolbar(...) // ツールバー部分 Column(...) { // ツールバーの開閉に応じて上下に移動 // ツールバー下部に常に表示するコンテンツ stickyContent?.invoke() // LazyColumn content() } } } 35 -
@Composable fun CollapsingToolbar( ... ) { ... CompositionLocalProvider( LocalContentColor provides
contentColor, ) { Box(modifier = modifier.height(toolbarMaxHeight)) { // 背景を表示、開閉状態に応じて高さを変更 Background(...) // navigationIcon とactions ToolbarWithoutTitle(...) // タイトルを表示、開閉状態に応じて位置を移動 ToolbarTitle(...) } } } 36 -
class ToolbarNestedScrollConnection(...) : NestedScrollConnection { // ユーザーがースクロール操作した場合、 // LazyColumn がコンテンツを実際にスクロールする前に呼ばれる
override fun onPreScroll(...): Offset { // LazyColumn のスクロール位置が0 なら、 // ツールバーの開閉にスクロール量を消費する } ... // ユーザーがフリンジ操作した場合、 // LazyColumn がフリンジ操作をハンドリングした後に呼ばれる override suspend fun onPostFling(...): Velocity { // LazyColumn のスクロール位置が0 かつツールバーがまだ開けるなら、 // ツールバーを開く } } 41 -
@Stable class CollapsingToolbarState(...) { private val maxOffset: Float = (maxHeight
- minHeight).toFloat() private var _offset: Float by mutableStateOf(offset.coerceIn(0f, maxOffset)) private var _collapseFraction: Float by mutableStateOf(calcCollapseFraction()) // how much the toolbar extends from its collapsed state val offset: Float get() = _offset // fraction of collapse progress, 1 -> fully collapsed, 0 -> fully expanded val collapseFraction: Float get() = _collapseFraction var navigationIconWidth by mutableStateOf(0) var actionsWidth by mutableStateOf(0) fun consumeScrollOffset(available: Offset): Offset { ... } } 45 -
値を読み出すのを後回しにする Defer reads as long as possible としてGoogle公式ドキュメントにも掲載されている 値を読み出すのを後回しにすることで、recompositionの範囲を狭められる (理解が合っているか不安ですが)値を読み出すと、読み出した箇所と同じ階層の
Composableラムダはrecompositionが起きてしまう 46 -
改善前 @Composable ToolbarWithScrollableContent( ... isReachedTop = listState.isReachedTop(), ) { ...
} 改善後:ラムダで包む @Composable ToolbarWithScrollableContent( ... isReachedTop = { listState.isReachedTop() }, ) { ... } 47 -
改善前 @Composable Background( ... toolbarStateHeight = toolbarState.minHeight + toolbarState.offset, )
改善後:@StableなStateクラスを渡す @Composable Background( ... toolbarState = toolbarState, ) 48 -
改善前 @Composable private fun Background( modifier: Modifier = Modifier, backgroundColor:
Color, toolbarState: CollapsingToolbarState, ) { val toolbarHeight = with(LocalDensity.current) { (toolbarState.minHeight + toolbarState.offset).toDp() } Box( modifier = modifier .background(backgroundColor) .fillMaxWidth() .height(toolbarHeight) ) } 50 -
改善版 @Composable private fun Background( modifier: Modifier = Modifier, backgroundColor:
Color, toolbarState: CollapsingToolbarState, ) { Box( modifier = modifier .fillMaxSize() .drawWithCache { onDrawBehind { drawRect( color = backgroundColor, size = Size( width = size.width, height = toolbarState.minHeight + toolbarState.offset, ), ) } }, ) } 51 -
ラムダ版のModifierを使う Defer reads as long as possible の中で触れられている Modifier が変わる
= recomposition が発生する なので、可能な限りラムダバージョン を使用する 52 -
改善前 Row( modifier = modifier .height(toolbarMinHeight) .graphicsLayer( translationX = toolbarState.navigationIconWidth
* toolbarState.collapseFraction, translationY = toolbarState.offset, ), verticalAlignment = Alignment.CenterVertically, ) {...} 53 -
改善後 Row( modifier = modifier .height(toolbarMinHeight) .graphicsLayer { translationX =
toolbarState.navigationIconWidth * toolbarState.collapseFraction translationY = toolbarState.offset }, verticalAlignment = Alignment.CenterVertically, ) {...} 54 -
derivedStateOfを使う Use derivedStateOf to limit recompositions としてGoogle公式ドキュメントにも掲載さ れている ある状態(例:Int)から派生した状態(例:Boolean)をComposable内の条件分岐な どに使用する場合、
derivedStateOf で無駄なrecompositionを防げる 55 -
改善前 Row( ... ) { Layout(content = title) { measurables,
constraints -> // cause a lot of recomposition!! val maxWidth = if (toolbarState.offset < toolbarState.minHeight) { constraints.maxWidth - toolbarState.navigationIconWidth - toolbarState.actionsWidth } else { constraints.maxWidth } ... } } 56 -
改善後 val almostCollapsed: Boolean by remember { derivedStateOf { toolbarState.offset
< toolbarState.minHeight } } Row( ... ) { Layout(content = title) { measurables, constraints -> val maxWidth = if (almostCollapsed) { constraints.maxWidth - toolbarState.navigationIconWidth - toolbarState.actionsWidth } else { constraints.maxWidth } ... } } 57