我有一个带有 Column 应用栏和一个可从 md3 组合的 SearchBar 的脚手架,我的脚手架的内容是具有垂直滚动行为的 Column:
val appBarState = rememberTopAppBarState()
val scrollBehavior = TopAppBarDefaults.enterAlwaysScrollBehavior(appBarState)
Scaffold(
modifier = Modifier.nestedScroll(scrollBehavior.nestedScrollConnection),
snackbarHost = {
SnackbarHost(hostState = snackbarHostState)
},
topBar = {
Column {
AnimatedVisibility(visible = !isSearchActive) {
ActionToolbar(
scrollBehavior = scrollBehavior,
title = -1,
titleComposable = {
Text(
text = "Browsing in",
style = MaterialTheme.typography.labelLarge
)
},
modifier = Modifier.toolbarActions(),
primaryActionVector = Icons.Outlined.Notifications,
primaryAction = {
},
secondaryActionVector = Icons.Outlined.BookmarkBorder,
secondaryAction = {},
navigationIconAction = null,
)
}
EmbeddedSearchBar(
onQueryChange = {
queryText = it
},
onSearch = {},
isSearchActive = isSearchActive,
onActiveChanged = { isSearchActive = it },
)
UiStateComposable(
modifier = modifier,
snackbarHostState = snackbarHostState,
uiState = featuredArenasState,
useLinearProgressBar = true
) {
}
}
},
content = { padding ->
ResourcesScreenContent(
modifier = Modifier.padding(padding),
featuredArenas = featuredArenasState.dataModel ?: emptyList(),
onErrorMessageShown = {
viewModel.clearErrorState()
}, onGetResources = {
viewModel.fetchMenu()
}
)
}
)
我想要实现的是使整个内容可滚动(带有顶部应用栏),同时保持 SearchBar 单击它时的行为(即完全扩展到屏幕)。
使用上面的代码,我设法通过传递 nestedScrollConnection 行为修饰符来滚动顶部应用程序栏,但 SearchBar 固定在顶部(因为它不接受滚动行为)
如果我将 SearchBar 移动到脚手架内容 (
ResourcesScreenContent()
)(这是一个可滚动列),以便实现所需的行为,则当 SearchBar 由于限制而尝试展开时,它会在单击它时崩溃:
java.lang.IllegalArgumentException: Can't represent a size of 671460339 in Constraints
我该如何解决这个问题?
唯一可以解决的问题是使用
NestedScrollConnection
s 手动实现滚动行为
请参阅 Levi Albuquerque 的这篇文章:
https://medium.com/androiddevelopers/understanding-nested-scrolling-in-jetpack-compose-eb57c1ea0af0