HarmonyOS鸿蒙Next ArkUI组件Scroll、Tabs、List嵌套时,给List添加nestedScroll无法解决滚动冲突

HarmonyOS鸿蒙Next ArkUI组件Scroll、Tabs、List嵌套时,给List添加nestedScroll无法解决滚动冲突 Scrol->Tabs->List嵌套时,给List添加nestedScroll无法解决滚动冲突

Scroll包含Tabs,Tabs中的TabContent包含List,此时给List增加:

.nestedScroll({ scrollForward: NestedScrollMode.PARENT_FIRST, scrollBackward: NestedScrollMode.SELF_FIRST })


期望Scroll和List同时只有一个滚动,但是现在是两个都可以响应滚动

更多关于HarmonyOS鸿蒙Next ArkUI组件Scroll、Tabs、List嵌套时,给List添加nestedScroll无法解决滚动冲突的实战教程也可以访问 https://www.itying.com/category-93-b0.html

3 回复

参考demo:

@Entry
@Component
struct page240429110107050 {
  @State message: string = 'Hello World'
  @State arr: number[] = []
  private touchDown: boolean = false;
  private listTouchDown: boolean = false;
  private scrolling: boolean = false;
  private scroller: Scroller = new Scroller()
  private listScroller: Scroller = new Scroller()

  @Styles
  listCard() {
    .backgroundColor(Color.White)
    .height(72)
    .width("100%")
    .borderRadius(12)
  }

  build() {
    Scroll(this.scroller) {
      Column() {
        Text("Scroll Area")
          .width("100%")
          .height("400")
          .backgroundColor('#0080DC')
          .textAlign(TextAlign.Center)
        Tabs({ barPosition: BarPosition.Start }) {
          TabContent() {
            List({ space: 10, scroller: this.listScroller }) {
              ForEach(this.arr, (item: number) => {
                ListItem() {
                  Text("item" + item)
                    .fontSize(16)
                }.listCard()
              }, (item: number) => item.toString())
            }.width("100%")
            .edgeEffect(EdgeEffect.Spring)
            .nestedScroll({
              scrollForward: NestedScrollMode.PARENT_FIRST,
              scrollBackward: NestedScrollMode.SELF_FIRST
            })
            .onTouch((event: TouchEvent) => {
              if (event.type == TouchType.Down) {
                this.listTouchDown = true;
              } else if (event.type == TouchType.Up) {
                this.listTouchDown = false;
              }
            })
          }.tabBar("Tab1")

          TabContent() {
          }.tabBar("Tab2")
        }
        .vertical(false)
        .height("100%")

        // .height(300)

        // Column()
        // .width('100%')
        // .height(1000)
      }.width("100%")
    }
    .onTouch((event: TouchEvent) => {
      if (event.type == TouchType.Down) {
        this.touchDown = true;
      } else if (event.type == TouchType.Up) {
        this.touchDown = false;
      }
    })
    .onScrollFrameBegin((offset: number, state: ScrollState) => {
      if (this.scrolling && offset > 0) {
        let yOffset: number = this.scroller.currentOffset().yOffset
        if (yOffset >= 400) {
          this.listScroller.scrollBy(0, offset)
          return { offsetRemain: 0 }
        } else if (yOffset + offset > 400) {
          this.listScroller.scrollBy(0, yOffset + offset - 400)
          return { offsetRemain: 400 - yOffset }
        }
      }
      return { offsetRemain: offset }
    })
    .onScrollStart(() => {
      if (this.touchDown && !this.listTouchDown) {
        this.scrolling = true;
      }
    })
    .onScrollStop(() => {
      this.scrolling = false;
    })
    .edgeEffect(EdgeEffect.Spring)
    .backgroundColor('#DCDCDC')
    .scrollBar(BarState.Off)
    .width('100%')
    .height('100%')
  }

  aboutToAppear() {
    for (let i = 0; i < 30; i++) {
      this.arr.push(i)
    }
  }
}

更多关于HarmonyOS鸿蒙Next ArkUI组件Scroll、Tabs、List嵌套时,给List添加nestedScroll无法解决滚动冲突的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙Next中,ArkUI组件的嵌套使用(如Scroll、Tabs、List)时,滚动冲突是一个常见问题。即使给List添加了nestedScroll属性,也无法完全解决滚动冲突。nestedScroll属性的作用是允许嵌套滚动,但在复杂的嵌套结构中,父组件和子组件的滚动事件可能会相互干扰,导致滚动行为不符合预期。

具体来说,nestedScroll属性通常用于处理父子组件之间的滚动协调,但在嵌套层级较深或组件类型不同的情况下(如Scroll与List、Tabs与List),滚动事件的传递和处理可能会出现问题。例如,当List在Tabs或Scroll组件中嵌套时,List的滚动可能会被父组件拦截,导致无法正常滚动。

在这种情况下,nestedScroll属性可能无法完全解决滚动冲突,因为滚动事件的优先级和传递机制在复杂嵌套结构中难以自动协调。需要进一步通过组件的事件处理或滚动控制逻辑来手动协调滚动行为。

在HarmonyOS鸿蒙Next中,ArkUI组件的嵌套滚动问题较为常见。当Scroll、Tabs、List组件嵌套使用时,单纯给List添加nestedScroll属性可能无法完全解决滚动冲突。建议尝试以下解决方案:

  1. 使用ScrollscrollEnabled属性:在父容器(如Scroll或Tabs)滚动时,动态控制子组件(如List)的滚动状态。

  2. 自定义滚动事件处理:通过监听滚动事件,判断当前滚动方向,手动控制子组件的滚动行为。

  3. 合理布局:避免过多嵌套,尽量简化组件结构,减少滚动的复杂性。

如果问题依旧,建议查阅官方文档或社区论坛,获取更多技术支持。

回到顶部