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


    前言

    DevEco Studio版本:4.0.0.600

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

    其他篇文章参考:

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

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

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

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

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

    效果

    导航页面实现

    从UI效果上可以看出是一个列表标题里面包含一些流式的数据块然后循环这样的布局,因此我们可以通过List组件中的ListItemGroup来实现,流式布局的item可以参考之前文章中的搜索页面的热搜里面的UI效果来实现:Flex({wrap: FlexWrap.Wrap })

    参考链接:OpenHarmony List 、OpenHarmony ListItemGroup

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

    1、List列表

    1. build() {
    2. List() {
    3. ForEach(this.homeListData, (item: NavigationListBean) => {
    4. ListItemGroup({ header: this.itemHead(item.name) }) {
    5. // ListItem
    6. }
    7. }, (item: NavigationListBean) => item.name)
    8. }
    9. .width('100%')
    10. .height('100%')
    11. }

    2、ListItem流式布局

    1. ListItem() {
    2. Flex({ justifyContent: FlexAlign.Start, wrap: FlexWrap.Wrap }) {
    3. ForEach(item.articles, (project: NavigationListItemBean, index: number) => {
    4. Text(project.title)
    5. .fontSize(18)
    6. .fontColor(this.getTextColor(index))
    7. .padding({ left: 15, right: 15, top: 10, bottom: 10 })
    8. .margin({ top: 10, right: 10 })
    9. .maxLines(1)
    10. .borderRadius(15)
    11. .textOverflow({ overflow: TextOverflow.Ellipsis })
    12. .textAlign(TextAlign.Start)
    13. .backgroundColor('#FFFFFF')
    14. .onClick(() => {
    15. LogUtils.info(TAG, "点击了:index: " + index + " project: " + JSON.stringify(project))
    16. router.pushUrl({
    17. url: 'pages/WebPage',
    18. params: {
    19. title: project.title,
    20. uriLink: project.link
    21. }
    22. }, router.RouterMode.Single)
    23. })
    24. },
    25. (item: string) => item.toString()
    26. )
    27. }
    28. .margin({ left: 10, right: 10, bottom: 10 })
    29. }

    3、动态设置item的颜色值

    根据数组的index动态设置颜色值

     Text(project.title).fontColor(this.getTextColor(index))
    1. private getTextColor(index: number): ResourceColor {
    2. if (index % 3 == 0) {
    3. return Color.Orange
    4. } else if (index % 3 == 1) {
    5. return Color.Blue
    6. } else if (index % 3 == 2) {
    7. return Color.Pink
    8. }
    9. return Color.Black
    10. }

    4、详细代码

    1. import { HttpManager, RequestMethod } from '@app/BaseLibrary';
    2. import LogUtils from '@app/BaseLibrary/src/main/ets/utils/LogUtils';
    3. import { NavigationListBean } from './bean/NavigationListBean';
    4. import { NavigationBean } from './bean/NavigationBean';
    5. import { NavigationListItemBean } from './bean/NavigationListItemBean';
    6. import router from '@ohos.router';
    7. const TAG = 'NavigationPage--- ';
    8. @Component
    9. export struct NavigationPage {
    10. @State homeListData: Array<NavigationListBean> = [];
    11. aboutToAppear() {
    12. this.getNavigationData()
    13. LogUtils.info(TAG, "33333333333 aboutToAppear执行了")
    14. }
    15. private getNavigationData() {
    16. HttpManager.getInstance()
    17. .request<NavigationBean>({
    18. method: RequestMethod.GET,
    19. url: 'https://www.wanandroid.com/navi/json' //wanAndroid的API:导航
    20. })
    21. .then((result: NavigationBean) => {
    22. LogUtils.info(TAG, "result: " + JSON.stringify(result))
    23. if (result.errorCode == 0) {
    24. this.homeListData = result.data
    25. }
    26. })
    27. .catch((error) => {
    28. LogUtils.info(TAG, "error: " + JSON.stringify(error))
    29. })
    30. }
    31. @Builder
    32. itemHead(text: string) {
    33. Text(text)
    34. .fontSize(20)
    35. .fontColor(Color.Black)
    36. .fontWeight(FontWeight.Bold)
    37. .padding(10)
    38. }
    39. build() {
    40. List() {
    41. ForEach(this.homeListData, (item: NavigationListBean) => {
    42. ListItemGroup({ header: this.itemHead(item.name) }) {
    43. ListItem() {
    44. Flex({ justifyContent: FlexAlign.Start, wrap: FlexWrap.Wrap }) {
    45. ForEach(item.articles, (project: NavigationListItemBean, index: number) => {
    46. Text(project.title)
    47. .fontSize(18)
    48. .fontColor(this.getTextColor(index))
    49. .padding({ left: 15, right: 15, top: 10, bottom: 10 })
    50. .margin({ top: 10, right: 10 })
    51. .maxLines(1)
    52. .borderRadius(15)
    53. .textOverflow({ overflow: TextOverflow.Ellipsis })
    54. .textAlign(TextAlign.Start)
    55. .backgroundColor('#FFFFFF')
    56. .onClick(() => {
    57. LogUtils.info(TAG, "点击了:index: " + index + " project: " + JSON.stringify(project))
    58. router.pushUrl({
    59. url: 'pages/WebPage',
    60. params: {
    61. title: project.title,
    62. uriLink: project.link
    63. }
    64. }, router.RouterMode.Single)
    65. })
    66. },
    67. (item: string) => item.toString()
    68. )
    69. }
    70. .margin({ left: 10, right: 10, bottom: 10 })
    71. }
    72. }
    73. }, (item: NavigationListBean) => item.name)
    74. }
    75. .width('100%')
    76. .height('100%')
    77. }
    78. private getTextColor(index: number): ResourceColor {
    79. if (index % 3 == 0) {
    80. return Color.Orange
    81. } else if (index % 3 == 1) {
    82. return Color.Blue
    83. } else if (index % 3 == 2) {
    84. return Color.Pink
    85. }
    86. return Color.Black
    87. }
    88. }

    5、页面初始化获取导航数据

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

    源代码地址:WanAndroid_Harmony: WanAndroid的鸿蒙版本

  • 相关阅读:
    Nacos相关面试题
    如何记账能简单高效,记账全攻略来了
    uiautomator2的安装,使用,在浏览器中查看页面的信息,简单快捷的ui测试工具,app测试工具(一)
    redis回顾
    2023下半年软考高级信息系统项目管理师考后解析
    操作符详解(3)
    C/C++数据结构课程设计安排
    sql中可以使用不在select中的字段排序
    理解MySQL的日志 Redo、Undo
    谷粒商城 (二十四) --------- 商品服务 API 平台属性 ① 规格参数
  • 原文地址:https://blog.csdn.net/Abner_Crazy/article/details/136709971