gpt4 book ai didi

android - 如何使用 JetPack Compose 在 LazyColumn 项目中单击导航到详细 View ?

转载 作者:行者123 更新时间:2023-12-04 23:49:07 24 4
gpt4 key购买 nike

我正在尝试使用 JetPack Compose(第一次)创建一个应用程序,但导航遇到了一些问题。该应用程序有一个bottomBar,其中包含导航到所选屏幕的3 个项目。
这行得通,问题是当我尝试访问其中一个屏幕中的 LazyColumn 的一项时。我想导航到显示所选项目数据的另一个屏幕(配置文件),但我找不到方法。无论我如何尝试,我总是得到这个“@Composable 调用只能在@Composable 函数的上下文中发生”。
有人可以通过解释如何做来帮助我吗?我想要的是学习如何以及为什么,而不仅仅是复制。
谢谢
主要 Activity

class MainActivity : ComponentActivity() {

@ExperimentalFoundationApi
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)


setContent {
val systemUiController = rememberSystemUiController()

SideEffect {
systemUiController.setStatusBarColor(color = PrimaryDark)
}
AppTheme() {


MainScreen()

}

}
}

@ExperimentalFoundationApi
@Composable
fun MainScreen() {
val navController = rememberNavController()
val navigationItems = listOf(Obras, Talleres, Ajustes)
Scaffold(bottomBar = {
BottomNavigationBar(
navController = navController,
items = navigationItems
)
}) {

NavigationHost(navController)
}
}
}
NavigationHost.kt
@ExperimentalFoundationApi
@Composable
fun NavigationHost(navController: NavHostController) {


NavHost(navController = navController, startDestination = Obras.route) {
composable(Obras.route) {
Pantalla1(navigateToProfile = { authorId ->
navController.navigate("${Profile.route}/$authorId")
})

}
composable(Talleres.route) {
Pantalla2()
}

composable(Ajustes.route) {
Pantalla3()
}

composable(
Profile.route + "/{authorId}",
arguments = listOf(navArgument("authorId") { type = NavType.StringType })
) { backStackEntry ->
val authorId = backStackEntry.arguments!!.getString("authorId")!!
Profile(authorId)
}

}
}

Pantalla1.kt
typealias AuthorId = String

@ExperimentalCoroutinesApi
@Composable
fun Pantalla1(navigateToProfile: (AuthorId) -> Unit) {


Column(
modifier = Modifier
.fillMaxSize()
.padding(
paddingValues = PaddingValues(

bottom = 50.dp
)
),
) {

AutoresInfo(navigateToProfile)
}

}

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun AutoresInfo(navigateToProfile: (AuthorId) -> Unit) {
var autoresList by remember {
mutableStateOf<List<Autor>?>(null)
}

JetFirestore(path = { collection("autores") },
queryOnCollection = { orderBy("nombre", Query.Direction.ASCENDING) },

onRealtimeCollectionFetch = { value, exception ->
autoresList = value.getListOfObjects()
}) {
autoresList?.let {
val grouped = it.groupBy { it.nombre[0] }
LazyColumn(

modifier = Modifier.fillMaxSize()

) {


grouped.forEach { (initial, autoresForInitial) ->
stickyHeader {
StickyHeaderAutores(initial = initial.toString())
}

items(autoresForInitial, key = { autor -> autor.nombre }) { autor ->
Surface(modifier = Modifier.clickable { navigateToProfile(autor.nombre) }) {
@OptIn(coil.annotation.ExperimentalCoilApi::class)
AutorCard(autor)
}


}
}


}
} ?: Column(
modifier = Modifier.fillMaxSize(),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
CircularProgressIndicator(
color = Color.Red,
modifier = Modifier

.size(50.dp)

)
}

}


}

最佳答案

步骤 1. 将参数添加到您的个人资料导航路线。查看 documentation关于使用参数导航

composable(
Profile.route + "/{authorId}",
arguments = listOf(navArgument("authorId") { type = NavType.StringType })
) { backStackEntry ->
val authorId = backStackEntry.arguments!!.getString("authorId")!!
Profile(authorId)
}
步骤 2. 您需要传递 navigateToProfile来自您的 NavigationHost 的函数.您可以替换 AuthorIdInt (或其他类型)如果不是 String :
typealias AuthorId = String

@Composable
fun NavigationHost(navController: NavHostController){
NavHost(navController = navController, startDestination = Obras.route) {
composable(Obras.route) {
Pantalla1(navigateToProfile = { authorId ->
navController.navigate("${Profile.route}/$authorId")
})
}
...
}

@Composable
fun Pantalla1(navigateToProfile: (AuthorId) -> Unit) {
...
AutoresInfo(navigateToProfile)
...
}

@Composable
fun AutoresInfo(navigateToProfile: (AuthorId) -> Unit) {
...
items(autoresForInitial, key = { autor -> autor.nombre }) { autor ->
Surface(modifier = Modifier.clickable {
navigateToProfile(author.id)
}) {
AutorCard(autor)
}
}
...
}
第 3 步。在您的可组合配置文件中,您需要按 id 获取作者。不确定 JetFirestore 是什么,您可能应该使用它。
@Composable
fun Profile(id: AuthorId) {
JetFirestore(
// fetch author by id
)
}

关于android - 如何使用 JetPack Compose 在 LazyColumn 项目中单击导航到详细 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69181995/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com