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 16class ChildDataSource implements IDataSource { 17 private list: string[] = []; 18 19 constructor(list: string[]) { 20 this.list = list; 21 } 22 23 totalCount(): number { 24 return this.list.length; 25 } 26 27 getData(index: number): string { 28 return this.list[index]; 29 } 30 31 registerDataChangeListener(_: DataChangeListener): void { 32 } 33 34 unregisterDataChangeListener(): void { 35 } 36} 37 38@Component 39export struct FrameTimelineAfterOptimization { 40 // 创建长度500的空数组,使用每个元素的索引转成的字符串作为元素的值,生成包含字符串0-499的数组 41 @State children: string[] = Array.from<undefined, string>(Array(500).fill(undefined), (item: undefined, index) => index.toString()); 42 @State data: ChildDataSource = new ChildDataSource(this.children) 43 build() { 44 Scroll() { 45 Grid() { 46 LazyForEach(this.data, (item: string) => { 47 GridItem() { 48 Text(item) 49 .fontSize(32) 50 } 51 }, (item: string) => item) 52 } 53 .cachedCount(80) 54 .columnsTemplate('1fr 1fr 1fr 1fr') 55 .columnsGap(0) 56 .rowsGap(0) 57 .size({ width: "100%", height: "100%" }) 58 } 59 } 60}