所以我有一个android电视项目,我的结构是这样的
TvLazyColumn(
state = listState,
pivotOffsets = PivotOffsets(0.15F),
modifier = Modifier
.focusRequester(setFocusContinueWatch)
.onKeyEvent {
if (it.nativeKeyEvent.action == KeyEvent.ACTION_DOWN) {
when (it.nativeKeyEvent.keyCode) {
KeyEvent.KEYCODE_DPAD_UP -> {
if (selectedRow > 0) {
selectedRow--
isUpFromList = false
} else {
isUpFromList = true
setFocusContinueWatch.freeFocus()
isFocusOnListing = false
}
}
KeyEvent.KEYCODE_DPAD_DOWN -> {
if (selectedRow < sizeOfRows!!) {
selectedRow++
isFocusOnListing = true
isUpFromList = false
}
}
}
}
false
}
现在在此之上,我有两种类型的视图,一种是在 focusonlisting 时查看,另一种是在 !focusonlisting 上查看,所以在 !focusonlisting 的情况下,我有一个按钮 watchnowbutton。所以我有一个索引逻辑,用于现在当我在顶行时按下向上按钮并阻止逻辑触发器
LaunchedEffect(isFocusOnListing) {
if (isUpFromList) {
setFocusContinueWatch.freeFocus()
watchNowBtnFocus = true
setFocusOnWatchNow.captureFocus()
}
}
现在,当我从第一行索引 0 向上按下时,焦点位于按钮上,因为按钮的颜色在焦点上发生变化,但焦点也停留在该行上,并且显示边框,当向左向右移动时,我正在移动行中的左侧和右侧。现在我必须再按一次才能真正转到按钮。
当您从第一行向上按时,您将获得分离焦点状态,其中按钮和该行似乎都具有焦点,需要额外的向上按才能完全移动到按钮。
我在你的代码中所做的更改:
1.处理UP/DOWN键消费事件时添加true返回值。
2.从第一行向上移动时清除行选择
3.添加启动效果小延迟
这是您更新的代码和我的评论:
TvLazyColumn(
state = listState,
pivotOffsets = PivotOffsets(0.15F),
modifier = Modifier
.focusRequester(setFocusContinueWatch)
.onKeyEvent {
if (it.nativeKeyEvent.action == KeyEvent.ACTION_DOWN) {
when (it.nativeKeyEvent.keyCode) {
KeyEvent.KEYCODE_DPAD_UP -> {
if (selectedRow > 0) {
selectedRow--
isUpFromList = false
} else {
isUpFromList = true
setFocusContinueWatch.freeFocus()
// Clear the row selection when moving up*
selectedRow = -1
// Or another invalid value*
isFocusOnListing = false
}
true
// Consume the event*
}
KeyEvent.KEYCODE_DPAD_DOWN -> {
if (selectedRow < sizeOfRows!!) {
selectedRow++
isFocusOnListing = true
isUpFromList = false
}
true
// Consume the event*
}
else -> false
}
} else false
}
)
在 Launcheffect 中更改此设置:
LaunchedEffect(isFocusOnListing) {
if (isUpFromList) {
// Ensure complete focus transfer
setFocusContinueWatch.freeFocus()
delay(50) // Small delay to ensure focus release
watchNowBtnFocus = true
setFocusOnWatchNow.captureFocus()
}
}