HarmonyOS @ObservedV2装饰器和@Track装饰器配合List组件,点击ListItem添加选中效果怎么做?

如题:HarmonyOS @ObservedV2装饰器和@Track装饰器配合List组件,点击ListItem添加选中效果怎么做?

阅读 592
1 个回答

ListItem添加选中效果可以参考demo:

@Entry
@Component
struct ListItemExample {
  @State arr: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  @State select: number[] = []

  build() {
    Column({ space: 10 }) {
      List({ space: 20 }) {
        ForEach(this.arr, (item: number) => {
          ListItem() {
            Text(''+ item)
              .width('100%')
              .height(100)
              .fontSize(16)
              .textAlign(TextAlign.Center)
              .backgroundColor(this.select.includes(item) ?'#ffa0a0':'#ffffff')
              .onClick(() => {
                this.select = [item]
              })
          }
        }, (item: number) => item.toString())
      }
      .listDirection(Axis.Vertical)
      .scrollBar(BarState.Off)
      .friction(0.6)
    }
  }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进