温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

OpenHarmony列表组件怎样动画

发布时间:2026-10-10 14:41:09 来源:亿速云 阅读:94 作者:小樊 栏目:软件技术

在 OpenHarmony(ArkUI) 中,列表组件(如 List、LazyForEach、ForEach)的“动画”通常分为几类:列表项增删改动画、滚动动画、状态变化动画、转场动画。下面按常见场景给你说明。


一、列表项增删改动画(最常用)

1️⃣ 使用 ForEach + 状态变化(自动动画)

OpenHarmony 的 ForEach 在数据源变化时会自动做进场 / 退场动画。

@Entry
@Component
struct ListAnimDemo {
  @State items: string[] = ['A', 'B', 'C']

  build() {
    Column() {
      Button('添加')
        .onClick(() => {
          this.items.push(`Item ${this.items.length}`)
        })

      Button('删除')
        .onClick(() => {
          this.items.pop()
        })

      List() {
        ForEach(this.items, (item: string) => {
          ListItem() {
            Text(item)
              .fontSize(20)
              .padding(10)
          }
        }, (item: string) => item)
      }
    }
  }
}

✅ 效果:

  • 添加:列表项滑入
  • 删除:列表项滑出

二、自定义列表项动画(显隐 / 位移 / 缩放)

2️⃣ 使用 animateTo

@State show: boolean = true

ListItem() {
  Text('动画项')
    .opacity(this.show ? 1 : 0)
    .height(this.show ? 50 : 0)
}
Button('切换')
  .onClick(() => {
    animateTo({ duration: 300 }, () => {
      this.show = !this.show
    })
  })

✅ 常用于:

  • 折叠展开
  • 高度变化
  • 透明度变化

三、列表滚动动画

3️⃣ List 滚动到指定位置

private listScroller: ListScroller = new ListScroller()

List(this.listScroller) {
  // items
}

Button('滚动到顶部')
  .onClick(() => {
    this.listScroller.scrollToIndex(0)
  })

可选参数:

scrollToIndex(index, smooth: true)

✅ 支持平滑滚动动画


四、列表项点击动画

4️⃣ scale / translate 动画

@State scaleValue: number = 1

ListItem() {
  Text('点击放大')
    .scale({ x: this.scaleValue, y: this.scaleValue })
    .onClick(() => {
      animateTo({ duration: 200 }, () => {
        this.scaleValue = 1.2
      })
    })
}

五、页面 / 列表转场动画

5️⃣ pageTransition

pageTransition() {
  PageTransitionEnter({ duration: 300, type: RouteType.Push })
  PageTransitionExit({ duration: 300, type: RouteType.Pop })
}

适用于:

  • 列表 → 详情页
  • 返回列表动画

六、性能建议(重要)

✅ 推荐

  • 使用 LazyForEach(长列表)
  • 列表项动画尽量用 opacity / translate / scale

❌ 避免

  • 大范围 width / height 动画
  • 每一帧频繁 setState

七、常见动画组合

场景 推荐方式
列表增删 ForEach
折叠展开 animateTo
滚动定位 ListScroller
点击反馈 scale / opacity
页面切换 pageTransition

如果你能告诉我:

  • 用的是 ArkTS / eTS
  • 列表是 短列表还是长列表
  • 想做 哪种动画(增删 / 滚动 / 点击 / 转场)

我可以给你更精准的示例代码。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI
助
手