我正在努力使用 jetpack compose LazyColumn 和 StickyHeader 功能。基本上静态视图效果很好,但是一旦我开始滚动,这些项目就会越过粘性标题,滚动会开始一种奇怪的行为,并且最后一个项目将永远不可见,因为滚动总是反弹回来。
它看起来是这样的:
这是可组合的:
@OptIn(ExperimentalFoundationApi::class)
@Composable
fun CollectionsScreen(
collectionsLive: LiveData<List<CollectionsView>>,
onCollectionChanged: (ICalCollection) -> Unit
/* some more hoisted functions left out for simplicity */
) {
val list by collectionsLive.observeAsState(emptyList())
val grouped = list.groupBy { it.accountName ?: it.accountType ?: "Account" }
LazyColumn(
modifier = Modifier.padding(8.dp)
) {
item {
Text(
stringResource(id = R.string.collections_info),
textAlign = TextAlign.Center,
modifier = Modifier.padding(bottom = 16.dp)
)
}
grouped.forEach { (account, collectionsInAccount) ->
stickyHeader {
Text(
account,
style = MaterialTheme.typography.titleLarge, …Run Code Online (Sandbox Code Playgroud) 我正在尝试将应用程序从 XML 布局迁移到 jetpack compose。由于该应用程序允许设置不同的用户主题,因此我想使用 XML 主题,直到所有文件都迁移完毕。但是,我就是无法让它发挥作用。
我有一个带有定义的可组合项的简单活动:
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import at.example.myapp.ui.composables.about.About
import com.google.android.material.composethemeadapter3.Mdc3Theme
class AboutActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
setTheme(MainActivity.getUserTheme(this))
super.onCreate(savedInstanceState)
setContent {
// Use MdcTheme instead of MaterialTheme
// Colors, typography, and shape have been read from the
// View-based theme used in this Activity
Mdc3Theme {
About()
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
setTheme(...) 正确加载主题,最顶部的颜色被正确设置,但没有其他。所有其他颜色、样式和字体均未在可组合项中设置。About() 可组合项本身没有什么特别的,只有一些文本、图像和一张卡片。
我正在导入以下版本:
//JETPACK COMPOSE
// Integration with activities
implementation 'androidx.activity:activity-compose:1.4.0'
// Compose Material Design …Run Code Online (Sandbox Code Playgroud)