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

news/2024/7/21 8:50:17 标签: harmonyos, 玩安卓

前言

DevEco Studio版本:4.0.0.600

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

其他篇文章参考:

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

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

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

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

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

效果

项目页面实现

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

参考链接:OpenHarmony Tabs

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

1、Tabs列表实现(ProjectList)

build() {
   Tabs({
      barPosition: BarPosition.Start,
      controller: this.tabsController,
   }) {
      ForEach(this.projectListData, (item: ProjectListItemBean) => {
         TabContent() {
            TabContentLayout({ tabId: item.id, onDataFinish: () => {
               this.onDataFinish()
            } })
         }
         .padding({ left: 12, right: 12 })
         .tabBar(new SubTabBarStyle(item.name))
      }, (item: ProjectListItemBean) => item.name)
   }
   .width('100%')
   .height('100%')
   .barMode(BarMode.Scrollable)
}

2、TabContentLayout列表内容实现

import { HttpManager, RefreshController, RefreshListView, RequestMethod } from '@app/BaseLibrary';
import LogUtils from '@app/BaseLibrary/src/main/ets/utils/LogUtils';
import { TabContentItemBean } from '../bean/TabContentItemBean';
import { TabContentBean } from '../bean/TabContentBean';
import router from '@ohos.router';

const TAG = 'TabContentLayout--- ';

@Component
export struct TabContentLayout {
   @State controller: RefreshController = new RefreshController()
   @State tabContentItemData: Array<TabContentItemBean> = [];
   @State pageNum: number = 1 //从1开始
   @State isRefresh: boolean = true
   @Prop tabId: number
   private onDataFinish: () => void //数据加载完成回调

   aboutToAppear() {
      LogUtils.info(TAG, "tabId: " + this.tabId)
      this.getTabContentData()
   }

   private getTabContentData() {
      LogUtils.info(TAG, "pageNum: " + this.pageNum)
      HttpManager.getInstance()
         .request<TabContentBean>({
            method: RequestMethod.GET,
            header: { "Content-Type": "application/json" },
            url: `https://www.wanandroid.com/project/list/${this.pageNum}/json?cid=${this.tabId}`
         })
         .then((result: TabContentBean) => {
            LogUtils.info(TAG, "result: " + JSON.stringify(result))
            if (this.isRefresh) {
               this.controller.finishRefresh()
            } else {
               this.controller.finishLoadMore()
            }
            if (result.errorCode == 0) {
               if (this.isRefresh) {
                  this.tabContentItemData = result.data.datas
               } else {
                  this.tabContentItemData = this.tabContentItemData.concat(result.data.datas)
               }
            }
            this.onDataFinish()
         })
         .catch((error) => {
            LogUtils.info(TAG, "error: " + JSON.stringify(error))
            if (this.isRefresh) {
               this.controller.finishRefresh()
            } else {
               this.controller.finishLoadMore()
            }
            this.onDataFinish()
         })
   }

   build() {
      RefreshListView({
         list: this.tabContentItemData,
         controller: this.controller,
         refreshLayout: (item: TabContentItemBean, index: number): void => this.itemLayout(item, index),
         onItemClick: (item: TabContentItemBean, index: number) => {
            LogUtils.info(TAG, "点击了:index: " + index + " item: " + item)
            router.pushUrl({
               url: 'pages/WebPage',
               params: {
                  title: item.title,
                  uriLink: item.link
               }
            }, router.RouterMode.Single)
         },
         onRefresh: () => {
            //下拉刷新
            this.isRefresh = true
            this.pageNum = 0
            this.getTabContentData()
         },
         onLoadMore: () => {
            //上拉加载
            this.isRefresh = false
            this.pageNum++
            this.getTabContentData()
         }
      })
   }

   @Builder
   itemLayout(item: TabContentItemBean, index: number) {
      RelativeContainer() {
         //封面
         Image(item.envelopePic)
            .alt($r('app.media.ic_default_cover'))
            .width(110)
            .height(160)
            .borderRadius(5)
            .id('imageEnvelope')
            .alignRules({
               top: { anchor: '__container__', align: VerticalAlign.Top },
               left: { anchor: '__container__', align: HorizontalAlign.Start }
            })

         //title
         //标题
         Text(item.title)
            .fontColor('#333333')
            .fontWeight(FontWeight.Bold)
            .maxLines(2)
            .textOverflow({
               overflow: TextOverflow.Ellipsis
            })
            .fontSize(18)
            .margin({ left: 15 })
            .maxLines(2)
            .textOverflow({ overflow: TextOverflow.Ellipsis })
            .id("textTitle")
            .alignRules({
               top: { anchor: '__container__', align: VerticalAlign.Top },
               left: { anchor: 'imageEnvelope', align: HorizontalAlign.End },
               right: { anchor: '__container__', align: HorizontalAlign.End }
            })

         //描述
         Text(item.desc)
            .fontColor('#666666')
            .fontSize(16)
            .id("textDesc")
            .margin({ left: 15, top: 15 })
            .maxLines(4)
            .textOverflow({ overflow: TextOverflow.Ellipsis })
            .alignRules({
               top: { anchor: 'textTitle', align: VerticalAlign.Bottom },
               left: { anchor: 'imageEnvelope', align: HorizontalAlign.End },
               right: { anchor: '__container__', align: HorizontalAlign.End }
            })

         //时间
         Text(item.niceDate + "  " + "作者:" + item.author)
            .fontColor('#666666')
            .fontSize(14)
            .margin({ left: 15 })
            .id("textNiceDate")
            .alignRules({
               bottom: { anchor: '__container__', align: VerticalAlign.Bottom },
               left: { anchor: 'imageEnvelope', align: HorizontalAlign.End }
            })
      }
      .width('100%')
      .height(180)
      .padding(10)
      .margin({ left: 10, right: 10, top: 6, bottom: 6 })
      .borderRadius(10)
      .backgroundColor(Color.White)
   }
}

3、项目页面对布局引用

import { LoadingDialog } from '@app/BaseLibrary';
import LogUtils from '@app/BaseLibrary/src/main/ets/utils/LogUtils';
import { ProjectList } from './widget/ProjectList';

@Component
export struct ProjectPage {
   @State projectLoadDataStatus: boolean = false

   aboutToAppear() {
      //弹窗控制器,显示
      this.dialogController.open()
      LogUtils.info("33333333333  ProjectPage  aboutToAppear执行了")
   }

   private dialogController = new CustomDialogController({
      builder: LoadingDialog(),
      customStyle: true,
      alignment: DialogAlignment.Center
   })

   build() {
      Column() {
         ProjectList({ onDataFinish: () => {
            this.dialogController.close()
            this.projectLoadDataStatus = true
         } })
      }
      .visibility(this.projectLoadDataStatus ? Visibility.Visible : Visibility.Hidden)
      .width('100%')
      .height('100%')
   }
}

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

aboutToAppear() {
   this.getProjectListData()
}

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

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

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


http://www.niftyadmin.cn/n/5429678.html

相关文章

王勇:硬科技的下一站 | 演讲嘉宾公布

一、智能耳机与可穿戴专题论坛 智能耳机与可穿戴专题论坛将于3月27日同期举办&#xff01; 智能耳机、可穿戴设备已经逐渐融入我们的生活&#xff0c;它们不仅带来了便捷与舒适&#xff0c;更在悄然改变着我们的生活方式和工作模式。在这里&#xff0c;我们将分享最新的研究成果…

【C++】每日一题 100 相同的树

给你两棵二叉树的根节点 p 和 q &#xff0c;编写一个函数来检验这两棵树是否相同。 如果两个树在结构上相同&#xff0c;并且节点具有相同的值&#xff0c;则认为它们是相同的 #include <iostream>// Definition for a binary tree node. struct TreeNode {int val;Tre…

【DL经典回顾】激活函数大汇总(九)(Hard Swish ReLU6附代码和详细公式)

激活函数大汇总&#xff08;九&#xff09;&#xff08;Hard Swish & ReLU6附代码和详细公式&#xff09; 更多激活函数见激活函数大汇总列表 一、引言 欢迎来到我们深入探索神经网络核心组成部分——激活函数的系列博客。在人工智能的世界里&#xff0c;激活函数扮演着…

使用Python和Wireshark进行数据包分析:简易指南

在网络安全和网络性能优化中&#xff0c;数据包分析是一项至关重要的任务。Wireshark是一个功能强大的网络协议分析工具&#xff0c;而Python则是一种灵活的编程语言&#xff0c;两者结合可以帮助你有效地分析网络数据包。本文将介绍如何使用Python和Wireshark进行数据包分析&a…

基于Python的pygame库的五子棋游戏

安装pygame pip install pygame五子棋游戏代码 """五子棋之人机对战"""import sys import random import pygame from pygame.locals import * import pygame.gfxdraw from collections import namedtupleChessman namedtuple(Chessman, Name…

快手,得物,蓝月亮,蓝禾,奇安信,三七互娱,顺丰,康冠科技,金证科技24春招内推

快手&#xff0c;得物&#xff0c;蓝月亮&#xff0c;蓝禾&#xff0c;奇安信&#xff0c;三七互娱&#xff0c;顺丰&#xff0c;康冠科技&#xff0c;金证科技24春招内推 ①得物 【岗位】技术&#xff0c;设计&#xff0c;供应链&#xff0c;风控&#xff0c;产品&#xff0c;…

代码随想录算法训练营第四十五天|动态规划|70. 爬楼梯 (进阶)、322. 零钱兑换、279.完全平方数

70. 爬楼梯 &#xff08;进阶&#xff09; 文章 假设你正在爬楼梯。需要 n 阶你才能到达楼顶。 每次你可以爬至多m (1 < m < n)个台阶。你有多少种不同的方法可以爬到楼顶呢&#xff1f; 注意&#xff1a;给定 n 是一个正整数。 输入描述&#xff1a;输入共一行&…

案例分析篇01:软件架构设计考点架构风格及质量属性(2024年软考高级系统架构设计师冲刺知识点总结系列文章)

专栏系列文章推荐: 2024高级系统架构设计师备考资料(高频考点&真题&经验)https://blog.csdn.net/seeker1994/category_12593400.html 【历年案例分析真题考点汇总】与【专栏文章案例分析高频考点目录】(2024年软考高级系统架构设计师冲刺知识点总结-案例分析篇-…