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") }
}
相关推荐
用户060905255221 天前
Compose 主题 MaterialTheme
android jetpack
用户060905255221 天前
Compose 简介和基础使用
android jetpack
用户060905255221 天前
Compose 重组优化
android jetpack
行墨1 天前
Jetpack Compose 深入浅出(一)——预览 @Preview
android jetpack
alexhilton3 天前
突破速度障碍:非阻塞启动画面如何将Android 应用启动时间缩短90%
android·kotlin·android jetpack
Pika4 天前
深入浅出 Compose 测量机制
android·android jetpack·composer
fundroid5 天前
掌握 Compose 性能优化三步法
android·android jetpack
ljt27249606619 天前
Compose笔记(五十一)--rememberTextMeasurer
android·笔记·android jetpack