鸿蒙HarmonyOS NEXT入门,影院列表实现(九)

1. 涉及到的技术点

  1. @Extend装饰器的使用
  2. 列表List的使用

2.官网文档教程

  1. @Extend装饰器: https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/arkts-extend-V5
  2. 列表 (List) https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/arkts-layout-development-create-list-V5

3.demo实现过程

  1. @Extend装饰器的使用
    1. 和@Styles不同,@Extend仅支持在全局定义,不支持在组件内部定义。
    2. 和@Styles不同,@Extend支持封装指定组件的私有属性、私有事件和自身定义的全局方法。
    3. 和@Styles不同,@Extend装饰的方法支持参数,开发者可以在调用时传递参数,调用遵循TS方法传值调用。
/**
 * 和@Styles不同,@Extend仅支持在全局定义,不支持在组件内部定义
 */
@Extend(Text)
function FancyTextStyle(fontColor: string, backgroundColor: string) {
  .fontColor(fontColor)
  .backgroundColor(backgroundColor)
  .fontSize(10)
  .fontWeight(800)
  .padding({
    left: 6,
    right: 6,
    top
    : 3,
    bottom: 3
  })
  .borderRadius(2)

}

  1. 创建影院数据实体CinemaInfo

在项目ets->新建entity文件夹->新建CinemaInfo.ets类

export class CinemaInfo {
  cinema_name: string = ''
  cinema_address: string = ''

  constructor(cinema_name: string, cinema_address: string) {
    this.cinema_name = cinema_name
    this.cinema_address = cinema_address
  }
}
  1. 在aboutToAppear()生命周期函数中,初始化影院数据
 aboutToAppear(): void {
    this.cinemaInfoList.push(new CinemaInfo('0090激光影城', '金山区朱泾镇沈浦泾路501A-01'))
    this.cinemaInfoList.push(new CinemaInfo('1929电影公园(XWORLD青浦吾悦店)',
      '青浦区淀山湖大道漕盈路218号吾悦广场内5楼'))
    this.cinemaInfoList.push(new CinemaInfo('AMG大光明影城(紫叶广场店)', '闵行区紫星路4881291F-D,3,4楼'))
    this.cinemaInfoList.push(new CinemaInfo('CMC大光明影城(莲花店)', '静安区万荣路777号大宁音乐广场A1层'))
    this.cinemaInfoList.push(new CinemaInfo('CineMaple红叶巨幕影城', '普陀区中山北路3300号环球港44038室'))
    this.cinemaInfoList.push(new CinemaInfo('DFC影城金山店(LUXE 4K )', '浦东新区花木街道芳甸路208-233号联洋广场A2层'))
    this.cinemaInfoList.push(new CinemaInfo('上影国际影城(新业坊店)',
      '闵行区莘庄镇沪闵路6088号莘庄凯德龙之梦购物中心4楼'))
  }
  1. 使用List实现列表
List({ space: 10 }) {
        ForEach(this.cinemaInfoList, (cinemaInfo: CinemaInfo, index: number) => {
          ListItem() {
            Column() {
              Row() {
                Text(cinemaInfo.cinema_name).layoutWeight(1)
                Button('选座购票').height(30).backgroundColor('#e22418').width(77).fontSize(11)
              }

              Row() {
                Image($r('app.media.img_location')).width(20).margin({ left: -5 })
                Text(cinemaInfo.cinema_address).fontSize(12).fontColor('#999999')
              }.width('100%').margin({ top: 10 })

              Row() {
                Text('4DX厅')
                  .FancyTextStyle('#f5810d', '#fef8e0')
                Text('杜比全景声厅')
                  .FancyTextStyle('#d67477', '#ffeef4').margin({ left: 10, right: 10 })
                Text('LUXE巨幕厅')
                  .FancyTextStyle('#99763c', '#f4ead4')
              }.width('100%').margin({ top: 10 })


              Row() {

              }.backgroundColor('#f5f5f5').height(1).width('100%').margin({ top: 10 })

            }.width('100%').padding({ left: 10, right: 10 })
          }
        })
      }.margin({ top: 20 })
  1. 影院列表页具体代码实现
import { router } from '@kit.ArkUI';
import { CinemaInfo } from '../entity/CinemaInfo';

/**
 * 和@Styles不同,@Extend仅支持在全局定义,不支持在组件内部定义
 */
@Extend(Text)
function FancyTextStyle(fontColor: string, backgroundColor: string) {
  .fontColor(fontColor)
  .backgroundColor(backgroundColor)
  .fontSize(10)
  .fontWeight(800)
  .padding({
    left: 6,
    right: 6,
    top
    : 3,
    bottom: 3
  })
  .borderRadius(2)

}


class MovieInfo {
  movie_title: string = ''
}

@Entry
@Component
struct CinemaListPage {
  @State cinemaInfoList: Array<CinemaInfo> = []
  @State movieInfo: MovieInfo = {
    movie_title: ''
  }

  aboutToAppear(): void {
    //接收跳转传值
    this.movieInfo = router.getParams() as MovieInfo

    this.cinemaInfoList.push(new CinemaInfo('0090激光影城', '金山区朱泾镇沈浦泾路501A-01'))
    this.cinemaInfoList.push(new CinemaInfo('1929电影公园(XWORLD青浦吾悦店)',
      '青浦区淀山湖大道漕盈路218号吾悦广场内5楼'))
    this.cinemaInfoList.push(new CinemaInfo('AMG大光明影城(紫叶广场店)', '闵行区紫星路4881291F-D,3,4楼'))
    this.cinemaInfoList.push(new CinemaInfo('CMC大光明影城(莲花店)', '静安区万荣路777号大宁音乐广场A1层'))
    this.cinemaInfoList.push(new CinemaInfo('CineMaple红叶巨幕影城', '普陀区中山北路3300号环球港44038室'))
    this.cinemaInfoList.push(new CinemaInfo('DFC影城金山店(LUXE 4K )', '浦东新区花木街道芳甸路208-233号联洋广场A2层'))
    this.cinemaInfoList.push(new CinemaInfo('上影国际影城(新业坊店)',
      '闵行区莘庄镇沪闵路6088号莘庄凯德龙之梦购物中心4楼'))
  }

  build() {
    Column() {
      Row() {
        Image($r('app.media.img_back')).width(26).onClick(() => {
          router.back()
        })
        //标题栏
        Text('影院列表')
          .fontSize(21)
          .fontWeight(600)
      }
      .width('100%')


      List({ space: 10 }) {
        ForEach(this.cinemaInfoList, (cinemaInfo: CinemaInfo, index: number) => {
          ListItem() {
            Column() {
              Row() {
                Text(cinemaInfo.cinema_name).layoutWeight(1)
                Button('选座购票').height(30).backgroundColor('#e22418').width(77).fontSize(11)
              }

              Row() {
                Image($r('app.media.img_location')).width(20).margin({ left: -5 })
                Text(cinemaInfo.cinema_address).fontSize(12).fontColor('#999999')
              }.width('100%').margin({ top: 10 })

              Row() {
                Text('4DX厅')
                  .FancyTextStyle('#f5810d', '#fef8e0')
                Text('杜比全景声厅')
                  .FancyTextStyle('#d67477', '#ffeef4').margin({ left: 10, right: 10 })
                Text('LUXE巨幕厅')
                  .FancyTextStyle('#99763c', '#f4ead4')
              }.width('100%').margin({ top: 10 })


              Row() {

              }.backgroundColor('#f5f5f5').height(1).width('100%').margin({ top: 10 })

            }.width('100%').padding({ left: 10, right: 10 })
          }
        })
      }.margin({ top: 20 })

    }
    .height('100%')
    .width('100%')
  }
}

4. 运行效果图

在这里插入图片描述

  • 8
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

浩宇软件开发

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值