Jetpack Compose Material3 和 Material2 Slider onValueChangeFinished() 的行为不同

问题描述 投票:0回答:1

从 Material2 迁移到 Material3 后,我遇到了 Slider 的意外行为。具体来说,如果我尝试在 onValueChangeFinished() 中寻找新位置,它总是会阻止我滑动滑块。

这适用于 Material2 滑块,但不适用于 Material3:

val currentPosition = Model.currentSongPosition.observeAsState()
val currentDuration = Model.currentSongDuration.observeAsState()
Slider(
    value = currentPosition.value.toFloat(),
    onValueChange = {position ->
        Model.currentSongPosition.value = position.toLong()
    },
    onValueChangeFinished = {
        exoInstance?.seekTo(Model.currentSongPosition.value)
    }
    ,
    valueRange = 0f..duration.value!!.toFloat(),
)

onValueChangeFinished() 似乎只在我尝试在 exoPlayer 中寻找新位置时才会提前触发。目前,我已经通过在 onValueChangeFinished() 之外寻找歌曲位置来解决这个问题,但如果有人能告诉我这种行为背后的真正原因,我将不胜感激。

var valueChangeFinished by remember {
        mutableStateOf(false)
    }
LaunchedEffect(valueChangeFinished){
        exoInstance?.seekTo(currentPosition.value)
    }
Slider(
    value = currentPosition.value.toFloat(),
    onValueChange = { position ->
        Model.currentSongPosition.value = position.toLong()
    },
    onValueChangeFinished = {
        valueChangeFinished = !valueChangeFinished
    },
    valueRange = 0f..duration.value!!.toFloat(),
)

提前谢谢您!

kotlin android-jetpack-compose exoplayer2.x android-jetpack-compose-material3
1个回答
0
投票

Google 问题跟踪器上有一个问题描述了您的问题。该错误是,当用户仍在拖动时,onValueChangeFinished

回调会提前触发。实际上应该只在用户完成拖动后调用。

Android 开发团队已经分配了该错误并给予其最高优先级。您可以点击问题页面上的

+1

按钮来表明您也受到该错误的影响。

作为解决方法,您可以记住要在

onValueChangeFinished

 回调中执行的 lamdba,如下所示:

val onValueChangeFinishedCallback: () -> Unit = remember { exoInstance?.seekTo(Model.currentSongPosition.value) } Slider( value = currentPosition.value.toFloat(), onValueChange = { position -> Model.currentSongPosition.value = position.toLong() }, onValueChangeFinished = onValueChangeFinishedCallback, valueRange = 0f..duration.value!!.toFloat(), )
    
© www.soinside.com 2019 - 2024. All rights reserved.