根据focusIndex控制当前选中标签的背景色。@Entry
@Component
struct CityList {
private listScroller: Scroller = new Scroller();
@State focusIndex: number = 0;
allListString: string[] = ['111', '222', '333', '444', '555', '666', '777', '888', '999'];
@Builder
child(tabName: string, tabIndex: number) {
Column() {
Text(tabName)
.fontSize(18)
.padding(4);
}
.borderRadius(3)
.onClick(() => {
this.focusIndex = tabIndex;
this.listScroller.scrollToIndex(tabIndex, true, ScrollAlign.CENTER);
})
.margin({ left: tabIndex === 0 ? 20 : 4, right: tabIndex === this.allListString.length - 1 ? 20 : 4 })
.backgroundColor(tabIndex === this.focusIndex ? '#0088FF' : '#F8F9F7');
}
build() {
List({ scroller: this.listScroller }) {
ForEach(this.allListString, (item: string, index: number) => {
ListItem() {
this.child(item, index);
};
}, (item: string) => item);
}
.scrollBar(BarState.Off)
.listDirection(Axis.Horizontal)
.backgroundColor('#FFF1F3')
.alignListItem(ListItemAlign.Center)
.height(60)
.margin({ top: 100 });
}
}