【鸿蒙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)
  }
}
相关推荐
duluo1332 小时前
鸿蒙NEXT实战:从零构建高尔夫挥杆教学App(API 24 / ArkTS 深度解析)
华为·harmonyos·鸿蒙·鸿蒙系统
zjxcq5203 小时前
鸿蒙深入理解 HarmonyOS NEXT ArkTS 中 `height(‘100%‘)` 在嵌套容器中的行为机制
华为·harmonyos
贾伟康3 小时前
【补能雷达 Skill|20】项目复盘与升级路线:从 Web Demo 到真正的车主补能助手
harmonyos·ai智能体·高德开放平台·高德skill
国服第二切图仔7 小时前
HarmonyOS APP《画伴梦工厂》开发第38篇-自适应布局API实战——adaptiveLayout模块
华为·harmonyos
特立独行的猫A7 小时前
HarmonyOS鸿蒙原生包HNP全解析:从规范到实战的完整指南
harmonyos
nashane11 小时前
HarmonyOS 6商城开发学习:剪贴板权限频繁弹窗的根治——从“自动嗅探“改为“用户主动触发“模型
华为·harmonyos
国服第二切图仔11 小时前
HarmonyOS APP《画伴梦工厂》开发第37篇-GridRow-GridCol——响应式网格布局
华为·harmonyos
痕忆丶11 小时前
openharmony开发基础之5.0.1版本文件管理器复制粘贴框架调用流程
harmonyos
国服第二切图仔12 小时前
HarmonyOS APP《画伴梦工厂》开发第31篇-语音识别实战——SpeechRecognitionEngine+AudioCapturer
语音识别·xcode·harmonyos