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

news/2024/7/21 11:33:28 标签: harmonyos, 学习, 华为

鸿蒙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')
        }
    }


}


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

相关文章

JeecgBoot3.61 实现实体类对象和请求参数映射构建查询,主要解决需要根据数据库外键展示相关信息的需求

JeecgBoot3.61 实现实体类对象和请求参数映射构建查询&#xff0c;主要解决需要根据数据库外键展示相关信息的需求 有一张表&#xff0c;例如有一张班级表&#xff0c;有一个外键叫teacher_id ,为教师表的主键&#xff0c;需要在班级列表展示班主任的姓名、年龄、性别 另外还有…

mac上搭建hbase伪集群

1. 前言 之前我们已经搭建过了 hbase单点环境&#xff0c;(单机版搭建参见&#xff1a; https://blog.csdn.net/a15835774652/article/details/135569456) 但是 为了模拟一把集群环境 我们还是尝试搭建一个伪集群版 2. 环境准备 jdk环境 1.8hdfs &#xff08;hadoop环境 可选…

【C++干货铺】C++中的IO流和文件操作

个人主页点击直达&#xff1a;小白不是程序媛 C系列专栏&#xff1a;C干货铺 代码仓库&#xff1a;Gitee 目录 C语言的输入输出 流是什么&#xff1f; C的IO流 C标准IO流 C文件IO流 文本文件读写 二进制文件的读写 stringstream的简单介绍 将数值类型数据格式化为字…

腐蚀及膨胀的python实现——数字图像处理

原理 像处理中的腐蚀和膨胀是形态学操作的两个基本概念&#xff0c;它们广泛应用于图像预处理、特征提取和其他图像分析任务。 腐蚀&#xff08;Erosion&#xff09; 腐蚀操作可以看作是图像中形状的"收缩"。其基本原理是使用一个结构元素&#xff08;通常是一个小…

Android底部导航栏创建——ViewPager + RadioGroup

Android底部导航栏有多种实现方式&#xff0c;本文详解其中的ViewPager RadioGroup方式的实现步骤。 我们先来看以下看一下最终做出的效果&#xff0c;使大家有个基本概念。 本结构特点&#xff1a; 1&#xff0c;ViewPager部分触摸左右滑动切换页面&#xff0c;RadioGroup部…

mac 10.15.7 Unity 2021.3.14 XCode 12.4 -> Unity IOS 自动安装 Cocoapods 失败解决方法

自己这两天在用Unity开发IOS时&#xff0c;遇到了安装Cocoapods失败的问题&#xff0c;记录一下问题及解决方法&#xff0c;便于自己后续查看&#xff0c;以及有相同遭遇的人查看 发生场景&#xff1a;打开 unity&#xff0c;触发自动安装 Cocoapods -> 安装失败&#xff08…

LeetCode 热题 100 | 普通数组

目录 1 53. 最大子数组和 2 56. 合并区间 3 189. 轮转数组 4 238. 除自身以外数组的乘积 5 41. 缺失的第一个正数 菜鸟做题第二周&#xff0c;语言是 C 1 53. 最大子数组和 题眼&#xff1a;“子数组是数组中的一个连续部分。” 遍历数组&#xff0c;问每一个元素…

幻兽帕鲁服务器多少钱一台?腾讯云新版报价

腾讯云幻兽帕鲁服务器4核16G、8核32G和16核64G配置可选&#xff0c;4核16G14M带宽66元一个月、277元3个月&#xff0c;8核32G22M配置115元1个月、345元3个月&#xff0c;16核64G35M配置580元年1个月、1740元3个月、6960元一年&#xff0c;腾讯云百科txybk.com分享腾讯云幻兽帕鲁…