Showing
2 changed files
with
61 additions
and
165 deletions
| @@ -20,7 +20,7 @@ export struct CarouselLayout01CardView { | @@ -20,7 +20,7 @@ export struct CarouselLayout01CardView { | ||
| 20 | 20 | ||
| 21 | build() { | 21 | build() { |
| 22 | RelativeContainer() { | 22 | RelativeContainer() { |
| 23 | - Image(this.item.hImageUrl) | 23 | + Image(this.item.coverUrl) |
| 24 | .width(CommonConstants.FULL_PARENT) | 24 | .width(CommonConstants.FULL_PARENT) |
| 25 | .height(CommonConstants.FULL_PARENT) | 25 | .height(CommonConstants.FULL_PARENT) |
| 26 | .objectFit(ImageFit.Cover) | 26 | .objectFit(ImageFit.Cover) |
| 1 | -/** | ||
| 2 | - * BannerComponent | ||
| 3 | - * 轮播图卡/单图 | ||
| 4 | - * 邢照杰 | ||
| 5 | - */ | ||
| 6 | - | ||
| 7 | -import { CommonConstants } from 'wdConstant'; | ||
| 8 | -import colorSpaceManager from '@ohos.graphics.colorSpaceManager'; | ||
| 9 | -import { CompUtils } from '../../utils/CompUtils'; | ||
| 10 | -import { Action, CompDTO, ContentDTO, Params } from 'wdBean'; | ||
| 11 | -import { Logger } from 'wdKit'; | ||
| 12 | -import { WDRouterRule } from 'wdRouter'; | ||
| 13 | - | 1 | +import { CompDTO, ContentDTO, } from 'wdBean'; |
| 2 | +import { BreakpointConstants,DurationEnum } from 'wdConstant'; | ||
| 3 | +import { BreakPointType, Logger } from 'wdKit'; | ||
| 4 | +import { CarouselLayout01CardView } from '../page/CardView'; | ||
| 5 | +import { EmptyComponent } from './EmptyComponent'; | ||
| 14 | 6 | ||
| 15 | const TAG = 'BannerComponent'; | 7 | const TAG = 'BannerComponent'; |
| 16 | -let timerIds: number[] = []; | ||
| 17 | - | ||
| 18 | 8 | ||
| 19 | /** | 9 | /** |
| 20 | - * 轮播卡(暂时仅展示主标题,不展示子标题) | ||
| 21 | - * comp类型 | ||
| 22 | - * 重磅推荐/精选/电视剧/电影/综艺/短剧/更多>/ | 10 | + * 轮播组件,即Banner/轮播大图/焦点图/自动滑动 |
| 11 | + * 样式: | ||
| 12 | + * 'Carousel_Layout-01', // 通用轮播卡:视频、直播、活动、专题、榜单、外链 | ||
| 23 | */ | 13 | */ |
| 24 | -@Entry | ||
| 25 | @Component | 14 | @Component |
| 26 | export struct BannerComponent { | 15 | export struct BannerComponent { |
| 16 | + @StorageLink('currentBreakpoint') @Watch('watchCurrentBreakpoint') currentBreakpoint: string = BreakpointConstants.BREAKPOINT_XS; | ||
| 27 | @State compDTO: CompDTO = {} as CompDTO | 17 | @State compDTO: CompDTO = {} as CompDTO |
| 28 | - @State index: number = 0; | ||
| 29 | - private bannerContent: ContentDTO = {} as ContentDTO; | ||
| 30 | - private swiperController: SwiperController = new SwiperController(); | ||
| 31 | - | ||
| 32 | - aboutToAppear() { | ||
| 33 | 18 | ||
| 34 | - // Data Initialization. | ||
| 35 | - this.bannerContent = this.compDTO.operDataList[0]; | ||
| 36 | - // Turn on scheduled task. | ||
| 37 | - if (this.compDTO.operDataList.length > 1) { | ||
| 38 | - startPlay(this.swiperController); | 19 | + watchCurrentBreakpoint() { |
| 20 | + Logger.info(TAG, `watchCurrentBreakpoint, this.currentBreakpoint: ${this.currentBreakpoint}`); | ||
| 39 | } | 21 | } |
| 22 | + | ||
| 23 | + aboutToAppear() { | ||
| 24 | + Logger.info(TAG, `aboutToAppear, beanList:${this.compDTO?.operDataList?.length}, currentBreakpoint:${this.currentBreakpoint}`); | ||
| 40 | } | 25 | } |
| 41 | 26 | ||
| 42 | aboutToDisappear() { | 27 | aboutToDisappear() { |
| 43 | - stopPlay(); | 28 | + Logger.info(TAG, 'aboutToDisappear'); |
| 44 | } | 29 | } |
| 45 | 30 | ||
| 46 | - build() { | ||
| 47 | - // 整体父视图 | ||
| 48 | - Column() { | ||
| 49 | - // 判断数组元素个数 | ||
| 50 | - if (this.compDTO.operDataList.length > 1) { | ||
| 51 | - // 滚动banner | ||
| 52 | - Swiper(this.swiperController) { | ||
| 53 | - ForEach(this.compDTO.operDataList, (item: ContentDTO, index: number) => { | ||
| 54 | - Stack() { | ||
| 55 | - // 背景图 | ||
| 56 | - Image(item.coverUrl) | ||
| 57 | - .objectFit(ImageFit.Fill) | ||
| 58 | - .borderRadius(5) | ||
| 59 | - | ||
| 60 | - // 底部标题和时间 | ||
| 61 | - Column() { | ||
| 62 | - Text(item.description) | ||
| 63 | - .fontSize(18) | ||
| 64 | - .margin({ bottom: 4 }) | ||
| 65 | - .fontColor(Color.White) | ||
| 66 | - .fontWeight(600) | ||
| 67 | - .maxLines(1) | ||
| 68 | - .textOverflow({ overflow: TextOverflow.Ellipsis }) | ||
| 69 | - .padding({ left: 10, right: 5 }) | ||
| 70 | - .width('100%') | ||
| 71 | - .textAlign(TextAlign.Start) | ||
| 72 | - if (item.lengthTime) { | ||
| 73 | - Row() { | ||
| 74 | - Image($r('app.media.videoTypeIcon')) | ||
| 75 | - .height(20) | ||
| 76 | - .width(20) | ||
| 77 | - // .margin({right:3}) | ||
| 78 | - Text(item.lengthTime + '') | ||
| 79 | - .padding({ left: 5, right: 5 }) | ||
| 80 | - .fontColor(Color.White) | ||
| 81 | - } | ||
| 82 | - .backgroundColor('#333333') | ||
| 83 | - .height(20) | ||
| 84 | - .margin({ right: 5, bottom: 3 }) | ||
| 85 | - .alignSelf(ItemAlign.End) | ||
| 86 | - .borderRadius(2) | 31 | + onPageShow() { |
| 32 | + Logger.info(TAG, 'onPageShow'); | ||
| 87 | } | 33 | } |
| 34 | + | ||
| 35 | + onPageHide() { | ||
| 36 | + Logger.info(TAG, 'onPageHide'); | ||
| 88 | } | 37 | } |
| 89 | - .height('50') | ||
| 90 | - .width('100%') | 38 | + |
| 39 | + onBackPress() { | ||
| 40 | + Logger.info(TAG, 'onBackPress'); | ||
| 91 | } | 41 | } |
| 92 | - .alignContent(Alignment.BottomStart) | ||
| 93 | - .onClick((event: ClickEvent) => { | ||
| 94 | - Logger.info(TAG, `BannerComponent onClick event index: ${this.index}`); | ||
| 95 | - // let taskAction: Action = { | ||
| 96 | - // type: 'JUMP_H5_BY_WEB_VIEW', | ||
| 97 | - // params: { | ||
| 98 | - // url: ConfigConstants.DETAIL_URL | ||
| 99 | - // } as Params, | ||
| 100 | - // }; | ||
| 101 | - // WDRouterRule.jumpWithAction(taskAction) | ||
| 102 | 42 | ||
| 103 | - let taskAction: Action = { | ||
| 104 | - type: 'JUMP_DETAIL_PAGE', | ||
| 105 | - params: { | ||
| 106 | - detailPageType: 7, // 沉浸式竖屏详情页 | ||
| 107 | - contentID: '863556812' | ||
| 108 | - } as Params, | ||
| 109 | - }; | ||
| 110 | - WDRouterRule.jumpWithAction(taskAction) | 43 | + build() { |
| 44 | + if (this.compDTO && this.compDTO?.operDataList?.length > 0) { | ||
| 45 | + Swiper() { | ||
| 46 | + ForEach(this.compDTO?.operDataList, (item: ContentDTO, index: number) => { | ||
| 47 | + this.buildItemBanner01(item, index) | ||
| 111 | }) | 48 | }) |
| 112 | - }, (item: ContentDTO, index: number) => JSON.stringify(item)) | ||
| 113 | } | 49 | } |
| 114 | - .width('100%') | ||
| 115 | - .height('100%') | ||
| 116 | - .index(this.index) | ||
| 117 | - .indicatorStyle({ | ||
| 118 | - selectedColor: Color.White, | ||
| 119 | - color: Color.Gray, | ||
| 120 | - size: 18, | ||
| 121 | - left: 15 | 50 | + .margin({ left: $r('app.float.main_margin'), right: $r('app.float.main_margin') }) |
| 51 | + .padding({ bottom: 14 }) | ||
| 52 | + .displayCount(this.buildDisplayCount()) // 仅展示1个图片 | ||
| 53 | + .cachedCount(2) | ||
| 54 | + .index(1) // The default index of Swiper. | ||
| 55 | + .autoPlay(true) | ||
| 56 | + .interval(DurationEnum.DURATION_4) | ||
| 57 | + .indicator(Indicator.dot() | ||
| 58 | + .right(5) | ||
| 59 | + .itemWidth(4) | ||
| 60 | + .itemHeight(4) | ||
| 61 | + .selectedItemWidth(10) | ||
| 62 | + .selectedItemHeight(6)) | ||
| 63 | + .loop(true) | ||
| 64 | + .duration(DurationEnum.DURATION_4) | ||
| 65 | + .vertical(false) | ||
| 66 | + .curve(Curve.Linear) | ||
| 67 | + .onChange((index: number) => { | ||
| 68 | + Logger.info(TAG, `Swiper onChange index : ${index}`); | ||
| 122 | }) | 69 | }) |
| 123 | - .indicator(true) | ||
| 124 | - .duration(500) | ||
| 125 | } else { | 70 | } else { |
| 126 | - // 不滚动banner | ||
| 127 | - Stack() { | ||
| 128 | - // 背景图 | ||
| 129 | - Image(this.bannerContent.coverUrl.toString()) | ||
| 130 | - .objectFit(ImageFit.Fill) | ||
| 131 | - .borderRadius(5) | ||
| 132 | - | ||
| 133 | - // 底部标题和时间 | ||
| 134 | - Row() { | ||
| 135 | - // 标题 | ||
| 136 | - Text(this.bannerContent.description.toString()) | ||
| 137 | - .fontSize(18) | ||
| 138 | - .fontColor(Color.White) | ||
| 139 | - .fontWeight(600) | ||
| 140 | - .maxLines(2) | ||
| 141 | - .textOverflow({ overflow: TextOverflow.Ellipsis }) | ||
| 142 | - .padding({ left: 10, right: 0, bottom: 5 }) | ||
| 143 | - .width('80%') | ||
| 144 | - // 时间 | ||
| 145 | - if (this.bannerContent.lengthTime) { | ||
| 146 | - Row() { | ||
| 147 | - Image($r('app.media.videoTypeIcon')) | ||
| 148 | - .height(20) | ||
| 149 | - .width(20) | ||
| 150 | - // .margin({right:3}) | ||
| 151 | - Text(this.bannerContent.lengthTime.toString()) | ||
| 152 | - .padding({ left: 5, right: 5 }) | ||
| 153 | - .fontColor(Color.White) | ||
| 154 | - } | ||
| 155 | - .backgroundColor('#333333') | ||
| 156 | - .height(20) | ||
| 157 | - .borderRadius(2) | ||
| 158 | - .margin({ bottom: 6 }) | ||
| 159 | - } | 71 | + EmptyComponent({ emptyHeight: 200 }) |
| 160 | } | 72 | } |
| 161 | - .width('100%') | ||
| 162 | - .height('100%') | ||
| 163 | - .alignItems(VerticalAlign.Bottom) | ||
| 164 | } | 73 | } |
| 165 | - .alignContent(Alignment.BottomStart) | ||
| 166 | - .width('100%') | ||
| 167 | - .height('100%') | ||
| 168 | - } | ||
| 169 | - } | ||
| 170 | - .width('100%') | ||
| 171 | - .aspectRatio(1.7) | ||
| 172 | - .padding({ left: 10, right: 15, top: 10, bottom: 10 }) | 74 | + |
| 75 | + public buildDisplayCount(): number { | ||
| 76 | + return new BreakPointType({ xs: 1, sm: 1, md: 2, lg: 3 }).getValue(this.currentBreakpoint) | ||
| 173 | } | 77 | } |
| 174 | -} | ||
| 175 | 78 | ||
| 176 | -/** | ||
| 177 | - * start scheduled task. | 79 | + /** |
| 80 | + * 组件项 | ||
| 178 | * | 81 | * |
| 179 | - * @param swiperController Controller. | 82 | + * @param programmeBean item 组件项 |
| 180 | */ | 83 | */ |
| 181 | -export function startPlay(swiperController: SwiperController) { | ||
| 182 | - let timerId = setInterval(() => { | ||
| 183 | - swiperController.showNext(); | ||
| 184 | - }, 3000); | ||
| 185 | - timerIds.push(timerId); | ||
| 186 | -} | ||
| 187 | - | ||
| 188 | -/** | ||
| 189 | - * stop scheduled task. | ||
| 190 | - */ | ||
| 191 | -export function stopPlay() { | ||
| 192 | - timerIds.forEach((item) => { | ||
| 193 | - clearTimeout(item); | 84 | + @Builder |
| 85 | + buildItemBanner01(item: ContentDTO, index: number) { | ||
| 86 | + CarouselLayout01CardView({ | ||
| 87 | + item: item, | ||
| 88 | + index: index | ||
| 194 | }) | 89 | }) |
| 90 | + } | ||
| 195 | } | 91 | } |
-
Please register or login to post a comment