Compose 在Row、Column上使用focusRestorer修饰符失效原因

focusRestorer 修饰符官方说明

复制代码
This modifier can be used to save and restore focus to a focus group. When focus leaves the focus group, it stores a reference to the item that was previously focused. Then when focus re-enters this focus group, it restores focus to the previously focused item.
Params:
fallback - A FocusRequester that is used when focus restoration fails to restore the initially focused item. For example, this might happen if the item is not available to be focused. The default value of FocusRequester.Default chooses the default focusable item.
Samples:
androidx.compose.ui.samples.FocusRestorerSample
LazyRow(Modifier.focusRestorer()) {
    item { Button(onClick = {}) { Text("1") } }
    item { Button(onClick = {}) { Text("2") } }
    item { Button(onClick = {}) { Text("3") } }
    item { Button(onClick = {}) { Text("4") } }
}
androidx.compose.ui.samples.FocusRestorerCustomFallbackSample
val focusRequester = remember { FocusRequester() }
LazyRow(
    // If restoration fails, focus would fallback to the item associated with focusRequester.
    Modifier.focusRestorer(focusRequester)
) {
    item {
        Button(modifier = Modifier.focusRequester(focusRequester), onClick = {}) { Text("1") }
    }
    item { Button(onClick = {}) { Text("2") } }
    item { Button(onClick = {}) { Text("3") } }
    item { Button(onClick = {}) { Text("4") } }
}

即针对focus group才能使用

所以 需要添加focusGroup修饰符

kotlin 复制代码
val focusRequester = remember { FocusRequester() }
Row(
    // If restoration fails, focus would fallback to the item associated with focusRequester.
    Modifier.focusRestorer(focusRequester).focusGroup() // 这里是关键
) {
    Button(modifier = Modifier.focusRequester(focusRequester), onClick = {}) { Text("1") }
    Button(onClick = {}) { Text("2") }
    Button(onClick = {}) { Text("3") }
    Button(onClick = {}) { Text("4") }
}
相关推荐
QING61811 小时前
Kotlin inline 实战详解 —— 新手须知
android·kotlin·android jetpack
我命由我123452 天前
Android 开发问题:TextView 内容超过宽度时,默认不会换行
android·开发语言·java-ee·android studio·android jetpack·android-studio·android runtime
simplepeng3 天前
我们都知道但总是忽略的5个Jetpack Compose细节
android·android jetpack
李斯维3 天前
Jetpack 生命周期组件 Lifecycle 的设计思想和使用
android·android studio·android jetpack
我命由我123454 天前
Android 开发:Unable to start service Intent { ... } U=0: not found
android·开发语言·android studio·android jetpack·android-studio·android runtime
李斯维5 天前
Android Jetpack 简介:由来和演进
android·android studio·android jetpack
我命由我123455 天前
Android Framework P1 - 低配学习 Framework 方案、开机启动 Init 进程
android·c语言·c++·学习·android jetpack·android-studio·android runtime
alexhilton6 天前
Android上的ZeroMQ:用发布/订阅模式连接Linux服务
android·kotlin·android jetpack
木子予彤8 天前
Jetpack Compose 的高性能下拉刷新与上拉加载更多组件
android jetpack