参考demo:@Entry @Component struct ListItemExample2 { @State arr: number[] = [0, 1, 2, 3, 4] @State enterEndDeleteAreaString: string = "not enterEndDeleteArea" @State exitEndDeleteAreaString: string = "not exitEndDeleteArea" @State isShow:boolean = true; @Builder itemEnd() { Row() { Button("Delete").margin("4vp") Button("Set").margin("4vp") }.padding("4vp").justifyContent(FlexAlign.SpaceEvenly) } build() { Column() { Button("点击").onClick(()=>{ this.isShow=!this.isShow console.log(this.isShow+"111111111") }) List({ space: 10 }) { ForEach(this.arr, (item: number) => { ListItem() { Text("item" + item) .width('100%') .height(100) .fontSize(16) .textAlign(TextAlign.Center) .borderRadius(10) .backgroundColor(0xFFFFFF) } .transition({ type: TransitionType.Delete, opacity: 0 }) .swipeAction({ end: this.isShow?{ builder: () => { this.itemEnd() }, onAction: () => { animateTo({ duration: 1000 }, () => { let index = this.arr.indexOf(item) this.arr.splice(index, 1) }) }, actionAreaDistance: 56, onEnterActionArea: () => { this.enterEndDeleteAreaString = "enterEndDeleteArea" this.exitEndDeleteAreaString = "not exitEndDeleteArea" }, onExitActionArea: () => { this.enterEndDeleteAreaString = "not enterEndDeleteArea" this.exitEndDeleteAreaString = "exitEndDeleteArea" } }:undefined }) }, (item: string) => item) } Text(this.enterEndDeleteAreaString).fontSize(20) Text(this.exitEndDeleteAreaString).fontSize(20) } .padding(10) .backgroundColor(0xDCDCDC) .width('100%') .height('100%') } }
参考demo: