GridLayout01Component.ets
3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import { Action, ContentDTO, Params } from 'wdBean';
import { CommonConstants, ConfigConstants } from 'wdConstant';
import { Logger } from 'wdKit';
import { WDRouterRule } from 'wdRouter';
const TAG = 'GridLayout01Component';
/**
* 热点视频推荐
* Grid_Layout-03
* 支持添加视频、直播内容
*/
@Component
export struct GridLayout01Component {
@StorageLink('currentBreakpoint') currentBreakpoint: string = 'xs';
@State dataList?: ContentDTO[] = undefined
@State adapterBeanList?: ContentDTO[] = []
aboutToAppear() {
this.buildAdapterBeanList(this.currentBreakpoint)
}
aboutToDisappear() {
}
build() {
GridRow({
columns: 12,
gutter: { x: vp2px(2.5), y: vp2px(2.5) },
}) {
ForEach(this.dataList, (programmeBean: ContentDTO, index: number) => {
GridCol({ span: { xs: 6, sm: 6, md: 3, lg: 3 } }) {
this.buildItemTopImgBottomTxt01(programmeBean, index)
}
})
}
.margin({ left: $r('app.float.main_margin'), right: $r('app.float.main_margin'), bottom: 5 })
.onBreakpointChange((currentBreakpoint: string) => {
this.currentBreakpoint = currentBreakpoint
this.buildAdapterBeanList(currentBreakpoint)
})
}
// 只从list最后添加/补充数据item,不截断list
buildAdapterBeanList(currentBreakpoint: string): void {
if (!this.dataList || this.dataList.length == 0) {
return
}
let divisor: number = 1; // 除数
switch (currentBreakpoint) {
case 'xs': // break; // pass through
case 'sm':
divisor = 2;
break;
case 'md': // break; // pass through
case 'lg': // break; // pass through
default:
divisor = 4;
break;
}
this.adapterBeanList = [] // 重置
this.adapterBeanList.push(...this.dataList.reverse());
let mod = this.dataList.length % divisor // 余数
if (mod == 0) {
return
}
// 在尾部补全数据
let complementIndex = 0
while (complementIndex < mod) {
// 从原有数据前(循环)依次取数据,补充到列表尾部,
this.adapterBeanList.push(this.dataList[complementIndex])
complementIndex++
}
return
}
@Builder
buildItemTopImgBottomTxt01(item: ContentDTO, index: number) {
Column() {
Image(item.hImageUrl)
.width('100%')
.aspectRatio(16 / 9)
.borderRadius($r("app.float.image_border_radius"))
.objectFit(ImageFit.Cover)
Text(item.title)
.width('100%')
.margin({ top: 4, left: 6, right: 6 })
.fontWeight(FontWeight.Normal)
.textAlign(TextAlign.Start)
.fontSize($r('app.float.font_size_14'))
.fontColor($r('app.color.color_333333'))
.maxLines(2)
.textOverflow({ overflow: TextOverflow.Ellipsis })
}
.backgroundColor(Color.White)
.hoverEffect(HoverEffect.Scale)
.alignItems(HorizontalAlign.Start)
.onClick((event: ClickEvent) => {
Logger.info(TAG, `buildItemTopImgBottomTxt01 onClick event index: ${index}`);
let taskAction: Action = {
type: 'JUMP_H5_BY_WEB_VIEW',
params: {
url: ConfigConstants.DETAIL_URL
} as Params,
};
WDRouterRule.jumpWithAction(taskAction)
})
}
}