【鸿蒙5.0】Scroll左右滑动

效果图

运行效果图

代码如下

复制代码
import { curves } from '@kit.ArkUI'

@Entry
@Component
struct ScrollExample {
  scroller: Scroller = new Scroller()
  private arr: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

  build() {
    Stack({ alignContent: Alignment.TopStart }) {
      Column(){
        Scroll(this.scroller) {
          Row() {
            ForEach(this.arr, (item: number) => {
              Text(item.toString())
                .fontColor(Color.Orange)
                .width(150)
                .height('90%')
                .backgroundColor(0xFFFFFF)
                .borderRadius(15)
                .fontSize(16)
                .textAlign(TextAlign.Center)
                .margin({ left: 10 })
            }, (item: string) => item)
          }
          .height('50%')
        }
        .scrollable(ScrollDirection.Horizontal)
        .scrollBar(BarState.On)
        .scrollBarColor(Color.Gray)
        .scrollBarWidth(10)
        .friction(0.6)
        .edgeEffect(EdgeEffect.None)
        .onWillScroll((xOffset: number, yOffset: number, scrollState: ScrollState) => {
          console.info(xOffset + ' ' + yOffset)
        })
        .onScrollEdge((side: Edge) => {
          console.info('To the edge')
        })
        .onScrollStop(() => {
          console.info('Scroll Stop')
        })

        Column({ space: 15 }) {
          Button('scroll 150 右滑指定距离150vp')
            .onClick(() => {
              this.scroller.scrollBy(150, 0)
            })

          Button('scroll 100')
            .onClick(() => {
              const xOffset: number = this.scroller.currentOffset().xOffset;
              this.scroller.scrollTo({ xOffset: xOffset + 100, yOffset: 0 })
            })

          Button('scroll 100 with animation')
            .onClick(() => {
              let curve = curves.interpolatingSpring(10, 1, 228, 30)
              const xOffset: number = this.scroller.currentOffset().xOffset;
              this.scroller.scrollTo({
                xOffset: xOffset + 100,
                yOffset: 0,
                animation: { duration: 1000, curve: curve }
              })
            })

          Button('back to start')
            .onClick(() => {
              this.scroller.scrollEdge(Edge.Start)
            })

          Button('next page')
            .onClick(() => {
              this.scroller.scrollPage({ next: true, animation: true })
            })

          Button('fling -3000')
            .onClick(() => {
              this.scroller.fling(-3000)
            })

          Button('scroll to end')
            .onClick(() => {
              this.scroller.scrollEdge(Edge.End, { velocity: 700 })
            })
        }
      }
      .width('100%')
      .height('100%')
      .backgroundColor(0xDCDCDC)
    }
    .width('100%')
    .height('100%')
    .backgroundColor(0xDCDCDC)
  }
}
相关推荐
木咺吟2 小时前
鸿蒙原生应用实战(四):愿望单与个人统计 — 数据聚合与可视化
华为·harmonyos
木咺吟2 小时前
鸿蒙原生应用实战(二):游戏库列表与筛选排序 — 卡片式UI设计
harmonyos
互联网散修4 小时前
鸿蒙实战:从零实现自定义相机(下)——填平预览拉伸、比例错乱、缩略图消失的六大坑
数码相机·华为·harmonyos
风华圆舞4 小时前
鸿蒙 + Flutter 下 AI 助手为什么要支持流式输出
人工智能·flutter·harmonyos
金启攻5 小时前
【鸿蒙原生应用实战】第四篇:打包清单——勾选交互、进度计算与实用工具
harmonyos
Swift社区5 小时前
鸿蒙 App 卡顿分析:定位方法 + 优化代码实战
华为·harmonyos
坚果派·白晓明5 小时前
鸿蒙 PC 应用集成 libhv 鸿蒙化三方库 —— AtomCode + Skills 驱动的高效集成实践
c语言·c++·ai编程·harmonyos·atomcode
祭曦念6 小时前
【共创季稿事节】HarmonyOS动态任务列表开发实战
华为·harmonyos
祭曦念7 小时前
【共创季稿事节】鸿蒙原生ArkTS动态列表布局实战_State_ForEach完整指南
华为·harmonyos
不羁的木木7 小时前
《HarmonyOS 6.1 新能力实战之智感握姿》第二篇:核心功能——查询与监听握持手状态
华为·harmonyos