当前位置: 首页 > news >正文

13.ArkUI Navigation的介绍和使用

ArkUI Navigation 组件介绍与使用指南

什么是 Navigation 组件?

Navigation 是 ArkUI 中的导航组件,用于管理页面间的导航和路由。它提供了页面栈管理、导航栏定制、页面切换动画等功能,是构建多页面应用的核心组件。

Navigation 的核心概念

  1. 页面栈:采用栈结构管理页面,遵循"后进先出"原则
  2. 导航模式
    • NavigationMode.Stack:栈模式(默认)
    • NavigationMode.Split:分栏模式(平板等大屏设备)
  3. 导航栏:可自定义标题栏区域
  4. 路由:通过路径标识页面,支持参数传递

基本使用方法

简单页面导航

// 首页
@Entry
@Component
struct HomePage {build() {Navigation() {Column() {Text('首页').fontSize(30).margin(20)Button('跳转到详情页').onClick(() => {router.pushUrl({url: 'pages/DetailPage'})})}.width('100%').height('100%')}.title('首页')}
}// 详情页
@Component
struct DetailPage {build() {Column() {Text('详情页内容').fontSize(30).margin(20)Button('返回').onClick(() => {router.back()})}.width('100%').height('100%')}
}

带参数的页面导航

// 列表页
@Entry
@Component
struct ListPage {private items: string[] = ['苹果', '香蕉', '橙子', '葡萄']build() {Navigation() {Column() {ForEach(this.items, (item) => {Button(item).width('80%').margin(5).onClick(() => {router.pushUrl({url: 'pages/DetailPage',params: { name: item }})})})}}.title('水果列表')}
}// 详情页
@Component
struct DetailPage {@State name: string = ''onPageShow() {this.name = router.getParams()?.['name'] || '未知'}build() {Column() {Text(`您选择了: ${this.name}`).fontSize(30).margin(20)}}
}

高级用法

自定义导航栏

@Entry
@Component
struct CustomNavPage {build() {Navigation() {Column() {Text('页面内容').fontSize(20).margin(20)}}.title('自定义标题').subTitle('副标题').hideBackButton(false).toolBar({items: [{icon: $r('app.media.ic_share'),action: () => {// 分享操作}},{icon: $r('app.media.ic_favorite'),action: () => {// 收藏操作}}]}).navBarWidth('80%').navBarPosition(NavBarPosition.Start)}
}

分栏模式 (Split)

@Entry
@Component
struct SplitNavigationExample {private menus: string[] = ['首页', '分类', '发现', '我的']@State selectedIndex: number = 0build() {Navigation() {Column() {ForEach(this.menus, (menu, index) => {Button(menu).width('100%').stateEffect(this.selectedIndex === index).onClick(() => {this.selectedIndex = index})})}.width('30%').backgroundColor('#f5f5f5')Column() {if (this.selectedIndex === 0) {Text('首页内容').fontSize(24)} else if (this.selectedIndex === 1) {Text('分类内容').fontSize(24)} else if (this.selectedIndex === 2) {Text('发现内容').fontSize(24)} else {Text('个人中心').fontSize(24)}}.layoutWeight(1).padding(20)}.mode(NavigationMode.Split).minNavBarWidth(200).maxNavBarWidth(300)}
}

页面切换动画

@Entry
@Component
struct AnimatedNavigation {build() {Navigation() {Column() {Button('淡入淡出效果').onClick(() => {router.pushUrl({url: 'pages/DetailPage',transition: {type: RouteType.Push,curve: Curve.EaseInOut,duration: 300}})})Button('滑动效果').onClick(() => {router.pushUrl({url: 'pages/DetailPage',transition: {type: RouteType.Push,slide: SlideEffect.Left}})})}}}
}

实际应用示例

电商应用导航

@Entry
@Component
struct ECommerceApp {@State currentTab: number = 0private tabs: string[] = ['首页', '分类', '购物车', '我的']build() {Navigation() {Column() {// 顶部导航栏Row() {ForEach(this.tabs, (tab, index) => {Button(tab).borderRadius(0).backgroundColor(this.currentTab === index ? '#ff5500' : '#ffffff').fontColor(this.currentTab === index ? Color.White : Color.Black).onClick(() => {this.currentTab = index})})}.width('100%').height(50)// 内容区域Column() {if (this.currentTab === 0) {HomeTab()} else if (this.currentTab === 1) {CategoryTab()} else if (this.currentTab === 2) {CartTab()} else {ProfileTab()}}.layoutWeight(1)}}.hideToolBar(true).hideTitleBar(true)}
}@Component
struct HomeTab {build() {Scroll() {Column() {// 轮播图Swiper() {ForEach(['banner1', 'banner2', 'banner3'], (img) => {Image(img).width('100%').height(200)})}.indicator(true).height(200)// 商品网格Grid() {ForEach(Array.from({ length: 12 }), (_, index) => {GridItem() {Column() {Image(`product_${index + 1}`).width(80).height(80)Text(`商品${index + 1}`).fontSize(14)Text('¥99').fontColor('#ff5500').fontSize(16)}}})}.columnsTemplate('1fr 1fr 1fr').rowsGap(10).columnsGap(10).margin(10)}}}
}

新闻应用导航

@Entry
@Component
struct NewsApp {@State currentChannel: string = '推荐'private channels: string[] = ['推荐', '热点', '科技', '娱乐', '体育']build() {Navigation() {Column() {// 频道导航Scroll({ scrollable: ScrollDirection.Horizontal }) {Row() {ForEach(this.channels, (channel) => {Button(channel).margin({ right: 15 }).stateEffect(this.currentChannel === channel).onClick(() => {this.currentChannel = channel})})}.padding(10)}.scrollBar(BarState.Off)// 新闻列表Scroll() {Column() {ForEach(Array.from({ length: 10 }), (_, index) => {Column() {Text(`${this.currentChannel}】新闻标题 ${index + 1}`).fontSize(18).fontWeight(FontWeight.Bold).margin({ bottom: 5 })Text('新闻摘要内容...').fontSize(14).fontColor('#666666').margin({ bottom: 10 })Divider()}.margin({ top: 10, left: 15, right: 15 }).onClick(() => {router.pushUrl({url: 'pages/NewsDetail',params: { id: index + 1 }})})})}}.layoutWeight(1)}}.title('新闻资讯').toolBar({items: [{icon: $r('app.media.ic_search'),action: () => {router.pushUrl({url: 'pages/SearchPage'})}}]})}
}

注意事项

  1. 页面生命周期

    • onPageShow:页面显示时触发
    • onPageHide:页面隐藏时触发
    • onBackPress:返回按钮点击时触发(可拦截返回操作)
  2. 路由管理

    • 使用 router.pushUrl 导航到新页面
    • 使用 router.back 返回上一页
    • 使用 router.replaceUrl 替换当前页面
    • 使用 router.clear 清空页面栈
  3. 性能优化

    • 避免在导航栏中使用复杂组件
    • 对于不常变化的页面考虑使用缓存
    • 合理使用分栏模式提升大屏设备体验
  4. 兼容性考虑

    • 分栏模式在大屏设备上效果更好
    • 确保导航结构在小屏设备上也能良好工作

Navigation 组件是构建复杂应用导航系统的基础,合理使用可以创建直观、高效的用户导航体验。在实际开发中,可以根据应用需求结合 Tabs、SideBar 等组件构建更丰富的导航结构。

http://www.xdnf.cn/news/149959.html

相关文章:

  • 队列基础和例题
  • Linux-05 半个月崩了三次 ubuntu 系统记录
  • Linux网络编程
  • 2025智能营销平台发展趋势
  • 消息唯一ID算法参考
  • DbCreateHelper数据库创建指南
  • 建筑节能成发展焦点,楼宇自控应用范围持续扩大
  • 文件IO(Java)
  • Python MCP客户端SDK实现
  • AIDL进程间通信
  • node.js 实战——从0开始做一个餐厅预订(express+node+ejs+bootstrap)
  • js的作用域,作用域链,执行上下文,变量对象,活动对象
  • 谷歌AI眼镜:你的第二大脑,未来人机共生从这里开始
  • 前端如何获取文件的 Hash 值?多种方式详解、对比与实践指南
  • 列表与字典应用
  • 动态规划算法详解(C++)
  • EFL格式|动态库加载 | 重谈地址空间(2)
  • 复合材料高置信度 DIC 测量与高级实验技术研讨会邀请函
  • 达梦数据库压力测试报错超出全局hash join空间,适当增加HJ_BUF_GLOBAL_SIZE解决
  • 【计算机视觉】CV实战项目 - 基于YOLOv5的人脸检测与关键点定位系统深度解析
  • mysql 安装
  • 项目实战-基于大数据分析的暖通系统改造模型【感谢Akila公司以及学院的支持】
  • Lobechat使用WolframAlpha MCP工具减少LLM幻觉
  • Java 设计模式心法之第23篇 - 状态 (State) - 让对象的行为随状态优雅切换
  • 【蓝桥杯选拔赛真题104】Scratch回文数 第十五届蓝桥杯scratch图形化编程 少儿编程创意编程选拔赛真题解析
  • IPOF(Input-Process-Output-Feedback)方法学简介
  • XMOS空间音频——在任何设备上都能提供3D沉浸式空间音频且实现更安全地聆听
  • 【计算机视觉】CV实践项目- 基于PaddleSeg的遥感建筑变化检测全解析:从U-Net 3+原理到工程实践
  • numpy.random.normal与numpy.random.randn的区别与联系
  • 雷电模拟器怎么更改IP地址