鸿蒙ArkUI 宫格+列表+HttpAPI实现

简介: 本文介绍了如何在鸿蒙系统中利用ArkUI组件构建一个带有轮播图、九宫格和图文列表的应用,同时展示了如何通过axios鸿蒙扩展库加载第三方HTTPAPI数据并动态显示。

鸿蒙ArkUI学习实现一个轮播图、一个九宫格、一个图文列表。然后请求第三方HTTPAPI加载数据,使用了axios鸿蒙扩展库来实现第三方API数据加载并动态显示数据。

import {
    navigateTo
} from '../common/Page'
 
import axios, {
    AxiosResponse
} from '@ohos/axios'
 
interface IDataDataAttr {
    "title": string
}
interface IDataData {
    "img": string,
    "remark": string,
    "id": number,
    "title": string,
    "attr": IDataDataAttr
}
interface IData {
    "msg": string,
    "code": number,
    "data": IDataData[]
}
 
@Entry
@Component
export struct Index {
 
    @Provide data: IData = {
        "code": 0,
        "msg": "",
        "data": [{
            "title": "",
            "remark": "",
            "id": 0,
            "attr": {
                "title": ""
            },
            "img": ""
        }]
    }
 
 
    async dataApi() {
        try {
            const response: AxiosResponse = await axios.post < IData,
            AxiosResponse < IData > , null > ('https://php.diygw.com/article.php');
            this.data = response ? response.data : null
        } catch (error) {
            console.error(JSON.stringify(error));
        }
    }
    async onPageShow() {
        await this.dataApi()
    }
 
    async aboutToAppear() {
        await this.onPageShow()
    }
 
    build() {
        Column() {
            Navigation()
                .width('100%')
                .height('56vp')
                .backgroundColor('#07c160')
                .title(this.NavigationTitle())
                .titleMode(NavigationTitleMode.Mini)
                .align(Alignment.Center)
                .hideBackButton(true)
            Scroll() {
                Flex({
                    direction: FlexDirection.Column
                }) {
                    Flex() {
                        Swiper() {
                            Column() {
                                Image($r('app.media.pic1')).objectFit(ImageFit.Fill).width('100%').height('150vp')
                                Text('滑块一').width('100%').textAlign(TextAlign.Start).backgroundColor("rgba(0,0,0,0.28)").padding(10).position({
                                    y: 110
                                })
                            }
                            Column() {
                                Image($r('app.media.pic2')).objectFit(ImageFit.Fill).width('100%').height('150vp')
                                Text('滑块二').width('100%').textAlign(TextAlign.Start).backgroundColor("rgba(0,0,0,0.28)").padding(10).position({
                                    y: 110
                                })
                            }
                            Column() {
                                Image($r('app.media.pic3')).objectFit(ImageFit.Fill).width('100%').height('150vp')
                                Text('滑块三').width('100%').textAlign(TextAlign.Start).backgroundColor("rgba(0,0,0,0.28)").padding(10).position({
                                    y: 110
                                })
                            }
                        }.interval(3000).autoPlay(true).loop(true)
                        .indicatorStyle({
                            size: 30,
                            selectedColor: '#fff',
                            color: 'rgba(51, 51, 51, 0.39)'
                        })
                    }
                    Grid() {
                        ForEach(this.data.data, (item) => {
                            GridItem() {
                                Column({
                                    space: 5
                                }) {
                                    Image(item.img).objectFit(ImageFit.Fill).width('42vp').height('42vp')
                                    Text(item.title).fontSize('12fp').width('100%').textAlign(TextAlign.Center)
                                }.width('100%')
                            }
                        }, item => JSON.stringify(item));
                    }.padding({
                        top: '10vp',
                        bottom: '10vp'
                    }).height(Math.ceil(this.data.data.length / 3) * 71 + 20).columnsTemplate('1fr 1fr 1fr ')
                    .rowsGap(15).layoutDirection(GridDirection.Row)
                    List() {
                        ListItem() {
                            Flex({
                                direction: FlexDirection.Row,
                                alignItems: ItemAlign.Center,
                            }) {
                                Image($r('app.media.grid1')).flexShrink(0).objectFit(ImageFit.Fill).width('42vp').height('42vp')
                                Column() {
                                    Text('菜单一').fontSize('14fp').width('100%')
                                    Text('说明文字').fontSize('12fp').width('100%')
                                }.padding({
                                    left: 5
                                })
                                Image($r('app.media.ic_arrow')).flexShrink(0).objectFit(ImageFit.Contain).width('12vp').height('24vp')
                            }.width('100%')
                        }.padding(15).borderWidth({
                            bottom: 1
                        }).borderColor('#efefef')
                        ListItem() {
                            Flex({
                                direction: FlexDirection.Row,
                                alignItems: ItemAlign.Center,
                            }) {
                                Image($r('app.media.grid2')).flexShrink(0).objectFit(ImageFit.Fill).width('42vp').height('42vp')
                                Column() {
                                    Text('菜单二').fontSize('14fp').width('100%')
                                    Text('说明文字').fontSize('12fp').width('100%')
                                }.padding({
                                    left: 5
                                })
                                Image($r('app.media.ic_arrow')).flexShrink(0).objectFit(ImageFit.Contain).width('12vp').height('24vp')
                            }.width('100%')
                        }.padding(15).borderWidth({
                            bottom: 1
                        }).borderColor('#efefef')
                        ListItem() {
                            Flex({
                                direction: FlexDirection.Row,
                                alignItems: ItemAlign.Center,
                            }) {
                                Image($r('app.media.grid3')).flexShrink(0).objectFit(ImageFit.Fill).width('42vp').height('42vp')
                                Column() {
                                    Text('菜单三').fontSize('14fp').width('100%')
                                    Text('说明文字').fontSize('12fp').width('100%')
                                }.padding({
                                    left: 5
                                })
                                Image($r('app.media.ic_arrow')).flexShrink(0).objectFit(ImageFit.Contain).width('12vp').height('24vp')
                            }.width('100%')
                        }.padding(15).borderWidth({
                            bottom: 1
                        }).borderColor('#efefef')
                    }
 
                }.height('100%')
            }.height('100%').layoutWeight(1)
        }.alignItems(HorizontalAlign.Start).height('100%')
    }
 
    @Builder
    NavigationTitle() {
        Column() {
            Text('首页')
                .width('100%')
                .textAlign(TextAlign.Center)
                .height('28vp')
                .fontSize('20fp')
                .fontWeight(500)
                .fontColor('#fff')
        }
    }
 
 
}
目录
相关文章
|
25天前
|
弹性计算 人工智能 架构师
阿里云携手Altair共拓云上工业仿真新机遇
2024年9月12日,「2024 Altair 技术大会杭州站」成功召开,阿里云弹性计算产品运营与生态负责人何川,与Altair中国技术总监赵阳在会上联合发布了最新的“云上CAE一体机”。
阿里云携手Altair共拓云上工业仿真新机遇
|
17天前
|
存储 关系型数据库 分布式数据库
GraphRAG:基于PolarDB+通义千问+LangChain的知识图谱+大模型最佳实践
本文介绍了如何使用PolarDB、通义千问和LangChain搭建GraphRAG系统,结合知识图谱和向量检索提升问答质量。通过实例展示了单独使用向量检索和图检索的局限性,并通过图+向量联合搜索增强了问答准确性。PolarDB支持AGE图引擎和pgvector插件,实现图数据和向量数据的统一存储与检索,提升了RAG系统的性能和效果。
|
4天前
|
JSON 自然语言处理 数据管理
阿里云百炼产品月刊【2024年9月】
阿里云百炼产品月刊【2024年9月】,涵盖本月产品和功能发布、活动,应用实践等内容,帮助您快速了解阿里云百炼产品的最新动态。
阿里云百炼产品月刊【2024年9月】
|
1天前
|
人工智能 Rust Java
10月更文挑战赛火热启动,坚持热爱坚持创作!
开发者社区10月更文挑战,寻找热爱技术内容创作的你,欢迎来创作!
253 12
|
19天前
|
人工智能 IDE 程序员
期盼已久!通义灵码 AI 程序员开启邀测,全流程开发仅用几分钟
在云栖大会上,阿里云云原生应用平台负责人丁宇宣布,「通义灵码」完成全面升级,并正式发布 AI 程序员。
|
21天前
|
机器学习/深度学习 算法 大数据
【BetterBench博士】2024 “华为杯”第二十一届中国研究生数学建模竞赛 选题分析
2024“华为杯”数学建模竞赛,对ABCDEF每个题进行详细的分析,涵盖风电场功率优化、WLAN网络吞吐量、磁性元件损耗建模、地理环境问题、高速公路应急车道启用和X射线脉冲星建模等多领域问题,解析了问题类型、专业和技能的需要。
2579 22
【BetterBench博士】2024 “华为杯”第二十一届中国研究生数学建模竞赛 选题分析
|
3天前
|
存储 人工智能 搜索推荐
数据治理,是时候打破刻板印象了
瓴羊智能数据建设与治理产品Datapin全面升级,可演进扩展的数据架构体系为企业数据治理预留发展空间,推出敏捷版用以解决企业数据量不大但需构建数据的场景问题,基于大模型打造的DataAgent更是为企业用好数据资产提供了便利。
169 2
|
1天前
|
编译器 C#
C#多态概述:通过继承实现的不同对象调用相同的方法,表现出不同的行为
C#多态概述:通过继承实现的不同对象调用相同的方法,表现出不同的行为
101 65
|
21天前
|
机器学习/深度学习 算法 数据可视化
【BetterBench博士】2024年中国研究生数学建模竞赛 C题:数据驱动下磁性元件的磁芯损耗建模 问题分析、数学模型、python 代码
2024年中国研究生数学建模竞赛C题聚焦磁性元件磁芯损耗建模。题目背景介绍了电能变换技术的发展与应用,强调磁性元件在功率变换器中的重要性。磁芯损耗受多种因素影响,现有模型难以精确预测。题目要求通过数据分析建立高精度磁芯损耗模型。具体任务包括励磁波形分类、修正斯坦麦茨方程、分析影响因素、构建预测模型及优化设计条件。涉及数据预处理、特征提取、机器学习及优化算法等技术。适合电气、材料、计算机等多个专业学生参与。
1578 16
【BetterBench博士】2024年中国研究生数学建模竞赛 C题:数据驱动下磁性元件的磁芯损耗建模 问题分析、数学模型、python 代码
|
4天前
|
Linux 虚拟化 开发者
一键将CentOs的yum源更换为国内阿里yum源
一键将CentOs的yum源更换为国内阿里yum源
257 2