中国优质的IT技术网站
专业IT技术创作平台
IT职业在线教育平台
手机号登录需要选择区号,支持右侧按A-Z快速滑动,是否有提供相关示例?
微信扫码分享
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', ["xxx", "xxx", "xxx"]), new Contacts('B', ["xxx", "xxx", "xxx"]), new Contacts('C', ["xxx", "xxx", "xxx"]), new Contacts('D', ["xxx", "xxx", "xxx"]), new Contacts('E', ["xxx", "xxx"]), new Contacts('F', ["xxx", "xxx"]), new Contacts('G', ["xxx", "xxx", "xxx"]), new Contacts('H', ["xxx", "xxx", "xxx"]), ] scroller: Scroller = new Scroller() // list 滚动控制 value: string[] = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'] @State flag: number = 0 // 绑定AlphabetIndexer的index 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, (name: string) => { Column({ space: 10 }) { Text('' + name) .fontSize(20) .textAlign(TextAlign.Center) .backgroundColor('#ffffff') .margin(10) } }) } } }) } .width('80%') .backgroundColor('#fffff0') .listDirection(Axis.Vertical) .onScrollIndex((firstIndex: number, lastIndex: number) => { this.flag = firstIndex // 刷新 AlphabetIndexer }) AlphabetIndexer({ arrayValue: this.value, selected: this.flag }) .width("20%") .font({ size: 20, weight: FontWeight.Bold }) .selectedColor(Color.Blue) .selectedBackgroundColor(0xCCCCCC) .usingPopup(true)// 是否显示弹出框 .selectedFont({ size: 28, weight: FontWeight.Bolder }) .itemSize(30) .alignStyle(IndexerAlign.Left) .onSelect((index: number) => { console.info(`AlphabetIndexer 选中了 ${this.value[index]}`) for (let i = 0; i < this.contacts.length; i++) { if (this.contacts[i].index.toString() === this.value[index]) { this.preIndex = index; this.scroller.scrollToIndex(i); return; } } this.flag = this.preIndex; }) }.width("100%") } }