导航转场Navigation

导航转场是页面的路由转场方式,也就是一个界面消失,另外一个界面出现的动画效果。开发者也可以自定义导航转场的动画效果,具体请参考Navigation示例3

导航转场推荐使用Navigation组件实现,可搭配NavDestination组件实现导航功能。

完整的代码示例和效果如下。

创建导航页

实现步骤为:

1.使用Navigation创建导航主页,并创建路由栈NavPathStack以此来实现不同页面之间的跳转。

2.在Navigation中增加List组件,来定义导航主页中不同的一级界面。

3.在List内的组件添加onClick方法,并在其中使用路由栈NavPathStack的pushPathByName方法,使组件可以在点击之后从当前页面跳转到输入参数name在路由表内对应的页面。

 
  1. //PageOne.ets
  2. @Entry
  3. @Component
  4. struct NavigationDemo {
  5. @Provide('pathInfos') pathInfos: NavPathStack = new NavPathStack();
  6. private listArray: Array<string> = ['WLAN', 'Bluetooth', 'Personal Hotspot', 'Connect & Share'];
  7. build() {
  8. Column() {
  9. Navigation(this.pathInfos) {
  10. TextInput({ placeholder: '输入关键字搜索' })
  11. .width('90%')
  12. .height(40)
  13. .margin({ bottom: 10 })
  14. // 通过List定义导航的一级界面
  15. List({ space: 12, initialIndex: 0 }) {
  16. ForEach(this.listArray, (item: string) => {
  17. ListItem() {
  18. Row() {
  19. Row() {
  20. Text(`${item.slice(0, 1)}`)
  21. .fontColor(Color.White)
  22. .fontSize(14)
  23. .fontWeight(FontWeight.Bold)
  24. }
  25. .width(30)
  26. .height(30)
  27. .backgroundColor('#a8a8a8')
  28. .margin({ right: 20 })
  29. .borderRadius(20)
  30. .justifyContent(FlexAlign.Center)
  31. Column() {
  32. Text(item)
  33. .fontSize(16)
  34. .margin({ bottom: 5 })
  35. }
  36. .alignItems(HorizontalAlign.Start)
  37. Blank()
  38. Row()
  39. .width(12)
  40. .height(12)
  41. .margin({ right: 15 })
  42. .border({
  43. width: { top: 2, right: 2 },
  44. color: 0xcccccc
  45. })
  46. .rotate({ angle: 45 })
  47. }
  48. .borderRadius(15)
  49. .shadow({ radius: 100, color: '#ededed' })
  50. .width('90%')
  51. .alignItems(VerticalAlign.Center)
  52. .padding({ left: 15, top: 15, bottom: 15 })
  53. .backgroundColor(Color.White)
  54. }
  55. .width('100%')
  56. .onClick(() => {
  57. this.pathInfos.pushPathByName(`${item}`, '详情页面参数'); // 将name指定的NaviDestination页面信息入栈,传递的参数为param
  58. })
  59. }, (item: string): string => item)
  60. }
  61. .listDirection(Axis.Vertical)
  62. .edgeEffect(EdgeEffect.Spring)
  63. .sticky(StickyStyle.Header)
  64. .chainAnimation(false)
  65. .width('100%')
  66. }
  67. .width('100%')
  68. .mode(NavigationMode.Auto)
  69. .title('设置') // 设置标题文字
  70. }
  71. .size({ width: '100%', height: '100%' })
  72. .backgroundColor(0xf4f4f5)
  73. }
  74. }

创建导航子页

导航子页1实现步骤为:

1.使用NavDestination,来创建导航子页CommonPage。

2.创建路由栈NavPathStack并在onReady时进行初始化,获取当前所在的页面栈,以此来实现不同页面之间的跳转。

3.在子页面内的组件添加onClick,并在其中使用路由栈NavPathStack的pop方法,使组件可以在点击之后弹出路由栈栈顶元素实现页面的返回。

 
  1. //PageOne.ets
  2. @Builder
  3. export function MyCommonPageBuilder(name: string, param: string) {
  4. MyCommonPage({ name: name, value: param });
  5. }
  6. @Component
  7. export struct MyCommonPage {
  8. pathInfos: NavPathStack = new NavPathStack();
  9. name: String = '';
  10. @State value: String = '';
  11. build() {
  12. NavDestination() {
  13. Column() {
  14. Text(`${this.name}设置页面`)
  15. .width('100%')
  16. .fontSize(20)
  17. .fontColor(0x333333)
  18. .textAlign(TextAlign.Center)
  19. .textShadow({
  20. radius: 2,
  21. offsetX: 4,
  22. offsetY: 4,
  23. color: 0x909399
  24. })
  25. .padding({ top: 30 })
  26. Text(`${JSON.stringify(this.value)}`)
  27. .width('100%')
  28. .fontSize(18)
  29. .fontColor(0x666666)
  30. .textAlign(TextAlign.Center)
  31. .padding({ top: 45 })
  32. Button('返回')
  33. .width('50%')
  34. .height(40)
  35. .margin({ top: 50 })
  36. .onClick(() => {
  37. //弹出路由栈栈顶元素,返回上个页面
  38. this.pathInfos.pop();
  39. })
  40. }
  41. .size({ width: '100%', height: '100%' })
  42. }.title(`${this.name}`)
  43. .onReady((ctx: NavDestinationContext) => {
  44. // NavDestinationContext获取当前所在的页面栈
  45. this.pathInfos = ctx.pathStack;
  46. })
  47. }
  48. }

导航子页2实现步骤为:

1.使用NavDestination,来创建导航子页SharePage。

2.创建路由栈NavPathStack并在onReady时进行初始化,获取当前所在的页面栈,以此来实现不同页面之间的跳转。

3.在子页面内的组件添加onClick,并在其中使用路由栈NavPathStack的pushPathByName方法,使组件可以在点击之后从当前页面跳转到输入参数name在路由表内对应的页面。

 
  1. //PageTwo.ets
  2. @Builder
  3. export function MySharePageBuilder(name: string, param: string) {
  4. MySharePage({ name: name });
  5. }
  6. @Component
  7. export struct MySharePage {
  8. pathInfos: NavPathStack = new NavPathStack();
  9. name: String = '';
  10. private listArray: Array<string> = ['Projection', 'Print', 'VPN', 'Private DNS', 'NFC'];
  11. build() {
  12. NavDestination() {
  13. Column() {
  14. List({ space: 12, initialIndex: 0 }) {
  15. ForEach(this.listArray, (item: string) => {
  16. ListItem() {
  17. Row() {
  18. Row() {
  19. Text(`${item.slice(0, 1)}`)
  20. .fontColor(Color.White)
  21. .fontSize(14)
  22. .fontWeight(FontWeight.Bold)
  23. }
  24. .width(30)
  25. .height(30)
  26. .backgroundColor('#a8a8a8')
  27. .margin({ right: 20 })
  28. .borderRadius(20)
  29. .justifyContent(FlexAlign.Center)
  30. Column() {
  31. Text(item)
  32. .fontSize(16)
  33. .margin({ bottom: 5 })
  34. }
  35. .alignItems(HorizontalAlign.Start)
  36. Blank()
  37. Row()
  38. .width(12)
  39. .height(12)
  40. .margin({ right: 15 })
  41. .border({
  42. width: { top: 2, right: 2 },
  43. color: 0xcccccc
  44. })
  45. .rotate({ angle: 45 })
  46. }
  47. .borderRadius(15)
  48. .shadow({ radius: 100, color: '#ededed' })
  49. .width('90%')
  50. .alignItems(VerticalAlign.Center)
  51. .padding({ left: 15, top: 15, bottom: 15 })
  52. .backgroundColor(Color.White)
  53. }
  54. .width('100%')
  55. .onClick(() => {
  56. this.pathInfos.pushPathByName(`${item}`, '页面设置参数');
  57. })
  58. }, (item: string): string => item)
  59. }
  60. .listDirection(Axis.Vertical)
  61. .edgeEffect(EdgeEffect.Spring)
  62. .sticky(StickyStyle.Header)
  63. .width('100%')
  64. }
  65. .size({ width: '100%', height: '100%' })
  66. }.title(`${this.name}`)
  67. .onReady((ctx: NavDestinationContext) => {
  68. // NavDestinationContext获取当前所在的页面栈
  69. this.pathInfos = ctx.pathStack;
  70. })
  71. }
  72. }

创建路由跳转

实现步骤为:

1.工程配置文件module.json5中配置 {"routerMap": "$profile:route_map"}。

2.route_map.json中配置全局路由表,路由栈NavPathStack可根据路由表中的name将对应页面信息入栈。

 
  1. {
  2. "routerMap" : [
  3. {
  4. "name" : "WLAN",
  5. "pageSourceFile" : "src/main/ets/pages/PageOne.ets",
  6. "buildFunction" : "MyCommonPageBuilder"
  7. },
  8. {
  9. "name" : "Bluetooth",
  10. "pageSourceFile" : "src/main/ets/pages/PageOne.ets",
  11. "buildFunction" : "MyCommonPageBuilder"
  12. },
  13. {
  14. "name" : "Personal Hotspot",
  15. "pageSourceFile" : "src/main/ets/pages/PageOne.ets",
  16. "buildFunction" : "MyCommonPageBuilder"
  17. },
  18. {
  19. "name" : "Connect & Share",
  20. "pageSourceFile" : "src/main/ets/pages/PageTwo.ets",
  21. "buildFunction" : "MySharePageBuilder"
  22. },
  23. {
  24. "name" : "Projection",
  25. "pageSourceFile" : "src/main/ets/pages/PageOne.ets",
  26. "buildFunction" : "MyCommonPageBuilder"
  27. },
  28. {
  29. "name" : "Print",
  30. "pageSourceFile" : "src/main/ets/pages/PageOne.ets",
  31. "buildFunction" : "MyCommonPageBuilder"
  32. },
  33. {
  34. "name" : "VPN",
  35. "pageSourceFile" : "src/main/ets/pages/PageOne.ets",
  36. "buildFunction" : "MyCommonPageBuilder"
  37. },
  38. {
  39. "name" : "Private DNS",
  40. "pageSourceFile" : "src/main/ets/pages/PageOne.ets",
  41. "buildFunction" : "MyCommonPageBuilder"
  42. },
  43. {
  44. "name" : "NFC",
  45. "pageSourceFile" : "src/main/ets/pages/PageOne.ets",
  46. "buildFunction" : "MyCommonPageBuilder"
  47. }
  48. ]
  49. }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值