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

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

先看效果图:

代码实现:

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

    向右滑动

相关推荐
NJPJI_Yang11 分钟前
鸿蒙操作系统使用 ArkTS 状态管理 V2 实现跨页面共享与持久化实战(本文已在AtomGit仓库开源)
华为·开源·harmonyos
渣哥搞AI1 小时前
在鸿蒙 PC 上用 Electron 写鸿蒙 PC 应用:星枢 AstroHub 的跨平台框架落地与 16 路能力调用拓扑
华为·harmonyos
向哆哆2 小时前
在鸿蒙PC上用ArkUI开发跨平台缓存可视化应用:CacheLoom实战
缓存·华为·harmonyos
向哆哆2 小时前
在鸿蒙PC上用Electron跨平台框架开发桌面应用:动量台 Momentum 的窗口动力学引擎与 HAP 交付实践
华为·electron·harmonyos
时光慢煮3 小时前
在鸿蒙 PC 上用 Electron 跨平台框架写鸿蒙 PC 应用:信交所 SignalBourse 的能力行情撮合与 HAP 上屏实战
华为·electron·harmonyos
程序猿追3 小时前
返库看板最小化后还会刷新吗?HarmonyOS 7 窗口状态实测
android·开发语言·kotlin·harmonyos
苗俊祥3 小时前
OpenHarmony 7.0 源码走读 14|包管理:一个 HAP 从安装到运行
系统架构·鸿蒙·源码分析·openharmony·7.0
向哆哆5 小时前
Electron 跨平台框架落地鸿蒙 PC:用 OrbitKey 构建“授权闸门 + 本地审计链“桌面应用
缓存·华为·harmonyos
传奇开心果编程5 小时前
【华为仓颉独特优势】第11课:回到主场——HarmonyOS 上的仓颉:ArkTS 互操作与“宿主问题”的终极答卷
开发语言·华为·harmonyos·编程语言理论·仓颉·cangjie·通用编程语言