【鸿蒙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)
  }
}
相关推荐
用户5951433221772 小时前
HarmonyOS应用开发之滚动容器Scroll
harmonyos
用户5951433221772 小时前
HarmonyOS应用开发之瀑布流、上拉加载、无限滚动一文搞定
harmonyos
用户5951433221772 小时前
鸿蒙应用开发之@Builder自定义构建函数:值传递与引用传递与UI更新
harmonyos
不爱吃糖的程序媛4 小时前
Flutter 开发的鸿蒙AtomGit OAuth 授权应用
华为·harmonyos
xq95279 小时前
编程之路 2025年终总结 ,勇往直前 再战江湖
harmonyos
不爱吃糖的程序媛10 小时前
鸿蒙PC命令行开发 macOS 上解决 pkg-config 命令未安装的问题
macos·华为·harmonyos
二流小码农11 小时前
鸿蒙开发:自定义一个圆形动画菜单
android·ios·harmonyos
yumgpkpm12 小时前
Cloudera CDP7、CDH5、CDH6 在华为鲲鹏 ARM 麒麟KylinOS做到无缝切换平缓迁移过程
大数据·arm开发·华为·flink·spark·kafka·cloudera
不爱吃糖的程序媛12 小时前
解决鸿蒙PC命令行编译 macOS 上 cp 命令参数冲突问题
macos·harmonyos·策略模式
不爱吃糖的程序媛12 小时前
OpenHarmony PC 第三方 C/C++ 库适配完整指南
c语言·c++·harmonyos