android 使用D-PAD编辑电视滚动

llycmphe  于 5个月前  发布在  Android
关注(0)|答案(1)|浏览(49)

我有这个密码

LazyVerticalStaggeredGrid(
    columns = StaggeredGridCells.Fixed(2),
    modifier = modifier.fillMaxSize(),
    contentPadding = PaddingValues(
        top = 16.dp,
        end = 12.dp,
        bottom = 16.dp,
    ),
    horizontalArrangement = Arrangement.spacedBy(16.dp),
    verticalItemSpacing = 16.dp,
    userScrollEnabled = true,
) {
    item {
        data.moreInformationText?.let { morInformationText ->
            Text(
                text = morInformationText,
                style = MaterialTheme.typography.bodyMedium.copy(
                    color = Color(context.partnerTheme.themePrimaryTint0),
                ),
            )
        }
    }

字符串
问题是,滚动与鼠标是工作,但因为这是一个电视滚动与D-垫不工作。
morinformationText真的很长,所以我们需要滚动它。
我试着用一个简单的

Column {
        Text(
            text = data.moreInformationText!!,
            modifier = Modifier.verticalScroll(rememberScrollState())
        )
    }


没有任何网格,但仍然没有工作。

34gzjxbg

34gzjxbg1#

在电视上,用户主要使用D-PAD进行交互。使用D-PAD,您可以将焦点从一个项目移动到另一个项目。因为您的文本不可聚焦,D-PAD无法将焦点移动到任何地方,因此没有滚动。您可能希望在屏幕上添加一些可聚焦的项目,如按钮,卡片等。顺便说一下,在电视上显示很长的文字不是一个好的用户体验。你应该寻找一些替代的设计。
如果您仍然希望有较长的文本块,您可以选择执行以下操作:
1.将文本分解为更小的块,并将每个块作为单独的可聚焦文本组件。将其 Package 在可滚动容器中。

LazyColumn {
  item {
    Text("Chunk 1", Modifider.focusable())
  }
  item {
    Text("Chunk 2", Modifider.focusable())
  }
  ...
}

字符串
1.使用onKeyEvent修饰符来监听长文本块上的按键事件,并使用该修饰符以编程方式滚动容器,如下所示。

@OptIn(ExperimentalTvMaterial3Api::class)
@Composable
fun App() {
    val someHugeText = (1..10000).joinToString(" ")
    val scrollAmount = 100f
    val scrollState = rememberScrollState()
    val cs = rememberCoroutineScope()

    fun scroll(reverse: Boolean = false) {
        cs.launch {
            scrollState.scrollBy(if (reverse) -scrollAmount else scrollAmount)
        }
    }

    Box(
        modifier = Modifier
            .fillMaxSize()
            .verticalScroll(scrollState)
            .onKeyEvent {
                if (it.nativeKeyEvent.action == ACTION_DOWN) {
                    return@onKeyEvent KeyEventPropagation.ContinuePropagation
                }
                if (it.nativeKeyEvent.keyCode == KEYCODE_DPAD_DOWN) {
                    scroll(false)
                    return@onKeyEvent KeyEventPropagation.StopPropagation
                }
                if (it.nativeKeyEvent.keyCode == KEYCODE_DPAD_UP) {
                    scroll(true)
                    return@onKeyEvent KeyEventPropagation.StopPropagation
                }
                return@onKeyEvent KeyEventPropagation.ContinuePropagation
            }
            .background(Color.Yellow)
            .clickable {
                println("Pikachu: scrollState value=${scrollState.value} maxValue=${scrollState.maxValue}")
            }
        ,
    ) {
        Text(text = someHugeText)
    }
}

internal object KeyEventPropagation {
    const val StopPropagation = true
    const val ContinuePropagation = false
}

相关问题