HarmonyOS 鸿蒙Next List第一个可见的ListItem
HarmonyOS 鸿蒙Next List第一个可见的ListItem
List怎么获取第一个可见的ListItem,是否有相关demo
2 回复
参考:
[@Entry](/user/Entry)
[@Component](/user/Component)
struct ListExample {
private arr: (number | string)[] =
[0, '概览', '周边', 3, 4, '房源', 6, 7, '小区', 9, 10, 11, 12, 13, 14, 15]
private tabArr: string[] = ['概览', '周边', '房源', '小区']
[@State](/user/State) currentIndex: number = 0
build() {
Column() {
Row() {
ForEach(this.tabArr, (item: string, index: number) => {
Text(item)
.fontColor(this.currentIndex === index ? Color.Blue : '')
})
}
.width('100%')
.padding({ left: 20 })
.alignItems(VerticalAlign.Top)
List({ space: 20, initialIndex: 0 }) {
ForEach(this.arr, (item: number) => {
ListItem() {
Text('' + item)
.width('100%')
.height(100)
.fontSize(16)
.textAlign(TextAlign.Center)
.borderRadius(10)
.backgroundColor(0xFFFFFF)
}
}, (item: string) => item)
}
.listDirection(Axis.Vertical) // 排列方向
.scrollBar(BarState.Off)
.friction(0.6)
.divider({ strokeWidth: 2, color: 0xFFFFFF, startMargin: 20, endMargin: 20 }) // 每行之间的分界线
.edgeEffect(EdgeEffect.Spring) // 边缘效果设置为Spring
.onScrollIndex((firstIndex: number, lastIndex: number, centerIndex: number) => {
console.info('first' + firstIndex)
console.info('last' + lastIndex)
console.info('center' + centerIndex)
if (firstIndex == 1) {
this.currentIndex = 0
}
else if (firstIndex == 2) {
this.currentIndex = 1
}
else if (firstIndex == 5) {
this.currentIndex = 2
}
else if (firstIndex == 8) {
this.currentIndex = 3
}
})
.width('90%')
}.width('100%').height('100%').backgroundColor(0xDCDCDC).padding({ top: 5 })
}
}
更多关于HarmonyOS 鸿蒙Next List第一个可见的ListItem的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html