Skip to content

xtf-virtual-list ​

组件说明 ​

xtf-virtual-list 是虚拟列表,用于只渲染可视区附近的定高列表项。当前实现读取 items 数据、通过 scroll 等事件反馈交互、提供实例方法。

  • 实际 Demo 路由:#/pages/components/xtf-virtual-list/index
  • Demo 源码

基础用法 ​

1. 最小可用 ​

提供能够直接看到或操作组件的最少业务内容。

vue
<template>
  <view class="doc-minimum">
    <XtfVirtualList :items="fixture" :style="{ height: '360px' }" />
  </view>
</template>

<script setup lang="uts">
import XtfVirtualList from "@/uni_modules/xtf-linkuiunix/components/xtf-virtual-list/xtf-virtual-list.uvue"

const fixture = Array.from({ length: 30 }, (_: any, index: number): any => ({ id: index, title: '记录 ' + index.toString() }))
</script>

<style>
.doc-minimum { display: flex; flex-direction: column; padding: 24rpx; }
</style>

2. 核心数据与主要事件 ​

使用组件真实数据入口,并记录当前源码声明的主要事件。

vue
<template>
  <view class="doc-core">
    <XtfVirtualList :items="fixture" :style="{ height: '360px' }" @scroll="onPrimaryEvent" />
  </view>
</template>

<script setup lang="uts">
import XtfVirtualList from "@/uni_modules/xtf-linkuiunix/components/xtf-virtual-list/xtf-virtual-list.uvue"

const fixture = Array.from({ length: 30 }, (_: any, index: number): any => ({ id: index, title: '记录 ' + index.toString() }))
function onPrimaryEvent(payload: any): void { console.log("xtf-virtual-list:scroll", payload) }
</script>

<style>
.doc-core { display: flex; flex-direction: column; padding: 24rpx; }
</style>

3. 动态 loadingMore 状态 ​

运行时切换 loadingMore,观察组件对应状态变化。

vue
<template>
  <view class="doc-dynamic">
    <XtfVirtualList :items="fixture" :style="{ height: '360px' }" :loading-more="state" />
    <button class="doc-action-button" @click="toggleState">切换 loadingMore</button>
    <text class="doc-state-text">loadingMore: {{ state }}</text>
  </view>
</template>

<script setup lang="uts">
import XtfVirtualList from "@/uni_modules/xtf-linkuiunix/components/xtf-virtual-list/xtf-virtual-list.uvue"

import { ref } from "vue"
const fixture = Array.from({ length: 30 }, (_: any, index: number): any => ({ id: index, title: '记录 ' + index.toString() }))
const state = ref(false)
function toggleState(): void { state.value = !state.value; console.log("loadingMore", state.value) }
</script>

<style>
.doc-dynamic { display: flex; flex-direction: column; padding: 24rpx; }
.doc-action-button { margin-top: 16rpx; }
.doc-state-text { margin-top: 12rpx; color: #475569; font-size: 24rpx; }
</style>

4. 定制 default 插槽 ​

使用源码真实声明的 default slot 替换对应内容。

vue
<template>
  <view class="doc-slot">
    <XtfVirtualList :items="fixture" :style="{ height: '360px' }">
      <template #default="scope"><text class="doc-content-text">自定义 default 内容:{{ scope.item }}</text></template>
    </XtfVirtualList>
  </view>
</template>

<script setup lang="uts">
import XtfVirtualList from "@/uni_modules/xtf-linkuiunix/components/xtf-virtual-list/xtf-virtual-list.uvue"

const fixture = Array.from({ length: 30 }, (_: any, index: number): any => ({ id: index, title: '记录 ' + index.toString() }))
</script>

<style>
.doc-slot { display: flex; flex-direction: column; padding: 24rpx; }
.doc-content-text { color: #334155; font-size: 26rpx; }
</style>

5. 调用 refresh() ​

重新计算或刷新;签名为 refresh(): void。

vue
<template>
  <view class="doc-method">
    <XtfVirtualList :items="fixture" :style="{ height: '360px' }" ref="componentRef" />
    <button class="doc-action-button" @click="callMethod">重新计算或刷新</button>
  </view>
</template>

<script setup lang="uts">
import XtfVirtualList from "@/uni_modules/xtf-linkuiunix/components/xtf-virtual-list/xtf-virtual-list.uvue"

import { ref } from "vue"
const fixture = Array.from({ length: 30 }, (_: any, index: number): any => ({ id: index, title: '记录 ' + index.toString() }))
const componentRef = ref<any>(null)
function callMethod(): void { componentRef.value?.refresh(); console.log("调用 refresh") }
</script>

<style>
.doc-method { display: flex; flex-direction: column; padding: 24rpx; }
.doc-action-button { margin-top: 16rpx; }
</style>

6. 父布局变化后显式 refresh() ​

父容器需提供确定高度;本例切换视口高度后重新测量。itemHeight 按 px 计算且必须与实际定高行一致,refresh 不会测量每条内容高度。

vue
<template>
  <view class="doc-layout-refresh">
    <XtfVirtualList :items="fixture" ref="componentRef" :style="{ height: viewportHeight.toString() + 'px' }" />
    <button class="doc-action-button" @click="refreshLayout">切换高度并刷新</button>
  </view>
</template>

<script setup lang="uts">
import XtfVirtualList from "@/uni_modules/xtf-linkuiunix/components/xtf-virtual-list/xtf-virtual-list.uvue"

import { nextTick, ref } from "vue"
const fixture = Array.from({ length: 30 }, (_: any, index: number): any => ({ id: index, title: '记录 ' + index.toString() }))
const componentRef = ref<any>(null)
const viewportHeight = ref(360)
async function refreshLayout(): Promise<void> { viewportHeight.value = viewportHeight.value == 360 ? 240 : 360; await nextTick(); componentRef.value?.refresh(); console.log("refresh 已请求") }
</script>

<style>
.doc-layout-refresh { display: flex; flex-direction: column; padding: 24rpx; }
.doc-action-button { margin-top: 16rpx; }
</style>

全部属性 ​

属性源码类型withDefaults 默认值实际行为 / 范围适用范围
itemsany[]() => [] as any[]参与组件运行逻辑;源码在 6 处读取该值;消费位置:script 行 22、script 行 24、script 行 26、script 行 27数据驱动或选项渲染场景。
itemHeightnumber88设置对应区域高度;消费位置:script 行 19需要调整布局或视觉表现时设置。
bufferSizenumber4设置对应元素尺寸;消费位置:script 行 20需要调整布局或视觉表现时设置。
keyFieldstring"id"兼容接收,当前实现未消费/不产生行为仅用于兼容现有调用;当前版本不应依赖其效果。
visibleCountnumber8参与组件运行逻辑;源码在 2 处读取该值;消费位置:script 行 21需要覆盖组件默认业务值时设置。
loadingMorebooleanfalse参与组件运行逻辑;源码在 1 处读取该值;消费位置:script 行 23需要控制显示、可用性或交互状态时设置。
finishedbooleanfalse参与组件运行逻辑;源码在 1 处读取该值;消费位置:script 行 23需要覆盖组件默认业务值时设置。

事件 ​

事件名称源码触发边界回调参数参数说明
scrollhandleScroll 内触发(script 行 33);参数表达式:{ scrollTop: scrollTop.value, startIndex: startIndex.value, endIndex: endIndex.value }(...args: any[])表中参数表达式来自当前源码直接调用点;禁用、守卫失败或未进入该函数分支时不会触发。
scroll-to-lowerhandleScrollToLower 内触发(script 行 36);参数表达式:无回调参数(...args: any[])表中参数表达式来自当前源码直接调用点;禁用、守卫失败或未进入该函数分支时不会触发。
visible-range-changesetup 内触发(script 行 60);参数表达式:{ startIndex: startIndex.value, endIndex: endIndex.value }(...args: any[])表中参数表达式来自当前源码直接调用点;禁用、守卫失败或未进入该函数分支时不会触发。

插槽 ​

插槽Scope 参数实际渲染位置
default{ item, index }仅在模板对应 <slot> 渲染位置生效。

方法 ​

实例方法 ​

方法签名 / 返回值调用边界
scrollToIndexscrollToIndex(index: number): void滚动到指定索引;通过组件 ref 调用,参数和返回值按签名处理。
refreshrefresh(): void重新计算或刷新;通过组件 ref 调用,参数和返回值按签名处理。

公开状态 ​

状态类型 / 包装读取边界
--当前组件没有公开状态。

主题说明 ​

  • 当前组件 CSS 未读取 var(--xtf-*) 主题变量;颜色或尺寸可能来自 props、固定样式或父级上下文。

平台与实现边界 ​

  • 基线为 HBuilderX 5.26+ 与 uni-app x;当前报告已验证 Web Chromium 的对应 Demo,不据此承诺 Safari、小程序或 App 原生端等价。
  • Android、iOS、HarmonyOS、Vapor 原生路径、触摸手势、字体、Canvas、安全区和系统权限能力,只有源码或完整性报告明确覆盖的部分才可视为已验证。
  • 本文按当前源码生成,不补入旧版 xtf-linkui 存在但迁移版未实现的 props、事件、slot 或方法。
  • 当前仅兼容接收、未发现运行消费的 props:keyField。
  • refresh(): void 在 nextTick 重新测量视口,不测量每行;父级必须提供明确高度,itemHeight 单位为 px,行内容必须定高。WEB 仅在存在 ResizeObserver 时观察,APP 条件编译 UniResizeObserver,小程序无自动尺寸观察,应在父布局变化后显式 refresh。卸载断开观察并使旧测量任务失效。
  • 详细公共边界见 平台与实现边界,逐项运行证据见项目根目录 COMPONENT_COMPLETENESS.md。

MIT Licensed