6.3:Grid、GridItem
Grid
表示网格布局,它可以设置行数和列数,它和 List
类似,子组件只能是 GridItem
。
6.3.1:Grid定义介绍
interface GridInterface {
(scroller?: Scroller): GridAttribute;
}
1
2
3
- scroller:绑定一个滚动控制器,控制
Grid
的滚动。
简单样例如下所示:
@Entry @Component struct ComponentTest {
private columns: Array<String> = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]
build() {
Column() {
Grid() {
ForEach(this.columns, (item: String) => {
GridItem() {
Text('Text: ' + item)
.fontSize(20)
.backgroundColor('#aabbcc')
.width('100%')
.height(70)
}
})
}
.columnsTemplate("1fr 1fr 1fr")
.columnsGap(10)
.rowsGap(10)
.width('100%')
.height(170)
.backgroundColor(Color.Pink)
}
.padding(10)
.width("100%")
.height("100%")
}
}
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
@Entry @Component class MyView {
var columns: ArrayList<String> = ArrayList(["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"])
func build() {
Column() {
Grid() {
ForEach(this.columns, itemGeneratorFunc: {item: String, idx: Int64 =>
GridItem() {
Text("Text: ${item}")
.fontSize(20)
.backgroundColor(0xaabbcc)
.width(100.percent)
.height(70)
}
})
}
.columnsTemplate("1fr 1fr 1fr")
.columnsGap(10)
.rowsGap(10)
.width(100.percent)
.height(170)
.backgroundColor(0xffc0cb)
}
.padding(10)
.width(100.percent)
.height(100.percent)
}
}
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
样例运行结果如下图所示:
6.3.2:Grid属性介绍
declare class GridAttribute<T> extends CommonMethod<T> {
columnsTemplate(value: string): T;
rowsTemplate(value: string): T;
columnsGap(value: number | string | Resource): T;
rowsGap(value: number | string | Resource): T;
cachedCount(value: number): T;
}
1
2
3
4
5
6
7
columnsTemplate:设置列数,默认值为 1fr ,表示 1 列,参数详解如下:
rowsTemplate:设置列数,默认值为 1fr ,表示 1 行,不设置时默认为 1 行,参数说明和 columnsTemplate
一致。
columnsGap:设置列与列的间距。
rowsGap:设置行与行的间距。
cachedCount:设置 Grid
缓存数量。
6.3.2:Grid事件介绍
declare class GridAttribute<T> extends CommonMethod<T> {
onScrollIndex(event: (first: number) => void): T;
}
1
2
3
onScrollIndex
:当前列表显示的起始位置 item 发生变化时触发该回调。
6.3.3:GridItem定义介绍
interface GridItemInterface {
(): GridItemAttribute;
}
1
2
3
GridItem
为无参定义
6.3.4:GridItem属性介绍
declare class GridItemAttribute<T> extends CommonMethod<T> {
rowStart(value: number): T;
rowEnd(value: number): T;
columnStart(value: number): T;
columnEnd(value: number): T;
forceRebuild(value: boolean): T;
}
1
2
3
4
5
6
7
rowStart:设置当前 item 起始行号。
rowEnd:设置当前 item 结束行号。
columnStart:设置当前 item 起始列号。
columnEnd:设置当前 item 结束列号。
forceRebuild:用于设置在触发组件 build 时是否重新创建此节点。
简单样例如下所示:
@Entry @Component struct ComponentTest {
private columns: Array<String> = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]
build() {
Column() {
Grid() {
ForEach(this.columns, (item: String, index: number) => {
GridItem() {
Text('Text: ' + item)
.fontSize(20)
.backgroundColor('#aabbcc')
.width('100%')
.height(70)
}
.columnStart(index == 0 ? 0 : 0)
.columnEnd(index == 0 ? 2 : 0)
})
}
.columnsTemplate("1fr 1fr 1fr")
.columnsGap(10)
.rowsGap(10)
.width('100%')
.height(170)
.backgroundColor(Color.Pink)
}
.padding(10)
.width("100%")
.height("100%")
}
}
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
样例运行结果如下图所示:
样例中设置了 Grid
为 3 列,并且设置了第 1 个 GridItem
的列从 0 开始到 2 结束,所以 GridItem
就占满整行布局。
6.3.5:完整样例演示
@Entry @Component struct ComponentTest {
private screenWidth: number = px2vp(1080);
private rowSpace: number = 10;
private rowCount: number = 5;
private columnSpace: number = 15;
private columnCount: number = 4;
private itemSize: number = (this.screenWidth - (this.columnSpace * (this.columnCount + 1))) / this.columnCount;
private items: number[] = [];
build() {
Column() {
Text()
.width('100%')
.height('100%')
.layoutWeight(1)
.backgroundColor(Color.Pink)
Grid() {
ForEach(this.items, (item: String, index: number) => {
GridItem() {
Text('Text:' + index)
}
.width('100%')
.height('100%')
.backgroundColor('#bbccdd')
.rowStart(index == 15 ? 3 : 0)
.rowEnd(index == 15 ? 4 : 0)
.columnStart(index == 16 ? 0 : 0)
.columnEnd((index == 16 ? 1 : 0))
})
}
.padding({left: this.columnSpace, right: this.columnSpace})
.columnsTemplate("1fr 1fr 1fr 1fr")
.rowsTemplate("1fr 1fr 1fr 1fr 1fr")
.rowsGap(this.rowSpace)
.columnsGap(this.columnSpace)
.width('100%')
.height(this.itemSize * this.rowCount + this.rowSpace * (this.rowCount - 1))
}
.width('100%')
.height('100%')
}
aboutToAppear(): void {
for(let i = 0; i < 18; i++) {
this.items[i] = i;
}
}
}
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
@Entry @Component class MyView {
var screenWidth: Float64 = 1080.0;
var rowSpace: Float64 = 10.0;
var rowCount: Float64 = 5.0;
var columnSpace: Float64 = 15.0;
var columnCount: Float64 = 4.0;
var itemSize: Float64 = (this.screenWidth - (this.columnSpace * (this.columnCount + 1.0))) / this.columnCount;
var items: ArrayList<Int64> = ArrayList([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 18])
func build() {
Column() {
Text("")
.width(100.percent)
.height(100.percent)
.layoutWeight(1)
.backgroundColor(0xffc0cb)
Grid() {
ForEach(this.items, itemGeneratorFunc: {item: Int64, index: Int64 =>
GridItem() {
Text("Text:${item}")
}
.width(100.percent)
.height(100.percent)
.backgroundColor(0xbbccdd)
.rowStart(rowStart(index))
.rowEnd(rowEnd(index))
.columnStart(columnStart(index))
.columnEnd(columnEnd(index))
})
}
.padding(left: this.columnSpace, right: this.columnSpace)
.columnsTemplate("1fr 1fr 1fr 1fr")
.rowsTemplate("1fr 1fr 1fr 1fr 1fr")
.rowsGap(10)
.columnsGap(15)
.width(100.percent)
.height(400)
}
.width(100.percent)
.height(100.percent)
}
func rowStart(index: Int64): Int32 {
if (index == 15) {
return 3;
}
return 0;
}
func rowEnd(index: Int64): Int32 {
if (index == 15) {
return 4;
}
return 0;
}
func columnStart(index: Int64): Int32 {
return 0;
}
func columnEnd(index: Int64): Int32 {
if (index == 16) {
return 1;
}
return 0;
}
}
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
样例运行结果如下图所示:
(adsbygoogle = window.adsbygoogle || []).push({});