1/** 2 * Copyright (c) 2024 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@Entry 16@Component 17struct ListExample_set { 18 private arr: number[] = [] 19 private scroller: ListScroller = new ListScroller() 20 @State listSpace: number = 10 21 @State listChildrenSize: ChildrenMainSize = new ChildrenMainSize(100) 22 aboutToAppear(){ 23 // 初始化数据源。 24 for (let i = 0; i < 10; i++) { 25 this.arr.push(i) 26 } 27 // 前5个item的主轴大小不是默认大小100,因此需要通过ChildrenMainSize通知List。 28 this.listChildrenSize.splice(0, 5, [300, 300, 300, 300, 300]) 29 } 30 @State set: string = 'set' 31 build() { 32 Column() { 33 Text(this.set).id('set') 34 List({ space: this.listSpace, initialIndex: 4, scroller: this.scroller }) { 35 ForEach(this.arr, (item: number) => { 36 ListItem() { 37 Text('item-' + item) 38 .height( item < 5 ? 300 : this.listChildrenSize.childDefaultSize) 39 .width('90%') 40 .fontSize(16) 41 .textAlign(TextAlign.Center) 42 .borderRadius(10) 43 .backgroundColor(0xFFFFFF) 44 } 45 }, (item: string) => item) 46 } 47 .backgroundColor(Color.Gray) 48 .layoutWeight(1) 49 .scrollBar(BarState.On) 50 .childrenMainSize(this.listChildrenSize) 51 .alignListItem(ListItemAlign.Center) 52 Row({ space: 10 }){ 53 Button() { Text('item size + 50') }.onClick(()=>{ 54 this.listChildrenSize.childDefaultSize += 50 55 if (this.listChildrenSize.childDefaultSize === 150) { 56 this.set = 'set childDefaultSize(150)' 57 } 58 }).height('50%').width('30%').id('set_childDefaultSize') 59 }.height('20%') 60 } 61 } 62}