HarmonyOS 滚动列表问题?

页面有两个列表,在滚动一个列表的时候另一个列表需要跟着同时滚动,碰到的问题是进入页面之后,滑动一个列表时另一个列表可以跟着滚动,反过来就不行了(滚动有延迟)。

阅读 597
1 个回答

参考demo:

@Entry
@Component
struct ListDragTest {
  @State arr: string[] = [];
  private leftListScroller: ListScroller = new ListScroller()
  private rightListScroller: ListScroller = new ListScroller()

  aboutToAppear() {
    for (let i = 1; i <= 15; i++) {
      this.arr.push("item_" + i);
    }
  }
  @Builder textItem(content: string) {
    Text(content)
      .width('100%')
      .height(150)
      .textAlign(TextAlign.Center)
  }
  build() {
    Row() {
      List({ space: 10, scroller: this.leftListScroller }) {
        ForEach(this.arr, (item: string) => {
          ListItem() {
            this.textItem(item)
          }
        })
      }
      .onScrollFrameBegin((offset: number, state: ScrollState) => {
        this.rightListScroller.scrollBy(0, offset)
        return {offsetRemain: offset}
      })
      .layoutWeight(1)
      .height('100%')
      List({ space: 10, scroller: this.rightListScroller }) {
        ForEach(this.arr, (item: string) => {
          ListItem() {
            this.textItem(item)
          }
        })
      }
      .onScrollFrameBegin((offset: number, state: ScrollState) => {
        this.leftListScroller.scrollBy(0, offset)
        return {offsetRemain: offset}
      })
      .layoutWeight(1)
      .height('100%')
    }.width('100%').margin({ top: 5 })

  }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进