kotlin 使用Jetpack合成的参数导航

sdnqo3pr  于 2023-02-13  发布在  Kotlin
关注(0)|答案(1)|浏览(137)

我已经解决这个问题几天了,现在关于在jetpack compose中用args导航。我觉得我错过了一些简单的东西,真的很简单,我不能弄清楚。我试图确保我的编码正确,但无济于事,我仍然得到错误。
因此,错误:无法在导航图中找到与请求NavDeepLink匹配的导航目的地请求{ uri=android-app://androidx.navigation/second_screen/Item Six } NavGraph(0x 0)startDestination={目的地(0x 442 b361 f)route=主页屏幕}
我觉得我做的每件事都是正确的,但还是不能克服这个错误。或者是我遗漏了一些简单的东西。
我的导航主机:

interface NavigationDestination{
    val route: String
}

@Composable
fun MainNavHost(
){
    val navController = rememberNavController()
    NavHost(
        navController = navController,
        startDestination = HomeScreenNavigation.route,
    ){
        composable(HomeScreenNavigation.route){
            HomeScreen(onItemClick = {nameString->
                    navController.navigate("${SecondNavigationDestination.route}/${nameString}")
                        {
                            launchSingleTop = true
                        }
            })
        }
        composable(
            route = SecondNavigationDestination.routeWithArg,
            arguments = listOf(navArgument(SecondNavigationDestination.nameArg)
            {type = NavType.StringType})
        ){
            SecondScreen()
        }
    }
}

我的屏幕
第一次筛选:

object HomeScreenNavigation: NavigationDestination{
    override val route = "home_screen"
}

@Composable
fun HomeScreen(
    modifier: Modifier = Modifier,
    onItemClick: (String) -> Unit
){
    ItemList(
        itemList = LocalData.itemList,
        onItemClick = onItemClick
    )
}

@Composable
fun ItemList(
    itemList: List<ItemModel>,
    onItemClick: (String) -> Unit,
    modifier: Modifier = Modifier
){
    LazyColumn(
        verticalArrangement = Arrangement.spacedBy(9.dp)
    ){
        items(
            items = itemList,
            key = {it.id}
        ){
            Item(
                item = it,
                onItemClick = onItemClick
            )
        }
    }
}

@Composable
fun Item(
    item: ItemModel,
    modifier: Modifier = Modifier,
    onItemClick: (String) -> Unit
){
    Row(
        modifier = modifier
            .fillMaxWidth()
            .padding(9.dp)
            .clickable { onItemClick(item.name) },
        horizontalArrangement = Arrangement.Center,
        verticalAlignment = Alignment.Top
    ){
    Text(
        text = item.name
    )
    }
}

/////////////////////////////////////////////////////////////第二屏:

object SecondNavigationDestination: NavigationDestination{
    override val route = "second_screen"
    const val nameArg = "nameArg"
    val routeWithArg = "${route}/$nameArg"
}

@Composable
fun SecondScreen(
){
    val viewModel: MainViewModel = viewModel()
    Box(
    contentAlignment = Alignment.Center
    ){
        Text(
            viewModel.id
        )
    }
}

/////////////////////////////////////视图模型:

class MainViewModel(
    savedStateHandle: SavedStateHandle
): ViewModel(){
    val id: String = savedStateHandle[SecondNavigationDestination.nameArg] ?: "empty"
}
fafcakar

fafcakar1#

改变这个

val routeWithArg = "${route}/$nameArg"

到这个

val routeWithArg = "${route}/{$nameArg}"

更多信息:文档

相关问题