From 0f72eafff0edd8911ac26382b9e79e2d904e9894 Mon Sep 17 00:00:00 2001 From: ywcoder <1104410818@qq.com> Date: Mon, 1 Sep 2025 21:10:29 +0800 Subject: [PATCH 1/2] =?UTF-8?q?1=E3=80=81=E4=BF=AE=E6=94=B9targetSdkVersio?= =?UTF-8?q?n?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SimpleChatList/README.md | 2 +- SimpleChatList/build-profile.json5 | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/SimpleChatList/README.md b/SimpleChatList/README.md index e9712c5c..6cb24310 100644 --- a/SimpleChatList/README.md +++ b/SimpleChatList/README.md @@ -1,4 +1,4 @@ -# 实现相机分段式拍照功能 +# 实现常见列表操作功能 ### 介绍 diff --git a/SimpleChatList/build-profile.json5 b/SimpleChatList/build-profile.json5 index d3fe3beb..9e87e7e6 100644 --- a/SimpleChatList/build-profile.json5 +++ b/SimpleChatList/build-profile.json5 @@ -5,8 +5,8 @@ { "name": "default", "signingConfig": "default", - "targetSdkVersion": "5.1.1(19)", - "compatibleSdkVersion": "5.1.1(19)", + "targetSdkVersion": "5.0.5(17)", + "compatibleSdkVersion": "5.0.5(17)", "runtimeOS": "HarmonyOS", "buildOption": { "strictMode": { -- Gitee From 112d3b9ab6b4d6503e4cb59d08df743208f94fb0 Mon Sep 17 00:00:00 2001 From: ywcoder <1104410818@qq.com> Date: Mon, 1 Sep 2025 21:11:41 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E3=80=8A=E7=BB=84=E4=BB=B6=E5=A4=8D?= =?UTF-8?q?=E7=94=A8=E5=B8=B8=E8=A7=81=E9=97=AE=E9=A2=98=E5=AE=9A=E4=BD=8D?= =?UTF-8?q?=E3=80=8B=E5=90=8C=E6=BA=90=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/ets/common/CommonConstants.ets | 96 ++++++++++++++ .../main/ets/pages/ImproveReuseHitRate.ets | 124 ++++++++++++++++++ .../src/main/ets/pages/UseComponentReuse.ets | 34 +++++ 3 files changed, 254 insertions(+) create mode 100644 ComponentReuse/entry/src/main/ets/common/CommonConstants.ets create mode 100644 ComponentReuse/entry/src/main/ets/pages/ImproveReuseHitRate.ets create mode 100644 ComponentReuse/entry/src/main/ets/pages/UseComponentReuse.ets diff --git a/ComponentReuse/entry/src/main/ets/common/CommonConstants.ets b/ComponentReuse/entry/src/main/ets/common/CommonConstants.ets new file mode 100644 index 00000000..49116743 --- /dev/null +++ b/ComponentReuse/entry/src/main/ets/common/CommonConstants.ets @@ -0,0 +1,96 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import { cloudStorage } from '@kit.CloudFoundationKit'; + +class BasicDataSource implements IDataSource { + private listeners: DataChangeListener[] = []; + private originDataArray: string[] = []; + + public totalCount(): number { + return this.originDataArray.length; + } + + public getData(index: number): string { + return this.originDataArray[index]; + } + + registerDataChangeListener(listener: DataChangeListener): void { + if (this.listeners.indexOf(listener) < 0) { + console.info('add listener'); + this.listeners.push(listener); + } + } + + unregisterDataChangeListener(listener: DataChangeListener): void { + const pos = this.listeners.indexOf(listener); + if (pos >= 0) { + console.info('remove listener'); + this.listeners.splice(pos, 1); + } + } + + notifyDataReload(): void { + this.listeners.forEach(listener => { + listener.onDataReloaded(); + }) + } + + notifyDataAdd(index: number): void { + this.listeners.forEach(listener => { + listener.onDataAdd(index); + }) + } + + notifyDataChange(index: number): void { + this.listeners.forEach(listener => { + listener.onDataChange(index); + }) + } + + notifyDataDelete(index: number): void { + this.listeners.forEach(listener => { + listener.onDataDelete(index); + }) + } + + notifyDataMove(from: number, to: number): void { + this.listeners.forEach(listener => { + listener.onDataMove(from, to); + }) + } + + notifyDatasetChange(operations: DataOperation[]): void { + this.listeners.forEach(listener => { + listener.onDatasetChange(operations); + }) + } +} + +export class MyDataSource extends BasicDataSource { + private dataArray: string[] = []; + + public totalCount(): number { + return this.dataArray.length; + } + + public getData(index: number): string { + return this.dataArray[index]; + } + + public pushData(data: string): void { + this.dataArray.push(data); + this.notifyDataAdd(this.dataArray.length - 1); + } +} \ No newline at end of file diff --git a/ComponentReuse/entry/src/main/ets/pages/ImproveReuseHitRate.ets b/ComponentReuse/entry/src/main/ets/pages/ImproveReuseHitRate.ets new file mode 100644 index 00000000..86b8f5ba --- /dev/null +++ b/ComponentReuse/entry/src/main/ets/pages/ImproveReuseHitRate.ets @@ -0,0 +1,124 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { MyDataSource } from '../common/CommonConstants'; + +@Observed +class Class1 { + public typeValue: string = ''; + public id: string = ''; +} + +@Entry +@Component +struct ImproveReuseHitRate { + private data: MyDataSource = new MyDataSource(); + + aboutToAppear(): void { + // Init Data. + } + + build() { + List({ space: 3 }) { + LazyForEach(this.data, (item: Class1) => { + ReusableComponent({ value1: item }).reuseId(item.typeValue) + }, (item: Class1) => item.id) + }.cachedCount(5) + } +} + +@Reusable +@Component +struct ReusableComponent { + @ObjectLink value1: Class1; + + aboutToAppear(): void { + // Do some init. + } + + build() { + ListItem() { + ContentBuilder({ value1: this.value1 }) + } + } +} + +interface ContentParamType { + value1: Class1 +} + +@Builder +function ContentBuilder($$: ContentParamType) { + if ($$.value1.typeValue === 'A') { + // BuildTypeA + } else if ($$.value1.typeValue === 'C') { + // BuildTypeC + } else if ($$.value1.typeValue === 'D') { + // BuildTypeD + } else if ($$.value1.typeValue === 'E') { + // BuildTypeE + } else if ($$.value1.typeValue === 'F') { + // BuildTypeF + } else if ($$.value1.typeValue === 'G') { + // BuildTypeG + } else if ($$.value1.typeValue === 'H') { + // BuildTypeH + } else if ($$.value1.typeValue === 'I') { + // BuildTypeI + } else if ($$.value1.typeValue === 'J') { + // BuildTypeJ + } else if ($$.value1.typeValue === 'K') { + // BuildTypeK + } +} + +@Builder +function TypeA($$: ContentParamType) { + + Column() { + Title() + .visibility(Visibility.Visible) // Control the display or concealment of components. + Row() { + Column() { + Image($r('app.media.startIcon')) + Text() + Text() + Text() + } + } + } +} + +@Builder +function TypeC($$: ContentParamType) { + Column() { + Title() + Grid() { + // ... + } + } +} + + +@Component +struct Title { + build() { + Column() { + Text('title') + } + } +} + + diff --git a/ComponentReuse/entry/src/main/ets/pages/UseComponentReuse.ets b/ComponentReuse/entry/src/main/ets/pages/UseComponentReuse.ets new file mode 100644 index 00000000..bde0b7b5 --- /dev/null +++ b/ComponentReuse/entry/src/main/ets/pages/UseComponentReuse.ets @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// Add Reusable decorator. +@Reusable +@Component +struct ReusableComponent { + @State item: string = ''; + + aboutToReuse(params: ESObject): void { + this.item = params.item; // Trigger the aboutToReuse lifecycle callback. + } + + build() { + ListItem() { + Row() { + Text(this.item).fontSize(50) + } + .margin({ left: 10, right: 10 }) + } + } +} \ No newline at end of file -- Gitee