HarmonyOS 页面需要左右滚动和上下滚动

页面需要左右滚动和上下滚动应该怎么实现

HarmonyOS
2024-12-20 15:54:08
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
FengTianYa

参考demo:

@Entry
@Component
struct ScrollExample {
  private panOption: PanGestureOptions = new PanGestureOptions({ direction: PanDirection.Left | PanDirection.Right })
  @Provide offsetX: number | undefined = undefined;

  @Builder
  topArea() {
    tableRow().margin({right: 100})
  }

  build() {
    Column() {
      // 顶部
      this.topArea()
      // 主体
      MainArea().margin({bottom:100})
    }
    .backgroundColor(Color.Orange)
    .gesture(
      // 用于触发拖动手势事件,滑动的最小距离为5vp时拖动手势识别成功。
      PanGesture(this.panOption).onActionStart((event?: GestureEvent) => {
        this.offsetX = event?.offsetX
        console.info('gesture11: PanGesture start' + event?.offsetX)
      })
    )
  }
}
@Component
struct MainArea {
  private arr: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  build() {
    List() {
      ForEach(this.arr, (item: number) => {
        ListItem() {
          tableRow()
        }.margin({right: 100})
      }, (item: number) => item.toString())
    }.sticky(StickyStyle.Header)
  }
}
// table row
@Component
struct tableRow {
  build() {
    Row() {
      Text('111111').fontSize(20).width(100).height(100)
      ScrollComponent()
    }.width('100%')
  }
}

@Component
struct ScrollComponent {
  scroller: Scroller = new Scroller()
  @Consume @Watch('onCountUpdated') offsetX: number;

  //监听滑动距离
  onCountUpdated() {
    if (this.offsetX) {
      this.scroller.scrollTo({
        xOffset: this.scroller.currentOffset()?.xOffset - this.offsetX * 50, //滑动距离可根据实际情况设置
        yOffset: 0,
        animation: { duration: 1000 }
      })
    }
  }

  build() {
    Scroll(this.scroller) {
      Row() {
        Text('111111').fontSize(20).width(100).height(100)
        Text('111113').fontSize(20).width(100).height(100)
        Text('111114').fontSize(20).width(100).height(100)
        Text('111115').fontSize(20).width(100).height(100)
        Text('111116').fontSize(20).width(100).height(100)
        Text('111117').fontSize(20).width(100).height(100)
        Text('111118').fontSize(20).width(100).height(100)
        Text('111119').fontSize(20).width(100).height(100)
        Text('1111110').fontSize(20).width(100).height(100)
        Text('1111111').fontSize(20).width(100).height(100)
      }
    }
    .scrollable(ScrollDirection.Horizontal) // 滚动方向横向
    .scrollBar(BarState.Off) // 滚动条常驻显示
    .edgeEffect(EdgeEffect.None)
    // 禁止scroll滚动
    .enabled(false)
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
分享
微博
QQ
微信
回复
2024-12-20 18:36:38
相关问题
Text文本过长时如何实现上下滚动
1598浏览 • 1回复 待解决
页面列表嵌套滚动,实现列表吸顶
2264浏览 • 1回复 待解决
HarmonyOS scrolllist滚动冲突
1122浏览 • 1回复 待解决
自适应页面滚动如何实现
1280浏览 • 1回复 待解决
HarmonyOS swiper如何滚动到任意页面
1113浏览 • 1回复 待解决
HarmonyOS List页面如何主动停止滚动
815浏览 • 1回复 待解决
HarmonyOS 嵌套滚动冲突
868浏览 • 1回复 待解决
如何获取List组件滚动滚动的距离
3474浏览 • 1回复 待解决
HarmonyOS 滚动列表问题
594浏览 • 1回复 待解决
HarmonyOS 滚动列表问题?
725浏览 • 0回复 待解决