当前位置:   article > 正文

WanAndroid(鸿蒙版)开发的第四篇_鸿蒙 tabbar subtabbarstyle

鸿蒙 tabbar subtabbarstyle

前言

DevEco Studio版本:4.0.0.600

WanAndroid的API链接:玩Android 开放API-玩Android - wanandroid.com

其他篇文章参考:

1、WanAndroid(鸿蒙版)开发的第一篇

2、WanAndroid(鸿蒙版)开发的第二篇

3、WanAndroid(鸿蒙版)开发的第三篇

4、WanAndroid(鸿蒙版)开发的第四篇

5、WanAndroid(鸿蒙版)开发的第五篇

 6、WanAndroid(鸿蒙版)开发的第六篇

效果

项目页面实现

从UI效果上我们知道是可滑动的tab,切换tab时内容切换,因此通过Tabs组件实现

参考链接:OpenHarmony Tabs

因为项目模块有对BaseLibrary模块的引用,在oh-package.json5添加对其引用

1、Tabs列表实现(ProjectList)

  1. build() {
  2. Tabs({
  3. barPosition: BarPosition.Start,
  4. controller: this.tabsController,
  5. }) {
  6. ForEach(this.projectListData, (item: ProjectListItemBean) => {
  7. TabContent() {
  8. TabContentLayout({ tabId: item.id, onDataFinish: () => {
  9. this.onDataFinish()
  10. } })
  11. }
  12. .padding({ left: 12, right: 12 })
  13. .tabBar(new SubTabBarStyle(item.name))
  14. }, (item: ProjectListItemBean) => item.name)
  15. }
  16. .width('100%')
  17. .height('100%')
  18. .barMode(BarMode.Scrollable)
  19. }

2、TabContentLayout列表内容实现

  1. import { HttpManager, RefreshController, RefreshListView, RequestMethod } from '@app/BaseLibrary';
  2. import LogUtils from '@app/BaseLibrary/src/main/ets/utils/LogUtils';
  3. import { TabContentItemBean } from '../bean/TabContentItemBean';
  4. import { TabContentBean } from '../bean/TabContentBean';
  5. import router from '@ohos.router';
  6. const TAG = 'TabContentLayout--- ';
  7. @Component
  8. export struct TabContentLayout {
  9. @State controller: RefreshController = new RefreshController()
  10. @State tabContentItemData: Array<TabContentItemBean> = [];
  11. @State pageNum: number = 1 //1开始
  12. @State isRefresh: boolean = true
  13. @Prop tabId: number
  14. private onDataFinish: () => void //数据加载完成回调
  15. aboutToAppear() {
  16. LogUtils.info(TAG, "tabId: " + this.tabId)
  17. this.getTabContentData()
  18. }
  19. private getTabContentData() {
  20. LogUtils.info(TAG, "pageNum: " + this.pageNum)
  21. HttpManager.getInstance()
  22. .request<TabContentBean>({
  23. method: RequestMethod.GET,
  24. header: { "Content-Type": "application/json" },
  25. url: `https://www.wanandroid.com/project/list/${this.pageNum}/json?cid=${this.tabId}`
  26. })
  27. .then((result: TabContentBean) => {
  28. LogUtils.info(TAG, "result: " + JSON.stringify(result))
  29. if (this.isRefresh) {
  30. this.controller.finishRefresh()
  31. } else {
  32. this.controller.finishLoadMore()
  33. }
  34. if (result.errorCode == 0) {
  35. if (this.isRefresh) {
  36. this.tabContentItemData = result.data.datas
  37. } else {
  38. this.tabContentItemData = this.tabContentItemData.concat(result.data.datas)
  39. }
  40. }
  41. this.onDataFinish()
  42. })
  43. .catch((error) => {
  44. LogUtils.info(TAG, "error: " + JSON.stringify(error))
  45. if (this.isRefresh) {
  46. this.controller.finishRefresh()
  47. } else {
  48. this.controller.finishLoadMore()
  49. }
  50. this.onDataFinish()
  51. })
  52. }
  53. build() {
  54. RefreshListView({
  55. list: this.tabContentItemData,
  56. controller: this.controller,
  57. refreshLayout: (item: TabContentItemBean, index: number): void => this.itemLayout(item, index),
  58. onItemClick: (item: TabContentItemBean, index: number) => {
  59. LogUtils.info(TAG, "点击了:index: " + index + " item: " + item)
  60. router.pushUrl({
  61. url: 'pages/WebPage',
  62. params: {
  63. title: item.title,
  64. uriLink: item.link
  65. }
  66. }, router.RouterMode.Single)
  67. },
  68. onRefresh: () => {
  69. //下拉刷新
  70. this.isRefresh = true
  71. this.pageNum = 0
  72. this.getTabContentData()
  73. },
  74. onLoadMore: () => {
  75. //上拉加载
  76. this.isRefresh = false
  77. this.pageNum++
  78. this.getTabContentData()
  79. }
  80. })
  81. }
  82. @Builder
  83. itemLayout(item: TabContentItemBean, index: number) {
  84. RelativeContainer() {
  85. //封面
  86. Image(item.envelopePic)
  87. .alt($r('app.media.ic_default_cover'))
  88. .width(110)
  89. .height(160)
  90. .borderRadius(5)
  91. .id('imageEnvelope')
  92. .alignRules({
  93. top: { anchor: '__container__', align: VerticalAlign.Top },
  94. left: { anchor: '__container__', align: HorizontalAlign.Start }
  95. })
  96. //title
  97. //标题
  98. Text(item.title)
  99. .fontColor('#333333')
  100. .fontWeight(FontWeight.Bold)
  101. .maxLines(2)
  102. .textOverflow({
  103. overflow: TextOverflow.Ellipsis
  104. })
  105. .fontSize(18)
  106. .margin({ left: 15 })
  107. .maxLines(2)
  108. .textOverflow({ overflow: TextOverflow.Ellipsis })
  109. .id("textTitle")
  110. .alignRules({
  111. top: { anchor: '__container__', align: VerticalAlign.Top },
  112. left: { anchor: 'imageEnvelope', align: HorizontalAlign.End },
  113. right: { anchor: '__container__', align: HorizontalAlign.End }
  114. })
  115. //描述
  116. Text(item.desc)
  117. .fontColor('#666666')
  118. .fontSize(16)
  119. .id("textDesc")
  120. .margin({ left: 15, top: 15 })
  121. .maxLines(4)
  122. .textOverflow({ overflow: TextOverflow.Ellipsis })
  123. .alignRules({
  124. top: { anchor: 'textTitle', align: VerticalAlign.Bottom },
  125. left: { anchor: 'imageEnvelope', align: HorizontalAlign.End },
  126. right: { anchor: '__container__', align: HorizontalAlign.End }
  127. })
  128. //时间
  129. Text(item.niceDate + " " + "作者:" + item.author)
  130. .fontColor('#666666')
  131. .fontSize(14)
  132. .margin({ left: 15 })
  133. .id("textNiceDate")
  134. .alignRules({
  135. bottom: { anchor: '__container__', align: VerticalAlign.Bottom },
  136. left: { anchor: 'imageEnvelope', align: HorizontalAlign.End }
  137. })
  138. }
  139. .width('100%')
  140. .height(180)
  141. .padding(10)
  142. .margin({ left: 10, right: 10, top: 6, bottom: 6 })
  143. .borderRadius(10)
  144. .backgroundColor(Color.White)
  145. }
  146. }

3、项目页面对布局引用

  1. import { LoadingDialog } from '@app/BaseLibrary';
  2. import LogUtils from '@app/BaseLibrary/src/main/ets/utils/LogUtils';
  3. import { ProjectList } from './widget/ProjectList';
  4. @Component
  5. export struct ProjectPage {
  6. @State projectLoadDataStatus: boolean = false
  7. aboutToAppear() {
  8. //弹窗控制器,显示
  9. this.dialogController.open()
  10. LogUtils.info("33333333333 ProjectPage aboutToAppear执行了")
  11. }
  12. private dialogController = new CustomDialogController({
  13. builder: LoadingDialog(),
  14. customStyle: true,
  15. alignment: DialogAlignment.Center
  16. })
  17. build() {
  18. Column() {
  19. ProjectList({ onDataFinish: () => {
  20. this.dialogController.close()
  21. this.projectLoadDataStatus = true
  22. } })
  23. }
  24. .visibility(this.projectLoadDataStatus ? Visibility.Visible : Visibility.Hidden)
  25. .width('100%')
  26. .height('100%')
  27. }
  28. }

4、页面初始化获取Tabs数据

  1. aboutToAppear() {
  2. this.getProjectListData()
  3. }

5、根据选中的Tab获取对应Tab的内容数据

  1. aboutToAppear() {
  2. LogUtils.info(TAG, "tabId: " + this.tabId)//选中的tab的id
  3. this.getTabContentData()
  4. }

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
  

闽ICP备14008679号