list组件无高度后scroll失效?

评论区List组件没有设置组件高度,导致一些定位方法比如scrollToIndex没有作用,无法跳到对应位置,怎么解决呢?

阅读 586
1 个回答

具体可参考以下demo实现:

@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: 10, initialIndex: 0, scroller: this.scrollerForList }) {
          ForEach(this.arr, (item: number) => {
            ListItem() {
              Text('' + item)
                .width('100%').height(50).fontSize(16)
                .textAlign(TextAlign.Center)
            }
            .borderRadius(10).backgroundColor(0xFFFFFF)
          }, (item: number) => JSON.stringify(item))
        }
        .chainAnimation(true)
        .borderRadius(10)
        .backgroundColor(0xDCDCDC)
      }
      .width('100%')
      .height(300)
      .backgroundColor(0xDCDCDC)
      Button() { Text('scrollToIndex (0)') }.onClick(()=>{
        this.scrollerForList.scrollToIndex(0,true)
      }).height('10%').width('60%')
      Button() { Text('scrollToIndex (19)') }.onClick(()=>{
        this.scrollerForList.scrollToIndex(19,true)
      }).height('10%').width('60%')
    }
  }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题