如何在关注 TVLazyRow 的同时始终关注第一项?

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

我正在尝试实现一个 TVLazyRow,当我们关注该行上方其他可组合项的行时,它必须始终首先关注第一个项目。目前,按下时,焦点将转到行中位于上面可组合项正下方的项目。我怎样才能实现这种行为?

这是我的更多上下文代码:

val tvListState = rememberTvLazyListState()
val coScope = rememberCoroutineScope()

TvLazyRow(
        horizontalArrangement = Arrangement.spacedBy(15.dp),
        state = tvListState,
        modifier = modifier
            .fillMaxHeight()
            .padding(end = 5.dp)
            .onFocusChanged {
                if (it.isFocused) {
                    coScope.launch {
                        tvListState.scrollToItem(0)
                    }
                }
            }, pivotOffsets = PivotOffsets(0f)
) { *items* }
android-jetpack-compose android-jetpack android-tv
1个回答
0
投票

您似乎试图确保当焦点移动到您的 TVLazyRow 时,它始终从焦点中的第一个项目开始。要实现此行为,您可以对第一项使用 requestFocus()focusRequester 的组合。您可以通过以下方式修改代码来实现此目的:

val tvListState = rememberTvLazyListState()
val coScope = rememberCoroutineScope()
val firstItemFocusRequester = FocusRequester()

TvLazyRow(
horizontalArrangement = Arrangement.spacedBy(15.dp),
state = tvListState,
modifier = modifier
    .fillMaxHeight()
    .padding(end = 5.dp)
) {
// For the first item, use the FocusRequester
Box(
    modifier = Modifier
        .focusRequester(firstItemFocusRequester)
        .onFocusChanged { focusState ->
            if (focusState.isFocused) {
                coScope.launch {
                    tvListState.scrollToItem(0)
                }
            }
        }
) {
    // Your first item content here
}

// Other items
// ...
}

// Request focus on the first item when your TVLazyRow gains focus
LaunchedEffect(Unit) {
firstItemFocusRequester.requestFocus()
}
© www.soinside.com 2019 - 2024. All rights reserved.