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 ListOpenHarmony ListItemGroup

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

1、List列表

复制代码
build() {
   List() {
      ForEach(this.homeListData, (item: NavigationListBean) => {
         ListItemGroup({ header: this.itemHead(item.name) }) {
           // ListItem 
         }
      }, (item: NavigationListBean) => item.name)
   }
   .width('100%')
   .height('100%')
}

2、ListItem流式布局

复制代码
ListItem() {
   Flex({ justifyContent: FlexAlign.Start, wrap: FlexWrap.Wrap }) {
      ForEach(item.articles, (project: NavigationListItemBean, index: number) => {
         Text(project.title)
            .fontSize(18)
            .fontColor(this.getTextColor(index))
            .padding({ left: 15, right: 15, top: 10, bottom: 10 })
            .margin({ top: 10, right: 10 })
            .maxLines(1)
            .borderRadius(15)
            .textOverflow({ overflow: TextOverflow.Ellipsis })
            .textAlign(TextAlign.Start)
            .backgroundColor('#FFFFFF')
            .onClick(() => {
               LogUtils.info(TAG, "点击了:index: " + index + " project: " + JSON.stringify(project))
               router.pushUrl({
                  url: 'pages/WebPage',
                  params: {
                     title: project.title,
                     uriLink: project.link
                  }
               }, router.RouterMode.Single)
            })
      },
         (item: string) => item.toString()
      )
   }
   .margin({ left: 10, right: 10, bottom: 10 })
}

3、动态设置item的颜色值

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

复制代码
 Text(project.title).fontColor(this.getTextColor(index))

private getTextColor(index: number): ResourceColor {
   if (index % 3 == 0) {
      return Color.Orange
   } else if (index % 3 == 1) {
      return Color.Blue
   } else if (index % 3 == 2) {
      return Color.Pink
   }
   return Color.Black
}

4、详细代码

复制代码
import { HttpManager, RequestMethod } from '@app/BaseLibrary';
import LogUtils from '@app/BaseLibrary/src/main/ets/utils/LogUtils';
import { NavigationListBean } from './bean/NavigationListBean';
import { NavigationBean } from './bean/NavigationBean';
import { NavigationListItemBean } from './bean/NavigationListItemBean';
import router from '@ohos.router';

const TAG = 'NavigationPage--- ';

@Component
export struct NavigationPage {
   @State homeListData: Array<NavigationListBean> = [];

   aboutToAppear() {
      this.getNavigationData()
      LogUtils.info(TAG, "33333333333   aboutToAppear执行了")
   }

   private getNavigationData() {
      HttpManager.getInstance()
         .request<NavigationBean>({
            method: RequestMethod.GET,
            url: 'https://www.wanandroid.com/navi/json' //wanAndroid的API:导航
         })
         .then((result: NavigationBean) => {
            LogUtils.info(TAG, "result: " + JSON.stringify(result))
            if (result.errorCode == 0) {
               this.homeListData = result.data
            }
         })
         .catch((error) => {
            LogUtils.info(TAG, "error: " + JSON.stringify(error))
         })
   }

   @Builder
   itemHead(text: string) {
      Text(text)
         .fontSize(20)
         .fontColor(Color.Black)
         .fontWeight(FontWeight.Bold)
         .padding(10)
   }

   build() {
      List() {
         ForEach(this.homeListData, (item: NavigationListBean) => {
            ListItemGroup({ header: this.itemHead(item.name) }) {
               ListItem() {
                  Flex({ justifyContent: FlexAlign.Start, wrap: FlexWrap.Wrap }) {
                     ForEach(item.articles, (project: NavigationListItemBean, index: number) => {
                        Text(project.title)
                           .fontSize(18)
                           .fontColor(this.getTextColor(index))
                           .padding({ left: 15, right: 15, top: 10, bottom: 10 })
                           .margin({ top: 10, right: 10 })
                           .maxLines(1)
                           .borderRadius(15)
                           .textOverflow({ overflow: TextOverflow.Ellipsis })
                           .textAlign(TextAlign.Start)
                           .backgroundColor('#FFFFFF')
                           .onClick(() => {
                              LogUtils.info(TAG, "点击了:index: " + index + " project: " + JSON.stringify(project))
                              router.pushUrl({
                                 url: 'pages/WebPage',
                                 params: {
                                    title: project.title,
                                    uriLink: project.link
                                 }
                              }, router.RouterMode.Single)
                           })
                     },
                        (item: string) => item.toString()
                     )
                  }
                  .margin({ left: 10, right: 10, bottom: 10 })
               }
            }
         }, (item: NavigationListBean) => item.name)
      }
      .width('100%')
      .height('100%')
   }

   private getTextColor(index: number): ResourceColor {
      if (index % 3 == 0) {
         return Color.Orange
      } else if (index % 3 == 1) {
         return Color.Blue
      } else if (index % 3 == 2) {
         return Color.Pink
      }
      return Color.Black
   }
}

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

复制代码
aboutToAppear() {
      this.getNavigationData()
}

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

相关推荐
FF2501_940228583 小时前
HarmonyOS应用开发实战:猫猫大作战-在排行榜和历史记录查询中,排序是必不可少的操作【apple_product_name】
华为·harmonyos·鸿蒙
程序员黑豆7 小时前
鸿蒙应用开发:@Provider 与 @Consumer 跨组件双向同步详解
前端·harmonyos
程序员黑豆8 小时前
鸿蒙应用开发之跨组件传参:@Provide 与 @Consume 跨层级数据同步详解
前端·harmonyos
懿路向前8 小时前
【HarmonyOS学习笔记】2026-07-30 | 小艺开放平台智能体接入实战
笔记·学习·harmonyos
大锅盖18 小时前
HarmonyOS ArkTS 的新手练手样例:从 Text 和 Button 开始,做一个会变化的计数页面
华为·harmonyos
袁震10 小时前
小图传输,大图呈现——用 HarmonyOS 7 端侧 AI 实现 4 倍图像超分重建
人工智能·华为·harmonyos
结网的兔子11 小时前
【前端开发】Web端迁移至 uni-app 及鸿蒙扩展方案对比
前端·uni-app·harmonyos
落叶飘飘s11 小时前
餐饮服务与软件创新的融合:解析海底捞 APP 的 Flutter 鸿蒙开发之路
flutter·华为·harmonyos
qizayaoshuap13 小时前
# HarmonyOS ArkTS 滑动删除列表实战(三):交互式列表增删操作
华为·harmonyos
GitCode官方14 小时前
openPangu-2.0-Pro 模型及技术报告正式开源上线 AtomGit AI
人工智能·华为·开源·harmonyos·atomgit