使用List和Scroller嵌套实现的长列表使用scrollToIndex()方法跳转跨越过多项时性能低下,如何优化?
@Entry @ComponentV2 export struct ListTest { private readonly list: string[] = Array .from({ length: 2000 }, (_: number, i: number) => i + 1) .map((i: number) => `index: ${i}`); private readonly scroller: Scroller = new Scroller(); build() { Column() { Row({ space: 10 }) { Button('scroll to top').onClick(() => this.scroller.scrollToIndex(0, true)).fontSize(13); Button('scroll to bottom').onClick(() => this.scroller.scrollToIndex(this.list.length - 1, true)).fontSize(13); Button('jump 1000').onClick(() => this.scroller.scrollToIndex(1000, false)).fontSize(13); } .width('100%') .margin({ left: 25 }); List({ scroller: this.scroller, }) { Repeat(this.list) .key((item: string) => item) .virtualScroll({ totalCount: this.list.length }) .templateId(() => '1') .template('1', (repeatItem: RepeatItem<string>) => { ListItem() { Text(repeatItem.item) .width('100%') .height(50) .padding(5) .textAlign(TextAlign.Center); }; }) .each((repeatItem: RepeatItem<string>) => { ListItem() { Text(repeatItem.item) .width('100%') .height(50) .padding(5); }; }); } .width('100%') .layoutWeight(1); } .backgroundColor(Color.White); } }
以纵向滚动的5000个元素的长列表,调用scroller反复滚动到列表顶部和底部为例。
@Entry @ComponentV2 export struct virtrulScroll { private itemHeight: number = 50; private readonly list: string[] = Array .from({ length: 5000 }, (_: number, i: number) => i + 1) .map((i: number) => `index: ${i}`); private readonly scroller: Scroller = new Scroller(); build() { Column() { Row({ space: 10 }) { Button('scroll to top') .fontSize(13) .onClick(() => { // 用currentOffset().yOffset获取当前滑动偏移量除以单项高度获得index做判断,离目标位置超过200项则以关闭动效先滚动到目标位置附近 if (this.scroller.currentOffset().yOffset / this.itemHeight >= 200) { this.scroller.scrollToIndex(200, false); } this.scroller.scrollToIndex(0, true); }); Button('scroll to bottom') .fontSize(13) .onClick(() => { if (this.scroller.currentOffset().yOffset / this.itemHeight <= this.list.length - 200) { this.scroller.scrollToIndex(this.list.length - 200, false); } this.scroller.scrollToIndex(this.list.length - 1, true); }); Button('jump 1000') .fontSize(13) .onClick(() => { console.info('currentoffset: ', this.scroller.currentOffset().yOffset); this.scroller.scrollToIndex(1000, false); }); }; List({ scroller: this.scroller, }) { Repeat(this.list) .key((item: string) => item) .virtualScroll({ totalCount: this.list.length }) .templateId(() => '1') .template('1', (repeatItem: RepeatItem<string>) => { ListItem() { Text(repeatItem.item) .width('100%') .height(this.itemHeight) .padding(5) .textAlign(TextAlign.Center); }; }) .each((repeatItem: RepeatItem<string>) => { ListItem() { Text(repeatItem.item) .width('100%') .height(this.itemHeight) .padding(5); }; }); } .width('100%') .height(200) .layoutWeight(1); }.backgroundColor(Color.White); } }