1***@qq.com
1***@qq.com
  • 发布:2026-04-09 14:06
  • 更新:2026-04-09 14:07
  • 阅读:39

HBuilder X 5.06版本,使用uni-app x,在list-view 组件里面,video无法自适应,在scroll-view就可以

分类:uni-app x
使用组件list-view,在list-item组件里面使用video,代码如下: <list-view class="scroll-content" :scroll-y="true" :scroll-top="scrollTop" :refresher-enabled="true"

refresher-triggered="isRefreshing" @refresherrefresh="onRefresh" @scrolltolower="onLoadMore"> <list-item v-if="historyList.length == 0 && !isLoading" class="empty-container">
<text class="empty-text">暂无历史记录</text>
</list-item>

    <list-item v-if="isLoading" class="loading-container">  
        <text class="loading-text">加载中...</text>  
    </list-item>  

    <list-item v-for="(item) in historyList" :key="item.time" class="history-item">  
        <text class="title">{{ item.input }}</text>  
        <view class="desc">  
            <text class="item-time">{{ item.occurTime }}</text>  
        </view>  
        <view class="divider"></view>  
        <view class="video-container">  
            <video v-if="item.videoUrl" :src="item.videoUrl" :key="item.time" controls class="generated-video"  
show-center-play-btn="true" :show-fullscreen-btn="true"

show-play-btn="true" :enable-progress-gesture="true" object-fit="contain"></video> <view v-else class="mask" @tap="getVideo(item.taskId)">
<image class="play" src="/static/play.png" mode="scaleToFill"></image>
</view>
</view>
</list-item>

    <list-item v-if="!hasMore && historyList.length > 0" class="no-more-container">  
        <text class="no-more-text">没有更多了</text>  
    </list-item>  
</list-view>  

css样式如下:
.video-container {
position: relative;
width: 100%;
height: 0;
padding-top: 56.25%;
box-sizing: border-box;
overflow: hidden;
background: #000;
z-index: 1;
}

.generated-video {  
    position: absolute;  
    top: 0;  
    left: 0;  
    width: 100%;  
    height: 100%;  
    background: transparent;  
}  

在安卓显示的结果

如果换成scroll-view 组件
<scroll-view class="scroll-content" :scroll-y="true" :scroll-top="scrollTop" :refresher-enabled="true" :refresher-triggered="isRefreshing" @refresherrefresh="onRefresh" @scrolltolower="onLoadMore">
<view v-if="historyList.length == 0 && !isLoading" class="empty-container">
<text class="empty-text">暂无历史记录</text>
</view>

        <view v-if="isLoading" class="loading-container">  
            <text class="loading-text">加载中...</text>  
        </view>  

        <view v-for="(item) in historyList" :key="item.time" class="history-item">  
            <text class="title">{{ item.input }}</text>  
            <view class="desc">  
                <text class="item-time">{{ item.occurTime }}</text>  
            </view>  
            <view class="divider"></view>  
            <view class="video-container">  
                <video v-if="item.videoUrl" :id="'' + item.taskId" :src="item.videoUrl" :key="item.videoUrl + '-' + item.occurTime" controls class="generated-video"  
                    :autoplay="item.shouldAutoplay" :show-center-play-btn="true" :show-fullscreen-btn="true"  
                    :show-play-btn="true" :enable-progress-gesture="true" object-fit="contain" @play="onVideoPlay(item)"></video>  
                <view v-else class="mask" @tap="getVideo(item.taskId)">  
                    <image class="play" src="/static/play.png" mode="scaleToFill"></image>  
                </view>  
            </view>  
        </view>  

        <view v-if="!hasMore && historyList.length > 0" class="no-more-container">  
            <text class="no-more-text">没有更多了</text>  
        </view>  
    </scroll-view>  

css样式一样的,显示结果为


list-view里面为什么对video的支持这么差,是什么原因造成的,有办法解决吗

2026-04-09 14:06 负责人:无 分享
已邀请:
Ask小助手

Ask小助手 - Ask社区AI助手

在 uni-app x 中,list-view 组件采用了原生列表的复用机制(类似 Android 的 RecyclerView),这会导致内部的 video 组件被复用(recycle/reuse)。这种复用机制会影响布局计算,特别是基于百分比(如 padding-top: 56.25%)的自适应布局方案在复用过程中可能无法正确计算高度。

根据 video 组件在 list-view 中复用的文档list-view 中的 video 组件会被复用,需要使用 @recycle@reuse 事件来管理视频状态。

解决方案:

  1. 避免使用百分比 padding 技巧:在 list-view 中,建议给 video-containerlist-item 设置固定高度,而不是使用 height: 0 + padding-top: 56.25% 的 CSS 技巧。

  2. 设置固定宽高比:尝试直接使用 aspect-ratio CSS 属性(如果目标平台支持),或计算固定高度:

.video-container {  
    width: 100%;  
    height: 200px; /* 或使用 calc 计算固定值 */  
    /* 移除 padding-top: 56.25% */  
}

本助手使用国产模型,生成代码可能不准确。专业的代码生成需要全球顶尖的AI Coding模型。推荐使用uni-agent

  1. 处理复用事件:如果需要在 list-view 中使用 video,建议参考官方示例 list-view-multiplex-video,使用 @recycle@reuse 事件来管理视频组件:
<video   
  class="video"   
  :src="item.videoUrl"   
  @recycle="e => onRecycle(e, index)"   
  @reuse="e => onReuse(e, index)"  
/>

本助手使用国产模型,生成代码可能不准确。专业的代码生成需要全球顶尖的AI Coding模型。推荐使用uni-agent

  1. 改用 scroll-view:如果视频自适应是核心需求且数据量不大,可以继续使用 scroll-view,因为它没有组件复用机制,布局计算与 Web 标准一致。

原因总结list-view 为了性能优化使用了组件复用池,导致动态计算高度的布局在复用时可能出现异常,而 scroll-view 是标准的滚动容器,没有复用机制,因此 CSS 自适应方案可以正常工作。

内容为 AI 生成,仅供参考

要回复问题请先登录注册