HarmonyOS List组件ForEach中无法正确删除元素

操作步骤:

List() {
  ForEach(this.codes, (item: string, index: number) => {
    ListItem() {
      Row() {
        Text(`${index + 1}`)
          .fontSize('10sp')
          .height('20vp')
          .textAlign(TextAlign.Center)
          .fontColor('#666666')
          .padding({
            left: '3vp',
            right: '3vp'
          })
          .margin({
            left: '12vp',
            right: '8vp'
          })
          .backgroundColor('#AAAAAA')
          .borderRadius($r('sys.float.corner_radius_level2'))
        Text(`${item}`)
          .fontSize('12sp')
          .layoutWeight(1)
          .fontColor('#333333')
        Stack() {
          Image($r('app.media.icon_delete_record'))
            .width('14vp')
        }
        .width('34vp')
        .height('100%')
        .alignContent(Alignment.Center)
        .onClick(() => {
          this.codes.splice(this.codes.indexOf(item), 1)
        })
      }
      .width('100%')
      .height('36vp')
      .alignItems(VerticalAlign.Center)

      .justifyContent(FlexAlign.Start)
    }
  }, (item: string, index: number) => index.toString())
}
  • 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.

点击右侧删除按钮,无法正确删除元素。

HarmonyOS
2025-01-09 16:11:42
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
aquaa

参考示例如下:

class BasicDataSource implements IDataSource {
  private listeners: DataChangeListener[] = [];
  private originDataArray: string[] = [];

  public totalCount(): number {
    return 0;
  }

  public getData(index: number): string {
    return this.originDataArray[index];
  }

  registerDataChangeListener(listener: DataChangeListener): void {
    if (this.listeners.indexOf(listener) < 0) {
      console.info('add listener');
      this.listeners.push(listener);
    }
  }

  unregisterDataChangeListener(listener: DataChangeListener): void {
    const pos = this.listeners.indexOf(listener);
    if (pos >= 0) {
      console.info('remove listener');
      this.listeners.splice(pos, 1);
    }
  }

  notifyDataReload(): void {
    this.listeners.forEach(listener => {
      listener.onDataReloaded();
    })
  }

  notifyDataAdd(index: number): void {
    this.listeners.forEach(listener => {
      listener.onDataAdd(index);
    })
  }

  notifyDataChange(index: number): void {
    this.listeners.forEach(listener => {
      listener.onDataChange(index);
    })
  }

  notifyDataDelete(index: number): void {
    this.listeners.forEach(listener => {
      listener.onDataDelete(index);
    })
  }

  notifyDataMove(from: number, to: number): void {
    this.listeners.forEach(listener => {
      listener.onDataMove(from, to);
    })
  }
}

class MyDataSource extends BasicDataSource {
  dataArray: string[] = [];

  public totalCount(): number {
    return this.dataArray.length;
  }

  public getData(index: number): string {
    return this.dataArray[index];
  }

  public addData(index: number, data: string): void {
    this.dataArray.splice(index, 0, data);
    this.notifyDataAdd(index);
  }

  // 在数据尾部增加一个元素
  public AddLastItem(): void {
    this.dataArray.splice(this.dataArray.length, 0, this.dataArray.length.toString())
    this.notifyDataAdd(this.dataArray.length - 1)
  }

  public pushData(data: string): void {
    this.dataArray.push(data);
    this.notifyDataAdd(this.dataArray.length - 1);
  }

  public deleteData(index: number): void {
    this.dataArray.splice(index, 1);
    this.notifyDataDelete(index);
  }
}

@Entry
@Component
struct ListEdit {
  private data: MyDataSource = new MyDataSource();
  @State
  transitionTime: TransitionEffect = (TransitionEffect.IDENTITY)

  setDeleteAnimation() {
    this.transitionTime = (TransitionEffect.opacity(0)
      .animation({
        duration: 300,
      })).combine(TransitionEffect.asymmetric(TransitionEffect.move(TransitionEdge.END),
      TransitionEffect.move(TransitionEdge.START)))
  }

  setAddAnimation() {
    this.transitionTime = (TransitionEffect.opacity(0)
      .animation({
        duration: 300,
        delay: 500
      })).combine(TransitionEffect.asymmetric(TransitionEffect.move(TransitionEdge.END),
      TransitionEffect.move(TransitionEdge.START)))
  }

  aboutToAppear() {
    for (let i = 0; i <= 20; i++) {
      this.data.pushData(`${i}`)
    }
  }

  @State index2: number = 100

  build() {
    List({ space: 3 }) {
      LazyForEach(this.data, (item: string, index: number) => {
        ListItem() {
          Row() {
            Text(item)
              .fontSize(50)
              .width('70%')
              .height(80)
              .fontSize(20)
              .textAlign(TextAlign.Center)
              .borderRadius(10)
              .border({ width: 1 })
            Button('del')
              .onClick(() => {
                this.setDeleteAnimation()
                animateTo({
                  duration: 800,
                  delay: 300,
                  curve: Curve.Linear,
                  onFinish: () => {
                    this.transitionTime = (TransitionEffect.IDENTITY) //动画结束时设置禁止转场
                  }
                }, () => {
                  this.data.deleteData(this.data.dataArray.indexOf(item))
                })
              })
            Button('add')
              .onClick(() => {
                this.setAddAnimation()
                // 点击添加数据
                this.index2 = this.index2 + 1
                animateTo({
                  duration: 500, curve: Curve.Smooth, onFinish: () => {
                    this.transitionTime = (TransitionEffect.IDENTITY)
                  }
                }, () => {
                  this.data.addData(0, "new Data" + this.index2)
                })
              })
          }
          .margin({ left: 10, right: 10 })
        }
        .transition(
          this.transitionTime)
        .swipeAction({
          end: {
            onAction: () => {
              this.setDeleteAnimation()
              animateTo({ duration: 1000 }, () => {
                this.data.deleteData(this.data.dataArray.indexOf(item))
              })
            },
            actionAreaDistance: 56,
            onEnterActionArea: () => {
            },
            onExitActionArea: () => {
            }
          }
        })
        .onAppear(() => {
          console.info('cbl' + item)
          // 即将触底时提前增加数据
          if (item + 1 == this.data.totalCount().toString()) {
            for (let i = 0; i < 10; i++) {
              this.data.AddLastItem()
            }
          }
        })
      }, (item: string) => item)
    }.cachedCount(5)
  }
}
  • 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.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.
  • 160.
  • 161.
  • 162.
  • 163.
  • 164.
  • 165.
  • 166.
  • 167.
  • 168.
  • 169.
  • 170.
  • 171.
  • 172.
  • 173.
  • 174.
  • 175.
  • 176.
  • 177.
  • 178.
  • 179.
  • 180.
  • 181.
  • 182.
  • 183.
  • 184.
  • 185.
  • 186.
  • 187.
  • 188.
  • 189.
  • 190.
  • 191.
  • 192.
  • 193.
  • 194.
  • 195.
  • 196.
分享
微博
QQ
微信
回复
2025-01-09 18:45:50
相关问题
HarmonyOS NavPathStack如何删除元素
731浏览 • 1回复 待解决
ForEach数组数据无法传输
6205浏览 • 1回复 待解决
HarmonyOS 无法正确生成
627浏览 • 1回复 待解决
HarmonyOS Repeat无法正确刷新
575浏览 • 1回复 待解决
HarmonyOS Navigation无法正确显示
664浏览 • 1回复 待解决
Record<string, string>如何删除里边的元素
2278浏览 • 1回复 待解决
ForEach在真机上无法执行
2745浏览 • 1回复 待解决
list组件无法滚动到底部
2312浏览 • 1回复 待解决
HarmonyOS Listlistitem较少时无法拖拽
395浏览 • 1回复 待解决
HarmonyOS list元素拖动换位置的实现
619浏览 • 1回复 待解决
HarmonyOS 平板无法正确横竖屏切换
933浏览 • 1回复 待解决
HarmonyOS list无法滑动
523浏览 • 1回复 待解决