HarmonyOS ListItem的swipeAction横滑,能否手动控制展开或者收起?

ListItem的swipeAction横滑,能否不通过手势滑动,通过事件触发收起,比如某个按钮控制swipeAction删除按钮的收起。

HarmonyOS
2024-10-16 11:44:37
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zxjiu

可以使用ListScroller提供的closeAllSwipeActions()方法将滑动效果进行恢复,参考文档如下:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-container-list-V5#closeallswipeactions11

示例代码如下:

import { common } from '@kit.AbilityKit'  
@CustomDialog  
struct CustomDialogExample {  
  cancel?: () => void  
  confirm?: () => void  
  controller: CustomDialogController  
  build() {  
    Column() {  
      Text('我是内容').fontSize(20).margin({ top: 10, bottom: 10 })  
      Flex({ justifyContent: FlexAlign.SpaceAround }) {  
        Button('cancel').onClick(() => {  
          this.controller.close()  
          if (this.cancel) this.cancel()  
        }).backgroundColor(0xffffff).fontColor(Color.Black)  
        Button('confirm').onClick(() => {  
          this.controller.close()  
          if (this.confirm) this.confirm()  
        }).backgroundColor(0xffffff).fontColor(Color.Red)  
      }.margin({ bottom: 10 })  
    }  
  }  
}  
@Entry  
@Component  
struct ListSwipePage {  
  @State arr: number[] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]  
  private scrollerForList: ListScroller = new ListScroller()  
  dialogController: CustomDialogController = new CustomDialogController({  
    builder: CustomDialogExample({  
      cancel: this.onCancel,  
      confirm: this.onAccept,  
    })  
  })  
  
  onCancel() {  
  }  
  
  onAccept() {  
  }  
  @Builder  
  itemEnd() {  
    Row() {  
      Button("Delete").margin("4vp")  
      Button("Set").margin("4vp")  
        .onClick(() => {  
          this.dialogController.open()  
          this.scrollerForList.closeAllSwipeActions() // 重点是这行代码  
        })  
    }.padding("4vp").justifyContent(FlexAlign.SpaceEvenly)  
  }  
  build() {  
    Column() {  
      List({ space: 5, scroller: this.scrollerForList }) {  
        ForEach(this.arr, (item: number) => {  
          ListItem() {  
            Text("item" + item)  
              .width('100%')  
              .height(100)  
              .textAlign(TextAlign.Center)  
              .borderRadius(10)  
              .backgroundColor(0xFFFFFF)  
          }  
          .transition({ type: TransitionType.Delete, opacity: 0 })  
          .swipeAction({  
            end: {  
              builder: () => {  
                this.itemEnd()  
              },  
              onAction: () => {  
                animateTo({ duration: 1000 }, () => {  
                  let index = this.arr.indexOf(item)  
                  this.arr.splice(index, 1)  
                })  
              }, actionAreaDistance: 56  
            }  
          })  
        }, (item: string) => item)  
      }  
    }  
    .padding(20).backgroundColor(0xDCDCDC).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.
  • 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.
分享
微博
QQ
微信
回复
2024-10-16 18:32:30
相关问题
HarmonyOS ListItemswipeAction(end:)问题
586浏览 • 1回复 待解决
如何实现文本展开收起功能
1415浏览 • 1回复 待解决
能否提供命令行,手动编译har或者hsp
1547浏览 • 1回复 待解决
HarmonyOS ListItem菜单动态设置
850浏览 • 1回复 待解决
HarmonyOS 怎么让listitem按钮失效
562浏览 • 1回复 待解决
HarmonyOS 如何控制软键盘打开、收起
1980浏览 • 1回复 待解决
HarmonyOS 怎么监控键盘弹出或者收起
585浏览 • 1回复 待解决