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 16// 导入性能打点模块 17import hiTraceMeter from "@ohos.hiTraceMeter"; 18 19@Component 20struct TextItem { 21 @State item: string = ""; 22 23 build() { 24 Text(this.item) 25 .fontSize(16) 26 .backgroundColor(0xF9CF93) 27 .width('100%') 28 .height(80) 29 .textAlign(TextAlign.Center) 30 } 31 32 aboutToAppear() { 33 // 结束打点任务 34 hiTraceMeter.finishTrace("useColumnStartColumnEnd", 1); 35 } 36} 37 38class MyDataSource implements IDataSource { 39 private dataArray: string[] = []; 40 41 public pushData(data: string): void { 42 this.dataArray.push(data); 43 } 44 45 public totalCount(): number { 46 return this.dataArray.length; 47 } 48 49 public getData(index: number): string { 50 return this.dataArray[index]; 51 } 52 53 registerDataChangeListener(listener: DataChangeListener): void { 54 } 55 56 unregisterDataChangeListener(listener: DataChangeListener): void { 57 } 58} 59 60@Component 61export struct GridColumnStartView { 62 private datasource: MyDataSource = new MyDataSource(); 63 scroller: Scroller = new Scroller(); 64 65 aboutToAppear() { 66 for (let i = 1; i < 2000; i++) { 67 this.datasource.pushData(i + ''); 68 } 69 } 70 71 build() { 72 Column({ space: 5 }) { 73 Text($r('app.string.use_column_start')) 74 .fontColor(0xCCCCCC) 75 .fontSize(9) 76 .width('90%') 77 Grid(this.scroller) { 78 LazyForEach(this.datasource, (item: string, index: number) => { 79 if ((index % 4) === 0) { 80 GridItem() { 81 TextItem({ item: item }) 82 }.columnStart(0).columnEnd(2) 83 } 84 else { 85 GridItem() { 86 TextItem({ item: item }) 87 } 88 } 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('scrollToIndexBtn') 100 .onClick(() => { 101 // 开始打点任务 102 hiTraceMeter.startTrace("useColumnStartColumnEnd", 1); 103 this.scroller.scrollToIndex(1900); 104 }) 105 } 106 .width("100%") 107 .margin({ top: 5 }) 108 } 109}