• 【HarmonyOS】ArkUI - 向左/向右滑动删除


    核心知识点:List容器 -> ListItem -> swipeAction

    先看效果图:

    代码实现:

    // 任务类
    class Task {
      static id: number = 1
      // 任务名称
      name: string = `任务${Task.id++}`
      // 任务状态
      finished: boolean = false
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    // 统一的卡片样式
    @Styles function card() {
      .width('95%')
      .padding(20)
      .backgroundColor(Color.White)
      .borderRadius(15)
      .shadow({ radius: 6, color: '#1F000000', offsetX: 2, offsetY: 4 })
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    @Entry
    @Component
    struct PropPage {
      // 总任务数量
      @State totalTask: number = 0
      // 已完成任务数量
      @State finishTask: number = 0
      // 任务数组
      @State tasks: Task[] = []
    
      build() {
        Column({ space: 10 }) {
          // 新增任务按钮
          Button('新增任务')
            .width(200)
            .margin({ top: 10 })
            .onClick(() => {
              // 新增任务数据
              this.tasks.push(new Task())
              // 更新任务总数量
              this.totalTask = this.tasks.length
            })
    
          // 任务列表
          List({ space: 10 }) {
            ForEach(
              this.tasks,
              (item: Task, index) => {
                ListItem() {
                  Row() {
                    Text(item.name)
                      .fontSize(20)
                    Checkbox()
                      .select(item.finished)
                      .onChange(val => {
                        // 更新当前任务状态
                        item.finished = val
                        // 更新已完成任务数量
                        this.finishTask = this.tasks.filter(item => item.finished).length
                      })
                  }
                  .card()
                  .justifyContent(FlexAlign.SpaceBetween)
                }
                .swipeAction({ end: this.DeleteButton(index) })
              }
            )
          }
          .width('100%')
          .layoutWeight(1)
          .alignListItem(ListItemAlign.Center)
        }
        .width('100%')
        .height('100%')
        .backgroundColor('#F1F2F3')
      }
    
      @Builder DeleteButton(index: number) {
        Button() {
          Image($r('app.media.delete'))
            .fillColor(Color.White)
            .width(20)
        }
        .width(40)
        .height(40)
        .type(ButtonType.Circle)
        .backgroundColor(Color.Red)
        .margin(5)
        .onClick(() => {
          this.tasks.splice(index, 1)
          this.totalTask = this.tasks.length
          this.finishTask = this.tasks.filter(item => item.finished).length
        })
      }
    }
    
    • 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
    • .swipeAction({ end: ... })

      向左滑动

    • .swipeAction({ start: ... })

      向右滑动

  • 相关阅读:
    vue自定义密码输入框解决浏览器自动填充密码的问题
    【xposed】虚拟机安装Magisk和LSPoesd
    elmentui el-select下拉输入不清空已选值
    逆向中webpack需要补充的模块很多怎么办
    【微服务 | 学成在线】项目易错重难点分析(媒资管理模块篇·上)
    Python绘制三维图详解
    基于JAVA+SSM+微信小程序+MySql的图书捐赠管理系统设计与实现
    第一百五十七回 SliverList组件
    阿里云幻兽帕鲁Linux 服务器下载游戏存档的方法
    CSS基本讲解与使用(详解)
  • 原文地址:https://blog.csdn.net/cnwutianhao/article/details/136774683