参考示例如下:class Contacts { constructor(index: string, names: string[]) { this.index = index this.names = names } index: string names: string[] } @Entry @Component struct Index { @State contacts: Contacts[] = [ new Contacts('A', ['HarmonyOS', 'HarmonyOS', 'ArkTS', 'ArkTS', 'ETS', 'ArkUI']), new Contacts('B', ['HarmonyOS', 'HarmonyOS', 'ArkTS', 'ArkTS', 'ETS', 'ArkUI']), new Contacts('C', ['HarmonyOS', 'HarmonyOS', 'ArkTS', 'ArkTS', 'ETS', 'ArkUI']), new Contacts('D', ['HarmonyOS', 'HarmonyOS', 'ArkTS', 'ArkTS', 'ETS', 'ArkUI']), new Contacts('E', ['HarmonyOS', 'HarmonyOS', 'ArkTS', 'ArkTS', 'ETS', 'ArkUI']), new Contacts('F', ['HarmonyOS', 'HarmonyOS', 'ArkTS', 'ArkTS', 'ETS', 'ArkUI']), ] scroller: Scroller = new Scroller value: string[] = ['A', 'B', 'C', 'D', 'E', 'F'] @State flag: number = 0 preIndex: number = 0 build() { Row() { List({ space: 20, initialIndex: 0, scroller: this.scroller }) { ForEach(this.contacts, (value: Contacts) => { ListItem() { Column() { Text('' + value.index) .fontSize(30) .textAlign(TextAlign.Center) .backgroundColor('#ffffff') .margin(30) ForEach(value.names, (names: string) => { Column({ space: 10 }) { Text('' + value.names) .fontSize(20) .textAlign(TextAlign.Center) .backgroundColor('#ffffff') .margin(10) } }) } } }) } .width('80%') .backgroundColor(Color.Pink) .listDirection(Axis.Vertical) .onScrollIndex((firstIndex: number, lastIndex: number) => { this.flag = firstIndex }) AlphabetIndexer({ arrayValue: this.value, selected: this.flag }) .width('20%') .font({ size: 20, weight: FontWeight.Bold }) .selectedColor(Color.Gray) .usingPopup(true) .selectedFont({ size: 28, weight: FontWeight.Bolder }) .itemSize(30) .alignStyle(IndexerAlign.Left) .onSelect((index: number) => { for (let i = 0; i < this.contacts.length; i++) { if (this.contacts[i].index.toString() === this.value[index]) { this.scroller.scrollToIndex(i) this.preIndex = index return } } this.flag = this.preIndex }) } } }
参考示例如下: