HarmonyOS 如何实现可展开的listview功能

HarmonyOS
2025-01-09 16:59:22
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
shlp

参考示例如下:

@Entry
@Component
struct List0812 {
  @State textHt: number = 30
  @State selectedIndex: number = 0;
  @State list: number[] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
  @State heightList: number[] = [this.textHt, 0, 0, 0, 0, 0, 0, 0, 0, 0];

  build() {
    Column() {
      List() {
        ForEach(this.list, (item: number, index) => {
          ListItem() {
            Column() {
              Text('text_' + item)
                .onClick(() => {
                  this.selectedIndex = index;
                  this.heightList.forEach((v, i) => {
                    if (i === this.selectedIndex) {
                      this.heightList[i] = this.textHt;
                    } else {
                      this.heightList[i] = 0;
                    }
                  })
                })
                .width('100%')
                .padding(10)
                .height(40)
                .backgroundColor(Color.Pink)

              Column() {
                Text('ui_' + item + '_1')
                  .height(this.heightList[index])
                Text('ui_' + item + '_2')
                  .height(this.heightList[index])
                Text('ui_' + item + '_3')
                  .height(this.heightList[index])
                Text('ui_' + item + '_4')
                  .height(this.heightList[index])
              }
              .width('100%')
              .backgroundColor(Color.Yellow)
            }
          }
        })
      }.width('90%').height('100%')
    }.width('100%').height('100%')
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
分享
微博
QQ
微信
回复
2025-01-09 19:49:52
相关问题
如何实现文本展开收起功能
1344浏览 • 1回复 待解决
HarmonyOS 如何实现半屏展开
963浏览 • 1回复 待解决
HarmonyOS 如何实现拖拽GridView
507浏览 • 1回复 待解决
HarmonyOS Image组件关闭拖拽功能
444浏览 • 1回复 待解决
ArkTS实现Text文本【...展开
2483浏览 • 3回复 待解决