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

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

先看效果图:

代码实现:

// 任务类
class Task {
  static id: number = 1
  // 任务名称
  name: string = `任务${Task.id++}`
  // 任务状态
  finished: boolean = false
}
// 统一的卡片样式
@Styles function card() {
  .width('95%')
  .padding(20)
  .backgroundColor(Color.White)
  .borderRadius(15)
  .shadow({ radius: 6, color: '#1F000000', offsetX: 2, offsetY: 4 })
}
@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
    })
  }
}
  • .swipeAction({ end: ... })

    向左滑动

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

    向右滑动

相关推荐

  1. 自动驾驶辅助功能规范

    2024-03-18 08:48:01       54 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-18 08:48:01       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-18 08:48:01       106 阅读
  3. 在Django里面运行非项目文件

    2024-03-18 08:48:01       87 阅读
  4. Python语言-面向对象

    2024-03-18 08:48:01       96 阅读

热门阅读

  1. 【数据库】MySQL数据库基础

    2024-03-18 08:48:01       43 阅读
  2. 如何在MATLAB中进行循环和条件语句?

    2024-03-18 08:48:01       41 阅读
  3. Vue-- 实现简单版 vue-router

    2024-03-18 08:48:01       41 阅读
  4. C语言中大小写字母是如何转化的?

    2024-03-18 08:48:01       48 阅读
  5. Euler angles and Quaterean

    2024-03-18 08:48:01       40 阅读
  6. Leetcode 第388场周赛 问题和解法

    2024-03-18 08:48:01       42 阅读
  7. Redis 的数据类型及使用场景

    2024-03-18 08:48:01       38 阅读
  8. PyTorch学习笔记之激活函数篇(六)

    2024-03-18 08:48:01       37 阅读
  9. redis常见面试题

    2024-03-18 08:48:01       40 阅读
  10. Bean的实例化方式

    2024-03-18 08:48:01       39 阅读
  11. 在类Unix平台实现TCP客户端

    2024-03-18 08:48:01       33 阅读
  12. mysql提权总结(自学)

    2024-03-18 08:48:01       40 阅读
  13. 基于深度学习的车辆检测技术

    2024-03-18 08:48:01       37 阅读