通过选择容器在 Compose LazyColumn 中启用文本选择

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

我之前使用 WebView 来显示长文本并根据某些业务逻辑设置每个单词的样式。不过,我最近将此 HTML 内容转换为句子列表,并利用 LazyColumn 将其呈现在我的 Compose 应用程序中。

我在之前的实现中珍惜的功能之一是能够选择文本并利用弹出选项进行复制或共享等操作。

我尝试将 LazyColumn 包装在 Jetpack Compose 中的 SelectionContainer 中,但它目前阻止我在列表中的不同项目之间选择文本。

我很好奇是否有办法在我的新 Compose 结构中保留相同的文本选择行为。任何建议或见解将不胜感激。

到目前为止我已经尝试过这些:

  LazyColumn(
    modifier = Modifier.fillMaxSize(),
    content = {
        items(sentenceList) { index ->
            SelectionContainer {
                Text(
                    text = sentenceList[index]
                )
            }
        }
    }
)

还有这个:

SelectionContainer {
  LazyColumn(
        modifier = Modifier.fillMaxSize(),
        content = {
            items(sentenceList) { index ->
                    Text(
                        text = sentenceList[index]
                    )
                }
            }
    )
 }

更新1:

我应该提到第二个选项确实有效,但是当您尝试上下滚动然后尝试再次长按文本选择时会出现问题;奇怪的是,它停止工作了。

android android-jetpack-compose textselection lazycolumn android-jetpack-compose-lazy-column
1个回答
0
投票

我想你可能错过了

State
中的
LazyColumn
。我已经尝试过下面的代码,它对我有用。

试试这个

@Preview(showBackground = true)
@Composable
 fun TestPreview() {
    LazyColumn(
        state = rememberLazyListState(),
        modifier = Modifier.fillMaxSize(),
        content = {
            items(65) { index ->
                SelectionContainer {
                    Text(
                        text = "I have pasted long text for text selection demo."
                    )
                }
            }
        }
    )

}
© www.soinside.com 2019 - 2024. All rights reserved.