小编Noa*_*oah的帖子

使用撰写导航传递 Parcelable 参数

我想BluetoothDevice使用组合导航将 Parcelable 对象 ( ) 传递给可组合对象。

传递原始类型很容易:

composable(
  "profile/{userId}",
  arguments = listOf(navArgument("userId") { type = NavType.StringType })
) {...}
Run Code Online (Sandbox Code Playgroud)
navController.navigate("profile/user1234")
Run Code Online (Sandbox Code Playgroud)

但是我不能在路由中传递一个parcelable 对象,除非我可以将它序列化为一个字符串。

composable(
  "deviceDetails/{device}",
  arguments = listOf(navArgument("device") { type = NavType.ParcelableType(BluetoothDevice::class.java) })
) {...}
Run Code Online (Sandbox Code Playgroud)
val device: BluetoothDevice = ...
navController.navigate("deviceDetails/$device")
Run Code Online (Sandbox Code Playgroud)

上面的代码显然不起作用,因为它只是隐式调用toString().

有没有办法要么序列化ParcelableString,所以我可以通过它的路线或通过导航参数作为比其他函数的对象navigate(route: String)

android kotlin android-jetpack-navigation android-jetpack-compose

13
推荐指数
5
解决办法
2672
查看次数

通过拖放重新排序 LazyColumn 项目

我想创建一个LazyColumn可以通过拖放重新排序的项目。如果没有 compose,我的方法是使用ItemTouchHelper.SimpleCallback,但我还没有找到类似的东西用于 compose。

我试过使用Modifier.longPressDragGestureFilterand Modifier.draggable,但这仅允许我使用偏移量拖动卡片。它没有给我一个列表索引(比如fromPosition/ toPositionin ItemTouchHelper.SimpleCallback),我需要交换列表中的项目。

有没有撰写相当于ItemTouchHelper.SimpleCallbackonMove功能?如果不是,它是计划中的功能吗?

自己尝试实施这种事情是否可能/可行?

android android-jetpack-compose

12
推荐指数
3
解决办法
1434
查看次数

MutableStateFlow 强制更新/通知收集器

MutableStateFlow如果更新值等于旧值(源),则不会通知收集器。我找到了解决此问题的方法,但对于复杂值来说它不能很好地扩展。

解决方法:使用/重复数据类copy()和列表。toList()toMutableList()

WorkoutRoutine示例 1:使用解决方法重命名的简单数据类name。这里没什么问题。

data class WorkoutRoutine(
    var name: String,
)

val workoutRoutine = MutableStateFlow(WorkoutRoutine("Initial"))
                                                                                           
workoutRoutine.value.name = "Updated" // Doesn't notify collectors
                                                                                           
workoutRoutine.value = workoutRoutine.value.copy(name = "Updated") // Workaround: works

Run Code Online (Sandbox Code Playgroud)

示例 2:WorkoutRoutine具有多个依赖项的复杂数据类,使用解决方法将 a 添加SetExercisein 中WorkoutRoutine:这需要大量的copy()andtoMutableList()调用,这使得代码不可读。

data class WorkoutRoutine(
    var name: String,
    var exercises: MutableList<Exercise> = mutableListOf(Exercise())
)
                                                                         
data class Exercise(
    var sets: MutableList<Set> = mutableListOf(Set())
)
                                                                         
data …
Run Code Online (Sandbox Code Playgroud)

kotlin kotlin-coroutines kotlin-flow

10
推荐指数
1
解决办法
1万
查看次数

在非惰性列中使用项目键

LazyColumn具有项目键,以便将项目的状态与唯一标识符而不是列表索引联系起来。有没有一种方法可以在像这样的非惰性列表中使用项目键?

Column {
  for (item in list) {
    Text(item)
  }
}
Run Code Online (Sandbox Code Playgroud)

我问的原因是因为我想实现SwipeToDismiss从列表中删除项目,这只有在将密钥传递给LazyColumn解决方案)时才有效,但是我的可忽略项目列表嵌套在 a 内部LazyColumn,并且我无法嵌套 aLazyColumn在 another 的 itemContent 块内部LazyColumn(不允许在同一方向布局中嵌套可滚动):

val items = listOf<String>(...)
val groups = items.groupBy { it.first() }

LazyColumn {
  items(groups, { /* key */ }) { (firstChar, group) ->
    // not allowed!
    LazyColumn {
      items(group, { /* key */ }) { item ->
        Text(item)
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

我可以将调用items()本身包装在 for …

android-jetpack-compose

7
推荐指数
1
解决办法
4723
查看次数

喷气背包组合导航的多个参数

如何声明具有多个导航参数的导航路线?我查了资料,并且所有 这些物品(这似乎只是重申文件说什么),我只能找到途径的例子有一个参数。

这是我所拥有的:

composable(
  route = "createExercise/{exerciseId}",
  arguments = listOf(navArgument("exerciseId") { type = NavType.IntType })
) { backStackEntry ->
  CreateExerciseScreen(
    exerciseId = backStackEntry.arguments!!.getInt("exerciseId"),
  )
}
Run Code Online (Sandbox Code Playgroud)

这是我想要的:

composable(
  route = "createExercise/{exerciseId},{workoutId}",
  arguments = listOf(
    navArgument("exerciseId") { type = NavType.IntType },
    navArgument("workoutId") { type = NavType.IntType },
  )
) { backStackEntry ->
  CreateExerciseScreen(
    exerciseId = backStackEntry.arguments!!.getInt("exerciseId"),
    workoutId = backStackEntry.arguments!!.getInt("workoutId"),
  )
}
Run Code Online (Sandbox Code Playgroud)

我为上面的例子随意选择了一个逗号分隔的语法来代替我正在寻找的真实语法。

所以,我的问题是:在声明导航路线时,多个参数的正确语法是什么?(可选参数呢?)

android android-jetpack-navigation android-jetpack-compose

4
推荐指数
2
解决办法
907
查看次数