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") }
}
相关推荐
hnlgzb6 小时前
请详细解释一下MVVM这个设计模型
android·kotlin·android jetpack·compose
hnlgzb2 天前
目前编写安卓app的话有哪几种设计模式?
android·设计模式·kotlin·android jetpack·compose
png3 天前
从零开始Compose天气预报(完结)
android jetpack
阿巴斯甜3 天前
produceState的使用:
android jetpack
阿巴斯甜3 天前
snapshotFlow的使用
android jetpack
菜鸟国国3 天前
从0开始学Jetpack Compose|第二篇:基础组件+核心布局,从零搭建实用UI
android jetpack
simplepeng3 天前
mutableStateOf(list) vs mutableStateListOf():该如何选择?
android jetpack
zh_xuan4 天前
Android Jetpack DataStore存储数据
android·android jetpack·datastore
simplepeng4 天前
MVI with Jetpack Compose:让你的应用更简洁和整洁
android jetpack
simplepeng4 天前
别再让团队困惑:少有人提及的 MVI 命名规范
android jetpack