当前位置:网站首页>基于eTS高效开发HarmonyOS课程类应用
基于eTS高效开发HarmonyOS课程类应用
2022-07-15 11:41:00 【InfoQ】
一、整体介绍
二、搭建界面布局

- 导航栏布局

Scroll() {
Column() {
ForEach(this.tabArray.map((item1, index1) => {
return { index: index1, data: item1 };
}), item => {
Text(item.data.superName)
.fontColor(0x696969)
.backgroundColor(this.index == item.index ? 0xffffff : null)
.fontSize(16)
.width('100%')
.height(60)
.textAlign(TextAlign.Center)
.onClick(() => {
if (this.index != item.index) {
this.index = item.index
this.scroller.scrollToIndex(item.data.position)
}
})
}, item => '' + item.data)
}.height('100%')
}.width(100).height('100%').backgroundColor(0xdddddd).scrollBar(BarState.Off)
- 课程内容布局

if (item.tag) {
ListItem() {
Text(item.courseName)
.fontColor(0x696969)
.fontSize(20)
.height(40)
.width('100%')
.padding({ left: 10 })
.backgroundColor(0xefefef)
}.sticky(Sticky.Normal)
Image(item.imageUrl)
.objectFit(ImageFit.Cover)
.width(130)
.height(100)
.margin({ left: 10, top: 10 })
Text(item.courseName)
.fontColor(0x363636)
.fontSize(14)
.margin({ left: 150, top: 12 })
.maxLines(2)
.textOverflow({ overflow: TextOverflow.Clip })
Text(item.price == 0 ? '免费' : '¥' + item.price)
.fontColor(0xff6600)
.fontSize(24)
.position({ x: 0, y: '100%' })
.markAnchor({ x: 0, y: '100%' })
.margin({ bottom: 18, left: 150 })
Divider()
.margin({ left: 10, right: 10 })
.color(0xefefef)
.strokeWidth(0.7)
.position({ x: 0, y: '100%' })
.markAnchor({ x: 0, y: '100%' })
三、构建数据模型
- 定义数据模型
export class TabItem {
position: number; // 点击该分类时课程内容滑动到的位置
superName: string; // 课程分类标题
constructor(position: number, superName: string) {
this.position = position;
this.superName = superName;
}
}
export class CourseItem {
tag: boolean; // 是否此类别课程的头部
index: number; // 课程所对应课程类别的索引位置
courseName: string; // 课程名称
imageUrl: string; // 图片地址
price: number; // 价格
constructor(tag: boolean, index: number, courseName: string, imageUrl: string, price: number) {
this.tag = tag;
this.index = index;
this.courseName = courseName;
this.imageUrl = imageUrl;
this.price = price;
}
}
- 预置数据
const LinkData: any[] = [
{
"superId": 1,
"superName": "热门课程",
"id": 1,
"courseName": "应用市场介绍",
"imageUrl": "/image/image1.jpg",
"price": 0
},
{
"superId": 1,
"superName": "热门课程",
"id": 2,
"courseName": "上架流程",
"imageUrl": "/image/image2.jpg",
"price": 100
},
...
]
aboutToAppear() {
// 延时数据加载
setTimeout(() => {
let linkDataItem = getLinkData();
this.tabArray = linkDataItem.tabArray;
this.listArray = linkDataItem.listArray;
this.requestSuccess = true;
}, 2000)
}
let superId: number = 0
model.forEach((item) => {
if (superId != item.superId) {
let tabItem = new TabItem(this.listArray.length, item.superName);
this.tabArray.push(tabItem)
let courseItem = new CourseItem(true, this.tabArray.length - 1, item.superName, '', 0);
this.listArray.push(courseItem)
}
})
ForEach(this.listArray, item => {
if (item.tag) {
ListItem() {
......
}.sticky(Sticky.Normal)
} else {
ListItem() {
Stack({ alignContent: Alignment.TopStart }) {
......
}.height(120)
}
}
}, item => '' + item)
四、实现界面联动

- 导航栏高亮显示
@State index: number= 0; // 导航栏课程分类的索引
Text(item.data.superName)
.backgroundColor(this.index == item.index ? 0xffffff : null)
private scroller: Scroller = new Scroller()
List({ scroller: this.scroller }) {
......
}
.onScrollIndex((firstIndex: number) => {
if (this.index != this.listArray[firstIndex].index) {
this.index = this.listArray[firstIndex].index
}
})
Text(item.data.superName)
.onClick(() => {
if (this.index != item.index) {
this.index = item.index
this.scroller.scrollToIndex(item.data.position)
}
})

边栏推荐
- 2022-04-20 unity beginner 9 - others
- Isolation level of MySQL
- 易基因|ENCODE组蛋白ChIP-seq和转录因子ChIP-seq数据标准及处理流程
- Landing DDD (7) - some misunderstandings in tactical design
- Experiment 2 Data modeling of after sales service management system
- 02 stm32cubemx new project
- three. JS infinite running VR games
- [benefit activity] stack a buff for your code! Click "tea" to receive the gift
- 2022-04-18 unity getting started 3 - script Basics
- [comprehensive pen test] difficulty 2/5, recursive application, prefix and optimization
猜你喜欢

Yunna dynamic loop monitoring system realizes unmanned rapid fault handling

2022-04-18 unity getting started 1 - window layout

1、OLED简单驱动

Isolation level of MySQL

MQTT---SUBSCRIBE和SUBACK

MySQL的隔离级别

Is there a future for the Internet?

Event preview | Apache Doris x Apache seatunnel joint meetup to start registration!

Google recommends using sealed and remotemediator in projects

Dragon lizard community recruitment Promotion Ambassador & experience officer| Everyone can participate in open source
随机推荐
Fragment(四)常见问题
IDEA Smart Checkout和Force Checkout区别
[design topics] project summary of graduation project based on STM32 - 350 cases
2022-04-18 unity getting started 1 - window layout
2022-04-20 Unity入门7——物理系统之碰撞检测
【招募】2022向光奖|年度ESG影响力投资奖正式启动!
Use iceberg in CDP to pressurize the data Lake warehouse
Chapter 8 delegates, lambda expressions, and events
Easy gene encode histone chip SEQ and transcription factor chip SEQ data standard and processing flow
Landing DDD (7) - some misunderstandings in tactical design
Fragment(三)ViewPager中使用Fragment
【综合笔试题】难度 2/5,递归运用及前缀和优化
Difference between idea smart checkout and force checkout
Experiment 2 Data modeling of after sales service management system
[design topics] project summary of graduation design topics based on C51 single chip microcomputer - 500 cases
MySQL的隔离级别
Alipay sandbox app login failed. There is no problem with the account
Fragment (IV) common problems
2022 global developer salary PK: China ranks 19th, and using go language is the most profitable
QT连接MySQL