《探索 HarmonyOS NEXT(5.0):开启构建模块化项目架构奇幻之旅 —— Tabs底 ...

打印 上一主题 下一主题

主题 1031|帖子 1031|积分 3093

简介
通过学习HarmonyOS Next,实战项目WanAndroid 鸿蒙版,API接口均来自WanAndroid 开源接口,我们一起来做个App吧。
玩Android 开放API:https://www.wanandroid.com/blog/show/2
效果图

创建每个模块下的各个目录

1、features基础特性层下面的模块对应的目录
  1.     features                                       # 基础特性层,包含独立的业务模块,如启动页、登录模块等              
  2.     |---home                                       // 首页
  3.     |   |---bean                                   // 数据模型
  4.     |   |---components                             // 自定义组件
  5.     |   |---constants                              // 常量
  6.     |   |---model                                  // 业务模型
  7.     |   |---service                                // 业务服务/接口
  8.     |   |---views                                  // 视图层
  9.     |   |---utils                                  // 此模块工具类 需要再加   
  10.     |---login                                      // 登录
  11.     |---question                                   // 问答
  12.     |---scheme                                     // 体系
  13.     |---mine                                       // 我的
  14.     |---login                                      // 登录
复制代码

2、products产品定制层下面的模块对应的目录
  1.     products                                       # 产品定制层,作为不同设备或场景应用入口,例如phone、tv等
  2.     |---phone                                      // 手机
  3.     |   |---app                                    // 全局初始化配置
  4.     |   |---bean                                   // 数据模型
  5.     |   |---components                             // 自定义组件
  6.     |   |---constants                              // 常量
  7.     |   |---model                                  // 业务模型
  8.     |   |---pages                                  // 页面
  9.     |   |---service                                // 业务服务/接口
  10.     |   |---test                                   // 测试某个效果的例子
复制代码

创建后就可以搭建了。
使用Tabs搭建底部导航栏



  • 详细使用步骤还得去官方(选项卡 (Tabs))去学习,再来看就一目了然了.
1、通过上面图一底部导航栏分为上面图片,下面是笔墨,选中都变颜色,定义一个对象的结构,命名为TabModel,因为后期大概为公共的,所以位置放在uicomponents模块model目录。
  1.     export interface TabModel {
  2.       index: number; // 下标
  3.       title: string; // 标题
  4.       selectImage: Resource; // 选中图片
  5.       // unSelectImage: Resource; // 未选中图片
  6.     }
复制代码
2、组装数据,定义一个首页底部Tab数据,图片资源都在源码里。
  1. /**
  2. * 首页底部Tab显示数据
  3. */
  4. export const tabBarModel: Array<TabModel> = [
  5.   {
  6.     index: 0,
  7.     title: '首页',
  8.     selectImage: $r('app.media.ic_bottom_bar_home'),
  9.   },
  10.   {
  11.     index: 1,
  12.     title: '问答',
  13.     selectImage: $r('app.media.ic_bottom_bar_ques'),
  14.   },
  15.   {
  16.     index: 2,
  17.     title: '体系',
  18.     selectImage: $r("app.media.ic_bottom_bar_scheme"),
  19.   },
  20.   {
  21.     index: 3,
  22.     title: '我的',
  23.     selectImage: $r('app.media.ic_bottom_bar_mine'),
  24.   }
  25. ]
复制代码
3、搭建Tabs


  • 定义一个选中的index
  • 设置为底部导航时,需要将barPosition设置为BarPosition.End。
  • 鼠标放到Tabs上,可以查看对应的API,很方便



  • 自定义导航栏,通过TabContent的tabBar
  1.     Column() {
  2.       Divider().color($r('app.color.color_F0F0F0'))
  3.       Badge({
  4.         count: index != 1 ? 0 : this.msgCount,
  5.         position: BadgePosition.RightTop,
  6.         style: {
  7.           fontSize: 8,
  8.           badgeSize: 13
  9.         }
  10.       }) {
  11.         Image(item.selectImage /*this.selectedIndex == index ? item.selectImage : item.unSelectImage*/)
  12.           .colorBlend(this.selectedIndex == index ? $r('app.color.colorPrimary') : $r('app.color.color_222222'))
  13.           .height(24)
  14.           .margin(4)
  15.       }.margin({ top: 4 })
  16.       Text(item.title)
  17.         .width(CommonConst.FULL_PARENT)
  18.         .fontSize(14)
  19.         .fontWeight(500)
  20.         .textAlign(TextAlign.Center)
  21.         .fontColor(this.selectedIndex == index ? $r('app.color.colorPrimary') : $r('app.color.color_222222'))
  22.         .margin({ bottom: 4 })
  23.     }
  24.     .width(CommonConst.FULL_PARENT)
  25.     .height(CommonConst.FULL_PARENT)
  26.     .backgroundColor($r('app.color.white'))
复制代码


  • !!!选中的图片假如只是变了颜色,实在不消再单独设置一个选中的图片,可以通过Image的colorBlend来设置选中图片


4、效果为



  • 把中间内容地域换成每个模块对应的页面,分别是
  1. @Builder
  2. tabContentBuilder(item: TabModel) {
  3.   if (item.index == 0) {
  4.     // 首页
  5.     HomeView()
  6.   } else if (item.index == 1) {
  7.     // 问答
  8.     QuestionView()
  9.   } else if (item.index == 2) {
  10.     // 体系
  11.     SchemeView()
  12.   } else if (item.index == 3) {
  13.     // 我的
  14.     MineView()
  15.   }
  16. }
复制代码
切换导致的问题



  • 每次切换都重新加载,这肯定对用户的体验是欠好的,那我们通过存储每个页面的状态来实现,就初始化一次。



  • 定义一个数组,存储状态
  1.     //存储页面状态
  2.     @Local tabContentArr: boolean[] = [true, false, false, false]
复制代码


  • 切换的时候状态设置为true
  1.     .onChange((index: number) => {
  2.       this.selectedIndex = index;
  3.       this.tabContentArr[index] = true;
  4.     })
复制代码


  • 加载视图判断
  1.     if (this.selectedIndex === index || this.tabContentArr[index]) {
  2.       this.tabContentBuilder(item)
  3.     }
复制代码


  • 终极效果为

完整代码

  1.     @Preview
  2.     @ComponentV2
  3.     export struct MainPage {
  4.       @Local selectedIndex: number = 0 // 当前选中的tab下标
  5.       @Local msgCount: number = 9 // 消息数量
  6.       //存储页面状态
  7.       @Local tabContentArr: boolean[] = [true, false, false, false]
  8.       build() {
  9.         Stack() {
  10.           Tabs({ index: this.selectedIndex, barPosition: BarPosition.End }) {
  11.             ForEach(tabBarModel, (item: TabModel, index: number) => {
  12.               TabContent() {
  13.                 if (this.selectedIndex === index || this.tabContentArr[index]) {
  14.                   this.tabContentBuilder(item)
  15.                 }
  16.               }.tabBar(this.tabBottom(tabBarModel[item.index], item.index))
  17.             }, (item: string) => item)
  18.           }.barWidth(CommonConst.FULL_PARENT)
  19.           .barHeight(56) //设置导航栏高度
  20.           .scrollable(false) // 禁止左右滑动
  21.           .onChange((index: number) => {
  22.             this.selectedIndex = index;
  23.             this.tabContentArr[index] = true;
  24.           })
  25.         }.width(CommonConst.FULL_PARENT)
  26.         .height(CommonConst.FULL_PARENT)
  27.       }
  28.       @Builder
  29.       tabContentBuilder(item: TabModel) {
  30.         if (item.index == 0) {
  31.           // 首页
  32.           HomeView()
  33.         } else if (item.index == 1) {
  34.           // 问答
  35.           QuestionView()
  36.         } else if (item.index == 2) {
  37.           // 体系
  38.           SchemeView()
  39.         } else if (item.index == 3) {
  40.           // 我的
  41.           MineView()
  42.         }
  43.       }
  44.       @Builder
  45.       tabBottom(item: TabModel, index: number) {
  46.         Column() {
  47.           Divider().color($r('app.color.color_F0F0F0'))
  48.           Badge({
  49.             count: index != 1 ? 0 : this.msgCount,
  50.             position: BadgePosition.RightTop,
  51.             style: {
  52.               fontSize: 8,
  53.               badgeSize: 13
  54.             }
  55.           }) {
  56.             Image(item.selectImage /*this.selectedIndex == index ? item.selectImage : item.unSelectImage*/)
  57.               .colorBlend(this.selectedIndex == index ? $r('app.color.colorPrimary') : $r('app.color.color_222222'))
  58.               .height(24)
  59.               .margin(4)
  60.           }.margin({ top: 4 })
  61.           Text(item.title)
  62.             .width(CommonConst.FULL_PARENT)
  63.             .fontSize(14)
  64.             .fontWeight(500)
  65.             .textAlign(TextAlign.Center)
  66.             .fontColor(this.selectedIndex == index ? $r('app.color.colorPrimary') : $r('app.color.color_222222'))
  67.             .margin({ bottom: 4 })
  68.         }
  69.         .width(CommonConst.FULL_PARENT)
  70.         .height(CommonConst.FULL_PARENT)
  71.         .backgroundColor($r('app.color.white'))
  72.       }
  73.     }
复制代码
工程目录,请看README.md

https://gitee.com/jiaojiaoone/explore-harmony-next/blob/master/README.md


  • 以往系列文章

  • 《探索 HarmonyOS NEXT(5.0):开启构建模块化项目架构奇幻之旅 —— 模块化基础篇》
  • 《探索 HarmonyOS NEXT(5.0):开启构建模块化项目架构奇幻之旅 —— 构建基础特性层》
  • 《探索 HarmonyOS NEXT(5.0):开启构建模块化项目架构奇幻之旅 —— 构建公共能力层》
若本文对您稍有帮助,诚望您不吝点赞,多谢。
有兴趣的同学可以点击查看源码



  • gitee:https://gitee.com/jiaojiaoone/explore-harmony-next.git
  • github:https://github.com/JasonYinH/ExploreHarmonyNext.git
接待加我微信一起交流:+V:yinshiyuba


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

王海鱼

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表