在 XML 中,我们有 Material3 底部工作表。它允许我们设置底部工作表的行为。它可以像这样更新:
bottomSheetBehavior.halfExpandedRatio = 0.6
bottomSheetBehavior.state = BottomSheetBehavior.STATE_HALF_EXPANDED
Run Code Online (Sandbox Code Playgroud)
我正在将项目迁移到 Compose。我的应用程序将这个半展开比例用于 3 个定位的底部工作表:折叠、半展开、展开。现在我正在尝试创建这样的底表:
val sheetState = rememberBottomSheetState(
initialValue = BottomSheetValue.Collapsed
)
val scaffoldState = rememberBottomSheetScaffoldState(
bottomSheetState = sheetState
)
BottomSheetScaffold(
scaffoldState = scaffoldState,
sheetContent = {}
) {}
Run Code Online (Sandbox Code Playgroud)
但看起来我们这里没有这些行为属性。我们能否获得与 XML 中具有一半展开的底部表相同的行为?
android kotlin bottom-sheet android-jetpack-compose android-jetpack-compose-material3
我的时间以毫秒为单位:
val past = System.currentTimeMillis()
val future = System.currentTimeMillis() + 1000L
// getting duration every second. imagine working stopwatch here
val duration = future - past
// Imconvert duration to HH:MM:SS
Run Code Online (Sandbox Code Playgroud)
。我需要将其转换为秒表格式(HH:MM:SS)。我知道有很多选择。但最现代、最简单的方法是什么?
我有2个流量。第一个流每 50 毫秒更新一次。我有第二个流,它等于第一个流,但我希望它每 300 毫秒从原始流生成最新值。我找到了debounce流程扩展,但它不起作用(文档中的注释):
Note that the resulting flow does not emit anything as long as the original flow emits items faster than every timeoutMillis milliseconds.
因此,当我尝试每 300 毫秒发出一次带有去抖动的值时,它根本不会发出,因为原始流比我需要的要快。那么我怎样才能做这样的事情:
我现在的流程:
// fast flow (50ms)
val orientationFlow = merge(_orientationFlow, locationFlow)
val cameraBearingFlow = orientationFlow.debounce(300ms)
Run Code Online (Sandbox Code Playgroud)
PS 这种方法不适合,因为我们延迟了值,所以 300 毫秒后它就不再新鲜了。我需要在 300 毫秒后获取最新值:
val cameraBearingFlow = azimuthFlow.onEach {
delay(ORIENTATION_UPDATE_DELAY)
it
}
Run Code Online (Sandbox Code Playgroud) 我有位置列表。像这样的东西:
[{"latitude": "45.42123", "longitude": "44.32132", "speed": 4.3}, {"latitude": "46.212", "longitude": "45.4334", "speed": 5.2}]
我们在 Kotlin 中有扩展,所以我可以轻松获得速度的最大值,如下所示:
myList.maxOf { it.speed }
但我怎样才能得到速度的平均值呢?
PS我在Java中发现了同样的问题。我需要 Kotlin 中的解决方案。