From 144f0142662de6d5dcb3f7573525b8b6fc55006e Mon Sep 17 00:00:00 2001 From: dhf_fanr <1511447814@qq.com> Date: Tue, 16 Sep 2025 13:54:41 +0800 Subject: [PATCH] =?UTF-8?q?IDE=E5=91=8A=E8=AD=A6=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- entry/src/main/ets/pages/DetailListPage.ets | 40 +++++++++++++++------ entry/src/main/ets/pages/DetailPage.ets | 23 ++++++++---- 2 files changed, 45 insertions(+), 18 deletions(-) diff --git a/entry/src/main/ets/pages/DetailListPage.ets b/entry/src/main/ets/pages/DetailListPage.ets index 967fad2..612b5b0 100644 --- a/entry/src/main/ets/pages/DetailListPage.ets +++ b/entry/src/main/ets/pages/DetailListPage.ets @@ -13,9 +13,12 @@ * limitations under the License. */ -import { router,display } from '@kit.ArkUI'; +import { display } from '@kit.ArkUI'; +import { BusinessError } from '@kit.BasicServicesKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; import Constants from '../common/constants/Constants'; + enum scrollTypeEnum { STOP = 'onScrollStop', SCROLL = 'onScroll' @@ -30,25 +33,37 @@ struct DetailListPage { @State smallImgWidth: number = (this.deviceWidth - Constants.LIST_ITEM_SPACE * (Constants.SHOW_COUNT - 1)) / Constants.SHOW_COUNT; @State imageWidth: number = this.deviceWidth + this.smallImgWidth; - private photoArr: Array = (this.getUIContext().getRouter().getParams() as Record>)[`${Constants.PARAM_PHOTO_ARR_KEY}`]; + private photoArr: Array = (this.getUIContext() + .getRouter() + .getParams() as Record>)[`${Constants.PARAM_PHOTO_ARR_KEY}`]; private smallPhotoArr: Array = new Array().concat(Constants.CACHE_IMG_LIST, - (this.getUIContext().getRouter().getParams() as Record>)[`${Constants.PARAM_PHOTO_ARR_KEY}`], + (this.getUIContext() + .getRouter() + .getParams() as Record>)[`${Constants.PARAM_PHOTO_ARR_KEY}`], Constants.CACHE_IMG_LIST) @StorageLink('selectedIndex') selectedIndex: number = 0; - @Builder SmallImgItemBuilder(img: Resource, index?: number) { - if (index && index > (Constants.CACHE_IMG_SIZE - 1) && index < (this.smallPhotoArr.length - Constants.CACHE_IMG_SIZE)) { + @Builder + SmallImgItemBuilder(img: Resource, index?: number) { + if (index && index > (Constants.CACHE_IMG_SIZE - 1) && + index < (this.smallPhotoArr.length - Constants.CACHE_IMG_SIZE)) { Image(img) .onClick(() => this.smallImgClickAction(index)) } } aboutToAppear() { - let displayClass: display.Display = display.getDefaultDisplaySync(); - let width = displayClass?.width / displayClass.densityPixels ?? Constants.DEFAULT_WIDTH; - this.deviceWidth = width; - this.smallImgWidth = (width - Constants.LIST_ITEM_SPACE * (Constants.SHOW_COUNT - 1)) / Constants.SHOW_COUNT; - this.imageWidth = this.deviceWidth + this.smallImgWidth; + try { + let displayClass: display.Display = display.getDefaultDisplaySync(); + let width = displayClass?.width / displayClass.densityPixels ?? Constants.DEFAULT_WIDTH; + this.deviceWidth = width; + this.smallImgWidth = (width - Constants.LIST_ITEM_SPACE * (Constants.SHOW_COUNT - 1)) / Constants.SHOW_COUNT; + this.imageWidth = this.deviceWidth + this.smallImgWidth; + } catch (error) { + let err = error as BusinessError; + hilog.error(0x00, 'DetailListPage', `getDefaultDisplaySync failed, code = ${err.code}, message = ${err.message}`); + } + } onPageShow() { @@ -60,6 +75,9 @@ struct DetailListPage { this.getUIContext().getRouter().pushUrl({ url: Constants.URL_DETAIL_PAGE, params: { photoArr: this.photoArr } + }).catch((error: BusinessError) => { + hilog.error(0x00, 'DetailListPage', + `pushUrl failed, code = ${error.code}, message = ${error.message}`); }); } @@ -71,7 +89,7 @@ struct DetailListPage { smallScrollAction(type: scrollTypeEnum): void { this.selectedIndex = Math.round(((this.smallScroller.currentOffset().xOffset as number) + - this.smallImgWidth / Constants.DOUBLE_NUMBER) / (this.smallImgWidth + Constants.LIST_ITEM_SPACE)); + this.smallImgWidth / Constants.DOUBLE_NUMBER) / (this.smallImgWidth + Constants.LIST_ITEM_SPACE)); if (type === scrollTypeEnum.SCROLL) { this.bigScroller.scrollTo({ xOffset: this.selectedIndex * this.imageWidth, yOffset: 0 }); } else { diff --git a/entry/src/main/ets/pages/DetailPage.ets b/entry/src/main/ets/pages/DetailPage.ets index ed4af19..f8193f5 100644 --- a/entry/src/main/ets/pages/DetailPage.ets +++ b/entry/src/main/ets/pages/DetailPage.ets @@ -13,14 +13,18 @@ * limitations under the License. */ -import { router, display } from '@kit.ArkUI'; +import { display } from '@kit.ArkUI'; +import { BusinessError } from '@kit.BasicServicesKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; import Constants from '../common/constants/Constants'; @Entry @Component struct DetailPage { scroller: Scroller = new Scroller(); - photoArr: Array = (this.getUIContext().getRouter().getParams() as Record>)[`${Constants.PARAM_PHOTO_ARR_KEY}`]; + photoArr: Array = (this.getUIContext() + .getRouter() + .getParams() as Record>)[`${Constants.PARAM_PHOTO_ARR_KEY}`]; preOffsetX: number = 0; preOffsetY: number = 0; currentScale: number = 1; @@ -36,11 +40,16 @@ struct DetailPage { @State bgOpacity: number = 0; aboutToAppear() { - let displayClass: display.Display = display.getDefaultDisplaySync(); - let width = displayClass?.width / displayClass.densityPixels ?? Constants.DEFAULT_WIDTH; - this.deviceWidth = width; - this.smallImgWidth = (width - Constants.LIST_ITEM_SPACE * (Constants.SHOW_COUNT - 1)) / Constants.SHOW_COUNT; - this.imageWidth = this.deviceWidth + this.smallImgWidth; + try { + let displayClass: display.Display = display.getDefaultDisplaySync(); + let width = displayClass?.width / displayClass.densityPixels ?? Constants.DEFAULT_WIDTH; + this.deviceWidth = width; + this.smallImgWidth = (width - Constants.LIST_ITEM_SPACE * (Constants.SHOW_COUNT - 1)) / Constants.SHOW_COUNT; + this.imageWidth = this.deviceWidth + this.smallImgWidth; + } catch (error) { + let err = error as BusinessError; + hilog.error(0x00, 'DetailPage', `getDefaultDisplaySync failed, code = ${err.code}, message = ${err.message}`); + } } resetImg(): void { -- Gitee