本示例使用Scroll和List组件嵌套,通过List组件的滚动控制器和nestedScroll属性实现了视频卡片和列表区域的联动滚动场景。
使用说明
// 当前播放的新闻
@State currentPlayNews: NewsItem = new NewsItem('', '');
// 当前播放的新闻在列表中的下标
@State currentIndex: number = 0;
aboutToAppear() {
// 新闻列表数据初始化
NEWS_LIST_DATA.forEach((news: NewsItem) => {
this.newsList.pushData(news);
})
this.currentPlayNews = this.newsList.getData(this.currentIndex);
...
}
List({ scroller: this.scroller }) {
...
}
.nestedScroll({
scrollForward: NestedScrollMode.PARENT_FIRST, // 可滚动组件往末尾端滚动时的嵌套滚动选项,父组件先滚动,父组件滚动到边缘以后自身滚动。
scrollBackward: NestedScrollMode.SELF_FIRST // 可滚动组件往起始端滚动时的嵌套滚动选项,自身先滚动,自身滚动到边缘以后父组件滚动。
})
Scroll(this.scroller) {
Column() {
NewsVideoView({
currentPlayNews: this.currentPlayNews,
currentIndex: this.currentIndex,
newsList: this.newsList,
isHideVideo: this.isHideVideo,
scrollHeight: this.scrollHeight,
newsListHeight: this.newsListHeight,
})
.margin({ top: this.videoMarginTop, bottom: $r('app.integer.video_linkage_list_video_card_margin_bottom') })
NewsListView({
currentPlayNews: this.currentPlayNews,
currentIndex: this.currentIndex,
newsList: this.newsList,
isHideVideo: this.isHideVideo,
newsListHeight: this.newsListHeight
})
}
.height(`calc(${Constants.VIDEO_HEIGHT}vp + 100%)`)
}
List({ scroller: this.scroller }) {
...
}
.layoutWeight(Constants.LAYOUT_WEIGHT)
欢迎大家关注公众号<程序猿百晓生>,可以了解到一下知识点。
1.OpenHarmony开发基础
2.OpenHarmony北向开发环境搭建
3.鸿蒙南向开发环境的搭建
4.鸿蒙生态应用开发白皮书V2.0 & V3.0
5.鸿蒙开发面试真题(含参考答案)
6.TypeScript入门学习手册
7.OpenHarmony 经典面试题(含参考答案)
8.OpenHarmony设备开发入门【最新版】
9.沉浸式剖析OpenHarmony源代码
10.系统定制指南
11.【OpenHarmony】Uboot 驱动加载流程
12.OpenHarmony构建系统--GN与子系统、部件、模块详解
13.ohos开机init启动流程
14.鸿蒙版性能优化指南
.......
// 是否隐藏视频区域
@State @Watch('onIsHideVideoChange') isHideVideo: boolean = false;
Scroll(this.scroller) {
...
}
// TODO: 性能知识点:onScroll属于频繁回调接口,应该避免在内部进行冗余和耗时操作,例如避免打印日志
.onScroll((xOffset: number, yOffset: number) => {
// 视频已隐藏情况下, Scroll向下滚动时显示视频
if (yOffset < 0 && this.isHideVideo) {
this.isHideVideo = false;
}
})
.onReachEnd(() => {
// Scroll滚动到末尾时隐藏视频
this.isHideVideo = true;
})
Stack({ alignContent: Alignment.Bottom }) {
...
}
.width($r('app.string.video_linkage_list_full_size'))
// 修改视频的高度实现显隐控制
.height(this.isHideVideo ? 0 : Constants.VIDEO_HEIGHT)
// TODO:知识点:根据视频显隐状态修改边距,使用边距代替video占位,使Scroll容器内容高度不变,可以向下滚动显示视频,并且避免滚动混乱
onIsHideVideoChange() {
if (!this.isHideVideo) {
// 视频显示,视频卡片上边距减去视频高度
this.videoMarginTop -= Constants.VIDEO_HEIGHT;
} else {
// 视频隐藏,视频卡片上边距加上视频高度
this.videoMarginTop += Constants.VIDEO_HEIGHT;
}
}
// 上一条
Image($r('app.media.video_linkage_list_play_previous'))
.height($r('app.integer.video_linkage_list_control_previous_next_height'))
.onClick(() => {
// 如果不是第一条,切换至上一条
if (this.currentIndex > 0) {
this.currentIndex--;
this.currentPlayNews = this.newsList.getData(this.currentIndex);
} else {
promptAction.showToast({
message: $r('app.string.video_linkage_list_first_data_toast')
});
}
})
...
// 下一条
Image($r('app.media.video_linkage_list_play_next'))
.height($r('app.integer.video_linkage_list_control_previous_next_height'))
.onClick(() => {
// 如果不是最后一条,切换至下一条
if (this.currentIndex < this.newsList.totalCount() - 1) {
this.currentIndex++;
this.currentPlayNews = this.newsList.getData(this.currentIndex);
} else {
promptAction.showToast({
message: $r('app.string.video_linkage_list_last_data_toast')
});
}
})
// TODO:知识点:监听currentIndex的变化,视频播放卡片切换新闻和点击列表项切换新闻时修改currentIndex,根据下标计算列表的滚动偏移
onCurrentIndexChange() {
// 选中的列表项下标大于3时,列表向上滚动,滚动到与列表显示区域内上方间隔3个列表项或列表尾部时停止。
if (this.currentIndex > Constants.NEWS_LIST_SCROLL_TO_INDEX) {
this.scroller.scrollTo({
yOffset: Constants.NEWS_LIST_ITEM_HEIGHT * (this.currentIndex - Constants.NEWS_LIST_SCROLL_TO_INDEX),
xOffset: 0
});
} else {
// 选中的列表项下标小于等于3时,列表滚动至头部
this.scroller.scrollTo({ yOffset: 0, xOffset: 0 });
}
}
videolinkagelist // har类型
|---/src/main/ets/model
| |---NewsListDataSource.ets // 数据模型层-列表数据模型
| |---NewsItemModel.ets // 数据模型层-列表项数据模型
|---/src/main/ets/pages
| |---VideoLinkageList.ets // 视图层-主页面
|---/src/main/ets/mock
| |---NewsListData.ets // 新闻列表mock数据
|---/src/main/ets/common
| |---Constants.ets // 常量数据
如果你觉得这篇内容对你还蛮有帮助,我想邀请你帮我三个小忙:
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。