原理说明:
@Entry @Component struct StickyHeaderExample { private arr: number[] = [0, 1]; @Builder CustomHeader() { Text('分组标题') .height(50) .width('100%') .backgroundColor(Color.Gray) .onAreaChange((oldValue: Area, newValue: Area) => { if (oldValue.position.y === 0 && newValue.position.y === 0) { console.info('没吸顶'); } else { console.info(`吸顶了 ${oldValue.position.y}`); } }); } build() { Column() { List({ space: 10 }) { ForEach(this.arr, (item: number) => { ListItem() { Text(item.toString()) .width('100%') .height(50) .fontSize(16) .textAlign(TextAlign.Center) .borderRadius(10) .backgroundColor(0xFFFFFF); }; }, (item: string) => item); ListItemGroup({ header: this.CustomHeader, space: 10 }) { ForEach(Array.from({ length: 20 }), (item: void, index: number) => { ListItem() { Text(`列表项 ${index}`) .height(80) .width('100%') .backgroundColor('#FFF'); }; }); }; } .sticky(StickyStyle.Header) .width('100%') .height('100%'); }; } }