在Navigation中定义工具栏,点击高亮标签时,标签会在高亮与不高亮之间切换,无法实现多次点击同一个页签,页签常亮的功能。问题代码如下:
@Entry @Component struct MethodOne { @Provide('navPathStack') navPathStack: NavPathStack = new NavPathStack(); @State currentTabIndex: number = 0; build() { Column() { Navigation() { Text('选中了tab' + this.currentTabIndex); } .width('100%') .navBarWidth('100%') .hideBackButton(true) .hideTitleBar(true) .hideToolBar(false) .mode(NavigationMode.Stack) .toolbarConfiguration(MenuList.GetDefaultMenuNavBarList(this.currentTabIndex, (index) => { this.currentTabIndex = index; }), { backgroundColor: Color.White, backgroundBlurStyle: BlurStyle.Regular }) .height('100%') .width('100%') .backgroundColor('#F1F3F5'); }; } } class MenuList { public static GetDefaultMenuList(): MenuEntity[] { return [ new MenuEntity('消息', 0, $r('app.media.nav_bar_btn_msg'), $r('app.media.nav_bar_btn_msg_selected')), new MenuEntity('待办', 1, $r('app.media.nav_bar_btn_todo'), $r('app.media.nav_bar_btn_todo_selected')), new MenuEntity('工作台', 2, $r('app.media.nav_bar_btn_work'), $r('app.media.nav_bar_btn_work_selected')), new MenuEntity('通讯录', 3, $r('app.media.nav_bar_btn_addressbook'), $r('app.media.nav_bar_btn_addressbook_selected')), new MenuEntity('我的', 4, $r('app.media.nav_bar_btn_me'), $r('app.media.nav_bar_btn_me_selected')), ]; } public static GetDefaultMenuNavBarList(currentIndex: number, menuOnClick?: (index: number) => void): Array<ToolbarItem> { let toolbarList: Array<ToolbarItem> = []; for (let i = 0; i < MenuList.GetDefaultMenuList().length; i++) { let menuEntity: MenuEntity = MenuList.GetDefaultMenuList()[i]; toolbarList.push({ value: menuEntity.name, icon: menuEntity.menuIcon, activeIcon: menuEntity.focusMenuIcon, status: currentIndex == i ? ToolbarItemStatus.ACTIVE : ToolbarItemStatus.NORMAL, action: () => { if (menuOnClick) { menuOnClick(menuEntity.index); } } }); } return toolbarList; } }; class MenuEntity { menuIcon?: Resource = $r('app.media.startIcon'); focusMenuIcon?: Resource = $r('app.media.startIcon'); name: string | Resource = 'text'; index: number = 0; isShow?: boolean = true; gotoPage: string = ''; constructor(name: string | Resource, index: number, menuIcon?: Resource, focusMenuIcon?: Resource, isShow?: boolean) { this.menuIcon = menuIcon; this.focusMenuIcon = focusMenuIcon; this.name = name; this.index = index; this.isShow = isShow; } };
问题现象:

Tabs的TabBar和Navigation工具栏点击效果区分:
标签类型 | 实现类型 | 常用效果 |
|---|---|---|
Navigation工具栏 | Array<ToolbarItem> 或 CustomBuilder | 作为工具栏时,通过点击实现工具栏开启与关闭,所以需要实现多次点击时高亮与不高亮循环显示。 |
Tabs标签 | ComponentContent、SubTabBarStyle、BottomTabBarStyle、string、Resource、CustomBuilder、TabBarOptions | 页签的切换,需要实现多次点击同一个标签时该标签一直高亮的效果。 |
public static GetDefaultMenuNavBarList(currentIndex: number, menuOnClick?: (index: number) => void): Array<ToolbarItem> { console.info('GetDefaultMenuNavBarList执行了一次。') // 监听函数执行。 let toolbarList: Array<ToolbarItem> = []; for (let i = 0; i < MenuList.GetDefaultMenuList().length; i++) { let menuEntity: MenuEntity = MenuList.GetDefaultMenuList()[i]; toolbarList.push({ value: menuEntity.name, icon: menuEntity.menuIcon, activeIcon: menuEntity.focusMenuIcon, status: currentIndex === i ? ToolbarItemStatus.ACTIVE : ToolbarItemStatus.NORMAL, action: () => { console.info('点击了一次。') // 监听点击次数。 menuOnClick ? menuOnClick(menuEntity.index) : undefined } }) } return toolbarList; }
验证效果如下:

第一次点击后的后续点击,由于状态变量this.currentTabIndex没有变化,导致toolbarConfiguration属性没有刷新,实现的是标签本身的ToolbarItemStatus.ACTIVE逻辑,所以导致多次点击无法一直常亮。
参考代码实现逻辑给出以下两种Navigation实现TabBar点击效果:
.toolbarConfiguration(MenuList.GetDefaultMenuNavBarList(this.currentTabIndex, (index) => { this.currentTabIndex = -1; this.currentTabIndex = index; }), { backgroundColor: Color.White, backgroundBlurStyle: BlurStyle.Regular })
示例完整demo如下:
@Entry @Component struct MethodOne { @Provide('navPathStack') navPathStack: NavPathStack = new NavPathStack(); @State currentTabIndex: number = 0; build() { Column() { Navigation() { Text('选中了tab' + this.currentTabIndex); } .width('100%') .navBarWidth('100%') .hideBackButton(true) .hideTitleBar(true) .hideToolBar(false) .mode(NavigationMode.Stack) .toolbarConfiguration(MenuList.GetDefaultMenuNavBarList(this.currentTabIndex, (index) => { this.currentTabIndex = -1; this.currentTabIndex = index; }), { backgroundColor: Color.White, backgroundBlurStyle: BlurStyle.Regular }) .height('100%') .width('100%') .backgroundColor('#F1F3F5'); }; } } class MenuList { public static GetDefaultMenuList(): MenuEntity[] { return [ new MenuEntity('消息', 0, $r('app.media.nav_bar_btn_msg'), $r('app.media.nav_bar_btn_msg_selected')), new MenuEntity('代办', 1, $r('app.media.nav_bar_btn_todo'), $r('app.media.nav_bar_btn_todo_selected')), new MenuEntity('工作台', 2, $r('app.media.nav_bar_btn_work'), $r('app.media.nav_bar_btn_work_selected')), new MenuEntity('通讯录', 3, $r('app.media.nav_bar_btn_addressbook'), $r('app.media.nav_bar_btn_addressbook_selected')), new MenuEntity('我的', 4, $r('app.media.nav_bar_btn_me'), $r('app.media.nav_bar_btn_me_selected')), ]; } public static GetDefaultMenuNavBarList(currentIndex: number, menuOnClick?: (index: number) => void): Array<ToolbarItem> { let toolbarList: Array<ToolbarItem> = []; for (let i = 0; i < MenuList.GetDefaultMenuList().length; i++) { let menuEntity: MenuEntity = MenuList.GetDefaultMenuList()[i]; toolbarList.push({ value: menuEntity.name, icon: menuEntity.menuIcon, activeIcon: menuEntity.focusMenuIcon, status: currentIndex == i ? ToolbarItemStatus.ACTIVE : ToolbarItemStatus.NORMAL, action: () => { if (menuOnClick) { menuOnClick(menuEntity.index); } } }); } return toolbarList; } }; class MenuEntity { menuIcon?: Resource = $r('app.media.startIcon'); focusMenuIcon?: Resource = $r('app.media.startIcon'); name: string | Resource = 'text'; index: number = 0; isShow?: boolean = true; gotoPage: string = ''; constructor(name: string | Resource, index: number, menuIcon?: Resource, focusMenuIcon?: Resource, isShow?: boolean) { this.menuIcon = menuIcon; this.focusMenuIcon = focusMenuIcon; this.name = name; this.index = index; this.isShow = isShow; } };
方式二:禁用ToolbarItemStatus.ACTIVE逻辑。
@Entry @Component struct MethodTwo { @State currentTabIndex: number = 0; // 标签信息。 @State toolList: Array<Array<string>> = [ ['menuItem1', 'app.media.background', 'app.media.startIcon'], ['menuItem2', 'app.media.background', 'app.media.startIcon'], ['menuItem3', 'app.media.background', 'app.media.startIcon'], ['menuItem4', 'app.media.background', 'app.media.startIcon'], ]; // 设置函数遍历标签信息并返回Array<ToolbarItem>类型数组。 GetDefaultMenuNavBarList(currentIndex: number, menuOnClick?: (index: number) => void): Array<ToolbarItem> { let toolbarList: Array<ToolbarItem> = []; for (let i = 0; i < this.toolList.length; i++) { toolbarList.push({ value: this.toolList[i][0], icon: currentIndex === i ? $r(this.toolList[i][1]) : $r(this.toolList[i][2]), action: () => { if (menuOnClick) { menuOnClick(i); } } }); } return toolbarList; } build() { Column() { Navigation() { Text('选中了tab' + this.currentTabIndex); } .width('100%') .navBarWidth('100%') .hideBackButton(true) .hideTitleBar(true) .hideToolBar(false) .mode(NavigationMode.Stack) .toolbarConfiguration(this.GetDefaultMenuNavBarList(this.currentTabIndex, (index: number) => { this.currentTabIndex = index; }), { backgroundColor: Color.White, backgroundBlurStyle: BlurStyle.Thin }); }.height('100%') .width('100%') .backgroundColor('#F1F3F5'); } }
采用@Builder封装UI构件,通过三目运算实现点击聚焦功能。
@Entry @Component struct OptionTwo { @State currentTabIndex: number = 0; @State toolList: Array<Array<string>> = [ ['item1', 'app.media.startIcon', 'app.media.background'], ['item2', 'app.media.startIcon', 'app.media.background'], ['item3', 'app.media.startIcon', 'app.media.background'], ['item4', 'app.media.startIcon', 'app.media.background'], ]; @Builder GetDefaultMenuNavBarList() { Row() { ForEach(this.toolList, (item: Array<string>, index: number) => { Column() { Stack() { Image($r(item[1])) .height(30) .width(30) .draggable(false); Image($r(item[2])) .height(30) .width(30) .draggable(false) .visibility(this.currentTabIndex === index ? Visibility.Visible : Visibility.None); }; Text(item[0]) .fontSize(10) .width(30) .fontColor(this.currentTabIndex === index ? Color.Blue : Color.Black); } .width(70) .onClick(() => { this.currentTabIndex = index; }); }); } .width('100%') .justifyContent(FlexAlign.SpaceBetween); } build() { Column() { Navigation() { Text('选中了tab' + this.currentTabIndex); } .width('100%') .navBarWidth('100%') .hideBackButton(true) .hideTitleBar(true) .hideToolBar(false) .mode(NavigationMode.Stack) .toolbarConfiguration(this.GetDefaultMenuNavBarList(), { backgroundColor: Color.White, backgroundBlurStyle: BlurStyle.Thin }); } .height('100%') .width('100%') .backgroundColor('#F1F3F5'); } }