HarmonyOS 应用可滚动的复杂首页如何实现,List ?Scroll?

应用首页包含多个部分,需要整体滚动,单独使用List,无法区分不同类型的布局;单独使用Scroll,由于列表内容较多,又担心性能问题;Scroll嵌套List又存在滑动问题。请问该场景该如何实现?

HarmonyOS
17h前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
fox280

长列表性能可参考文档:https://developer.huawei.com/consumer/cn/doc/best-practices-V5/bpta-best-practices-long-list-V5

可参考:

@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%")
      }.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)
    }
  }
}
分享
微博
QQ
微信
回复
14h前
相关问题
HarmonyOS scrolllist滚动冲突
435浏览 • 1回复 待解决
如何实现scrolllist单边回弹效果
577浏览 • 1回复 待解决
如何获取List组件滚动滚动距离
2599浏览 • 1回复 待解决
HarmonyOS Scroll组件滚动控制
93浏览 • 1回复 待解决
HarmonyOS scroll滚动问题
33浏览 • 1回复 待解决