HarmonyOS scroll组件可以同时设置垂直和水平两个方向滑动吗

比如类似浏览大图的场景,可以水平垂直方向都滑动。

HarmonyOS
2024-12-26 15:35:48
779浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
fox280

scroll组件从api9以后不支持两个方向同时滑动,可通过组件嵌套实现同时滑动:

@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]
  // table header
  @Builder
  tabHeader() {
    Stack() {
      Row() {
        Text('1111111').fontSize(20).width(100).height(100)
        Blank()
        Text('1111110').fontSize(20).width(100).height(100)
      }.width('100%').backgroundColor(Color.Gray)
    }
  }
  build() {
    List() {
      ListItemGroup({ header: this.tabHeader() }) {
        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.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
分享
微博
QQ
微信
回复
2024-12-26 16:47:59


相关问题
HarmonyOS 怎么让两个list同时滚动
570浏览 • 1回复 待解决
HarmonyOS 两个同级的组件问题
1104浏览 • 1回复 待解决
关于Image组件两个问题
1002浏览 • 1回复 待解决
鸿蒙支持两个SPP server??? 20
1909浏览 • 1回复 待解决