HarmonyOS 如何让网格显示的控件可以同时左右和垂直滚动

网格显示的布局,水平和垂直都超过了屏幕,需要可以在水平方向和垂直方向都能进行滑动显示超出屏幕的部分。

HarmonyOS
2024-12-25 13:27:29
1477浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zxjiu

参考示例:

@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(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)
  }
}

@Component
struct tableRow {
  build() {
    Row() {
      Text('TabContent').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('Navigation').fontSize(20).width(100).height(100)
        Text('NavRouter').fontSize(20).width(100).height(100)
        Text('NavDestination').fontSize(20).width(100).height(100)
        Text('Navigator').fontSize(20).width(100).height(100)
        Text('Stepper').fontSize(20).width(100).height(100)
        Text('StepperItem').fontSize(20).width(100).height(100)
        Text('Tabs').fontSize(20).width(100).height(100)
        Text('TabContent').fontSize(20).width(100).height(100)
        Text('CheckboxGroup').fontSize(20).width(100).height(100)
        Text('CalendarPicker').fontSize(20).width(100).height(100)
      }
    }.scrollable(ScrollDirection.Horizontal)
    .scrollBar(BarState.Off)
    .edgeEffect(EdgeEffect.None)
    .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-25 16:20:31


相关问题
HarmonyOS 怎么两个list同时滚动
711浏览 • 1回复 待解决
HarmonyOS 控件高度随滚动变化
907浏览 • 1回复 待解决
如果分割线垂直起来
6523浏览 • 1回复 待解决