diff --git a/entry/src/main/ets/pages/interface.test.ets b/entry/src/main/ets/pages/interface.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..09d710f207453e097af63669bcc528b13f6844c2 --- /dev/null +++ b/entry/src/main/ets/pages/interface.test.ets @@ -0,0 +1,276 @@ +/* + * Copyright (C) 2023 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 router from '@ohos.router'; +import promptAction from '@ohos.promptAction'; +import mediaquery from '@ohos.mediaquery'; +import { PullToRefresh} from '@ohos/pulltorefresh/index' +class BasicDataSource implements IDataSource{ + private listeners: DataChangeListener[] = new Array(); + public totalCount(): number { + return 0; + } + public getData(index: number): Object { + return index; + } + // 为LazyForEach组件向其数据源处添加listener监听 + registerDataChangeListener(listener: DataChangeListener): void { + if (this.listeners.indexOf(listener) < 0) { + console.info('add listener'); + this.listeners.push(listener); + } + } + // 为对应的LazyForEach组件在数据源处去除listener监听 + unregisterDataChangeListener(listener: DataChangeListener): void { + const pos = this.listeners.indexOf(listener); + if (pos >= 0) { + console.info('remove listener'); + this.listeners.splice(pos, 1); + } + } + // 通知LazyForEach组件需要重载所有子组件 + notifyDataReload(): void { + this.listeners.forEach(listener => { + listener.onDataReloaded(); + }) + } + // 通知LazyForEach组件需要在index对应索引处添加子组件 + notifyDataAdd(index: number): void { + this.listeners.forEach(listener => { + listener.onDataAdd(index); + }) + } + // 通知LazyForEach组件需要在index对应索引处添加子组件 + notifyDataChange(index: number): void { + this.listeners.forEach(listener => { + listener.onDataChange(index); + }) + } + // 通知LazyForEach组件需要在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); + }) + } +} + +class MyDataSource extends BasicDataSource { + private dataArray: string[] = []; + public totalCount(): number { + return this.dataArray.length; + } + public getData(index: number): Object { + return this.dataArray[index]; + } + public addData(index: number, data: string): void { + this.dataArray.splice(index, 0, data); + this.notifyDataAdd(index); + } + public pushData(data: string): void { + this.dataArray.push(data); + this.notifyDataAdd(this.dataArray.length - 1); + } + public clear():void{ + this.dataArray = []; + } +} +@Entry +@Component +struct MyComponent { + @State refreshText: string = ''; + @State data: MyDataSource = new MyDataSource(); + // 需绑定列表或宫格组件 + private scroller: Scroller = new Scroller(); + private timer:null|number=null; + aboutToAppear() { + for (let i = 1; i <= 20; i++) { + this.data.pushData(`Hello ${i}`); + } + } + build() { + Column(){ + Column(){ + PullToRefresh({ + // 必传项,列表组件所绑定的数据 + data: $data, + // 必传项,需绑定传入主体布局内的列表或宫格组件 + scroller: this.scroller, + // 必传项,自定义主体布局,内部有列表或宫格组件 + customList:() => { + // 一个用@Builder修饰过的UI方法 + this.getListView(); + }, + // 可选项,下拉刷新回调 + onRefresh: () => { + return new Promise((resolve, reject) => { + // 模拟网络请求操作,请求网络2秒后得到数据,通知组件,变更列表数据 + this.timer = setTimeout(() => { + resolve('刷新成功'); + console.log(' 刷新成功'); + this.data.addData(0,'ADD HEAD 0'); + }, 2000); + }); + }, + // 可选项,上拉加载更多回调 + onLoadMore: () => { + return new Promise((resolve, reject) => { + // 模拟网络请求操作,请求网络2秒后得到数据,通知组件,变更列表数据 + this.timer=setTimeout(() => { + resolve(''); + console.log('上拉加载完成'); + this.data.pushData(`Hello ${this.data.totalCount()}`); + }, 2000); + }); + }, + customLoad: null, + customRefresh: null, + }) + + } + Column(){ + //添加this.data.totalCount接口的性能测试按钮 + Button('this.data.totalCount') + .width('90%') + .margin({top:20}) + .onClick(()=>{ + try { + const BASE_COUNT = 2000 // 循环次数:测试普通接口性能 + let startTime = new Date().getTime() + for(let index = 0;index{ + try { + const BASE_COUNT = 2000 // 循环次数:测试普通接口性能 + let startTime = new Date().getTime() + for(let index = 0;index{ + try { + const BASE_COUNT = 2000 // 循环次数:测试普通接口性能 + let startTime = new Date().getTime() + for(let index = 0;index{ + try { + const BASE_COUNT = 2000 // 循环次数:测试普通接口性能 + let startTime = new Date().getTime() + for(let index = 0;index { + ListItem() { + Row() { + Text(item).fontSize(50) + //组件挂载显示触发 + .onAppear(() => { + if(index){ + console.log(" onAppear: index="+index + ' content= ' +this.data.getData(index) ); + } + }) + //组件卸载载显示触发 + .onDisAppear(()=>{ + if(index) { + console.log(" onDisAppear: index=" + index + ' content= ' + this.data.getData(index)); + } + }) + }.margin({ left: 10, right: 10 }) + } + }, (item: string) => item); + }.cachedCount(5) + .backgroundColor('#eeeeee') + .divider({ strokeWidth: 1, color: 0x222222 }) + .edgeEffect(EdgeEffect.None) // 必须设置列表为滑动到边缘无效果 + } + aboutToDisappear() { + clearTimeout(this.timer); + this.data.clear(); + } + + +} \ No newline at end of file diff --git a/entry/src/main/resources/base/profile/main_pages.json b/entry/src/main/resources/base/profile/main_pages.json index 8aaf28b1109834cda041f8cc76e9674110fe3bd1..3c5b296f7bd23363f2b31b51af53d8f43b3cf92c 100644 --- a/entry/src/main/resources/base/profile/main_pages.json +++ b/entry/src/main/resources/base/profile/main_pages.json @@ -6,6 +6,7 @@ "pages/customRefreshAnim", "pages/tabsTestPage", "pages/lazyForEachGuide", - "pages/fullScreen" + "pages/fullScreen", + "pages/interface.test" ] }