1/* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import hiTraceMeter from '@ohos.hiTraceMeter'; 17 18@Component 19struct TextItem { 20 @State item: string = " "; 21 22 build() { 23 Text(this.item) 24 .fontSize(16) 25 .textAlign(TextAlign.Center) 26 .backgroundColor(0xF9CF93) 27 .width('100%') 28 .height(80) 29 } 30 31 aboutToAppear() { 32 hiTraceMeter.finishTrace("useGridLayoutOptions", 1); 33 } 34} 35 36class MyDataSource implements IDataSource { 37 private dataArray: string[] = []; 38 39 public pushData(data: string): void { 40 this.dataArray.push(data); 41 } 42 43 // 数据源的数据总量 44 public totalCount(): number { 45 return this.dataArray.length; 46 } 47 48 public getData(index: number): string { 49 return this.dataArray[index]; 50 } 51 52 registerDataChangeListener(listener: DataChangeListener): void { 53 } 54 55 unregisterDataChangeListener(listener: DataChangeListener): void { 56 } 57} 58 59@Component 60export struct GridLayoutOptionsView { 61 // 数据源 62 private dataSource: MyDataSource = new MyDataSource(); 63 scroller: Scroller = new Scroller(); 64 private irregularData: number[] = []; 65 layoutOptions: GridLayoutOptions = { 66 regularSize: [1, 1], 67 irregularIndexes: this.irregularData, 68 }; 69 70 aboutToAppear() { 71 for (let i = 1; i <= 2000; i++) { 72 this.dataSource.pushData(i + ""); 73 if ((i - 1) % 4 === 0) { 74 this.irregularData.push(i - 1); 75 } 76 } 77 } 78 79 build() { 80 Column({ space: 5 }) { 81 Text($r('app.string.use_layout_options')) 82 .fontColor(0xCCCCCC) 83 .fontSize(16) 84 .width('90%') 85 Grid(this.scroller, this.layoutOptions) { 86 LazyForEach(this.dataSource, (item: string, index: number) => { 87 GridItem() { 88 TextItem({ item: item }) 89 } 90 }, (item: string) => item) 91 } 92 .columnsTemplate("1fr 1fr 1fr") 93 .columnsGap(10) 94 .rowsGap(10) 95 .width("90%") 96 .height("40%") 97 98 Button("scrollToIndex:1900") 99 .id('scrollToIndexTwoBtn') 100 .onClick(() => { 101 // 开始打点任务 102 hiTraceMeter.startTrace("useGridLayoutOptions", 1); 103 this.scroller.scrollToIndex(1900); 104 }) 105 } 106 .width("100%") 107 .margin({ top: 5 }) 108 } 109}