1/* 2 * Copyright (c) 2022 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 17@Entry 18@Component 19struct MyComponent { 20 @State arr: number[] = [10, 20, 30] 21 22 build() { 23 Column({ space: 5 }) { 24 Button('Reverse Array') 25 .onClick(() => { 26 this.arr.reverse() 27 }) 28 29 ForEach(this.arr, (item: number) => { 30 Text(`item value: ${item}`).fontSize(18) 31 Divider().strokeWidth(2) 32 }, (item: number) => item.toString()) 33 } 34 } 35} 36 37 38interface DataChangeListener { 39 onDataReloaded(): void; 40 onDataAdd(index: number): void; 41 onDataMove(from: number, to: number): void; 42 onDataDelete(index: number): void; 43 onDataChange(index: number): void; 44} 45 46 47class BasicDataSource02 implements IDataSource { 48 private listeners: DataChangeListener[] = [] 49 public getData(index: number): any { 50 return undefined; 51 } 52 public totalCount(): number { 53 return 0; 54 } 55 56 registerDataChangeListener(listener: DataChangeListener): void { 57 if (this.listeners.indexOf(listener) < 0) { 58 this.listeners.push(listener) 59 } 60 } 61 62 unregisterDataChangeListener(listener: DataChangeListener): void { 63 const pos = this.listeners.indexOf(listener); 64 if (pos >= 0) { 65 this.listeners.splice(pos, 1) 66 } 67 } 68 69 informDataReload(): void { 70 this.listeners.forEach(listener => { 71 listener.onDataReloaded() 72 }) 73 } 74 75 informDataAdd(index: number): void { 76 this.listeners.forEach(listener => { 77 listener.onDataAdd(index) 78 }) 79 } 80 81 informDataChange(index: number): void { 82 this.listeners.forEach(listener => { 83 listener.onDataChange(index) 84 }) 85 } 86 87 informDataDelete(index: number): void { 88 this.listeners.forEach(listener => { 89 listener.onDataDelete(index) 90 }) 91 } 92 93 informDataMove02(from: number, to: number): void { 94 this.listeners.forEach(listener => { 95 listener.onDataMove(from, to) 96 }) 97 } 98} 99 100class MyDataSource02 extends BasicDataSource02 { 101 private dataArray: string[] = ['/path/image0.png', '/path/image1.png', '/path/image2.png', '/path/image3.png'] 102 103 public totalCount02(): number { 104 return this.dataArray.length 105 } 106 107 public getData(index: number): any { 108 return this.dataArray[index] 109 } 110 111 public addData(index: number, data: string): void { 112 this.dataArray.splice(index, 0, data) 113 this.informDataAdd(index) 114 } 115 116 public pushData(data: string): void { 117 this.dataArray.push(data) 118 this.informDataAdd(this.dataArray.length - 1) 119 } 120} 121 122@Entry 123@Component 124struct MyComponent02 { 125 private data: MyDataSource02 = new MyDataSource02() 126 127 build() { 128 List({ space: 3 }) { 129 LazyForEach(this.data, (item: string) => { 130 ListItem() { 131 Row() { 132 Image(item).width(50).height(50) 133 }.margin({ left: 10, right: 10 }) 134 } 135 .onClick(() => { 136 this.data.pushData('/path/image' + this.data.totalCount02() + '.png') 137 }) 138 }, item => item) 139 } 140 } 141}