import { HashMap } from '@kit.ArkTS'
interface ListData {
title: string;
projects: string[];
}
@Entry
@Component
struct Index {
private letterList: string[] = []
private letterPositionMap: HashMap<string, number> = new HashMap()
@State textFontSizeArray: Length[] = []
@State textFontColorArray: ResourceColor[] = []
@State letterSelect: string | undefined = undefined
private timeoutId: number = 0
private scroller: Scroller = new Scroller()
private listData: ListData[] = [
{
title: 'A',
projects: ['安大同学', '安少同学', '安1同学', '安2同学', '安3同学']
},
{
title: 'B',
projects: ['包1同学', '包2同学', '包3同学', '包4同学', '包5同学', '包6同学', '包7同学', '包8同学', '包9同学']
},
{
title: 'C',
projects: ['蔡1同学', '蔡2同学', '蔡3同学', '蔡4同学', '蔡5同学', '蔡6同学', '蔡7同学', '蔡8同学', '蔡9同学']
},
{
title: 'D',
projects: ['杜1同学', '杜2同学', '杜3同学', '杜4同学', '杜5同学', '杜6同学']
},
{
title: 'F',
projects: ['范1同学', '范2同学', '范3同学', '范4同学', '范5同学', '范6同学', '范7同学']
},
{
title: 'L',
projects: ['李大同学', '李2同学', '李3同学', '李4同学', '李5同学', '李6同学']
},
{
title: 'M',
projects: ['马大同学', '马2同学', '马3同学', '马4同学', '马5同学', '马6同学']
}
]
aboutToAppear(): void {
for (let i = 65; i <= 90; i++) {
this.letterList.push(String.fromCharCode(i))
this.textFontSizeArray.push(14)
this.textFontColorArray.push("#666666")
}
}
@Builder
itemHead(text: string) {
Text(text)
.fontSize(16)
.backgroundColor("#f2f6f9")
.width('100%')
.height(40)
.fontWeight(FontWeight.Bold)
.padding({ left: 10 })
}
build() {
RelativeContainer() {
List({ space: 10, scroller: this.scroller }) {
ForEach(this.listData, (item: ListData) => {
ListItemGroup({ header: this.itemHead(item.title) }) {
ForEach(item.projects, (project: string) => {
ListItem() {
Text(project)
.width('100%')
.height(50)
.fontSize(18)
.textAlign(TextAlign.Center)
}
}, (item: string) => item)
}
.divider({ strokeWidth: 1, color: "#f2f6f9" })
})
}
.width('100%')
.height("100%")
.sticky(StickyStyle.Header)
.scrollBar(BarState.Off)
.onScrollIndex((start: number) => {
let title = this.listData[start].title
let latterPosition = this.letterList.indexOf(title)
this.latterChange(latterPosition)
})
List({ space: 5 }) {
ForEach(this.letterList, (item: string, index: number) => {
ListItem() {
Text(item)
.fontColor(this.textFontColorArray[index])
.fontSize(this.textFontSizeArray[index])
}.onAreaChange((_: Area, newValue: Area) => {
this.letterPositionMap.set(item, Number(newValue.position.y) + Number(newValue.height))
})
})
}
.width(30)
.backgroundColor("#f2f6f9")
.margin({ right: 10 })
.padding({ top: 10, bottom: 10 })
.borderRadius(30)
.alignListItem(ListItemAlign.Center)
.alignRules({
right: { anchor: "__container__", align: HorizontalAlign.End },
center: { anchor: "__container__", align: VerticalAlign.Center },
})
.onTouch((event) => {
switch (event.type) {
case TouchType.Down:
case TouchType.Move:
this.changeStatus(event.touches[0].y);
break
case TouchType.Up:
clearTimeout(this.timeoutId)
this.timeoutId = setTimeout(() => {
this.letterSelect = undefined
}, 500)
break
}
})
Text(this.letterSelect)
.visibility(this.letterSelect == undefined ? Visibility.None : Visibility.Visible)
.backgroundColor("#f2f6f9")
.fontColor("#222222")
.fontWeight(FontWeight.Bold)
.fontSize(18)
.textAlign(TextAlign.Center)
.width(50)
.height(50)
.borderRadius(50)
.alignRules({
middle: { anchor: "__container__", align: HorizontalAlign.Center },
center: { anchor: "__container__", align: VerticalAlign.Center },
})
}
.width("100%")
.height("100%")
}
changeStatus(moveY: number) {
const positions = this.letterList.map(letter => this.letterPositionMap.get(letter))
let index = 0
for (let i = 0; i < positions.length; i++) {
if (moveY < positions[i]) {
break
}
index = i + 1
}
this.letterSelect = this.letterList[index]
const scrollIndex = this.listData.findIndex(item => item.title === this.letterSelect)
if (scrollIndex !== -1) {
this.scroller.scrollToIndex(scrollIndex)
}
this.latterChange(index)
}
latterChange(index: number) {
this.textFontSizeArray.forEach((_, index) => {
this.textFontSizeArray[index] = 14
this.textFontColorArray[index] = "#666666"
})
this.textFontSizeArray[index] = 16
this.textFontColorArray[index] = "#222222"
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.
- 104.
- 105.
- 106.
- 107.
- 108.
- 109.
- 110.
- 111.
- 112.
- 113.
- 114.
- 115.
- 116.
- 117.
- 118.
- 119.
- 120.
- 121.
- 122.
- 123.
- 124.
- 125.
- 126.
- 127.
- 128.
- 129.
- 130.
- 131.
- 132.
- 133.
- 134.
- 135.
- 136.
- 137.
- 138.
- 139.
- 140.
- 141.
- 142.
- 143.
- 144.
- 145.
- 146.
- 147.
- 148.
- 149.
- 150.
- 151.
- 152.
- 153.
- 154.
- 155.
- 156.
- 157.
- 158.
- 159.
- 160.
- 161.
- 162.
- 163.
- 164.
- 165.
- 166.
- 167.
- 168.
- 169.
- 170.
- 171.
- 172.
- 173.
- 174.
- 175.
- 176.
- 177.
- 178.
- 179.
- 180.
- 181.
- 182.
- 183.
- 184.
所有评论(0)