HarmonyOS 如何知道List的组件滑倒底了,或者到达某个位置了?

如题:HarmonyOS 如何知道List的组件滑倒底了,或者到达某个位置了?

阅读 554
1 个回答

可以使用onReachEnd回调来判断列表是否到底末尾位置,参考链接:

https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-container-list-V5\#onreachend

也可以使用currentOffset获取具体的位置偏移量,参考示例

@Entry
@Component
struct ListExample {
  private arr: number[] = []
  private scrollerForList: Scroller = new Scroller()

  aboutToAppear() {
    for (let i = 0; i < 20; i++) {
      this.arr.push(i)
    }
  }
  build() {
    Column() {
      Row() {
        List({ space: 20,  scroller: this.scrollerForList }) {
          ForEach(this.arr, (item: number) => {
            ListItem() {
              Text('' + item)
              .width('100%').height(100).fontSize(16)
                .textAlign(TextAlign.Center)
            }
            .borderRadius(10).backgroundColor(0xFFFFFF)
            .width('100%')
            .height('20%')
          }, (item: number) => JSON.stringify(item))
        }
        .onWillScroll(()=>{
          console.log(this.scrollerForList.currentOffset().yOffset+'this.scrollerForList.currentOffset().yOffset')
        })
        .listDirection(Axis.Vertical)
        .height('100%')
        .width('100%')
          .borderRadius(10)
          .backgroundColor(0xDCDCDC)
      }
      .width('100%')
      .height('100%')
      .backgroundColor(0xDCDCDC)
        .padding({ top: 10 })
    }
  }
}