Appearance
Tabs 标签页
模板标签:<qtx-tabs>
手写 Vue 扩展的 Tab 容器,包一层原生 qtx-tabs 标签,提供 active / offscreen / maxInstances / 动画配置与 changeTab 命令式方法。
<qtx-tabs> 由 Android @QtComponent 原生视图 + Vue 透传壳提供。完整 API 见下文「API 参考」与独立 API 页。
适用场景
- TV 首页频道 + 内容网格:内容区用
qtx-tabs+qtx-tab-content,频道栏用qtx-channel-nav,页内板块流用qtx-list;完整可复制骨架见 TV 首页示例 - 少量 Tab 的页面切换
- 需要控制离屏缓存页数与切换动画方向
不适用场景
- 复杂数据驱动列表,用 qtx-list
Web 演示
Web 演示浏览器模拟演示 · TV 真机行为以实机为准
首页内容 · 板块流列表
maxInstancesanimationDirectionchangeTab()
核心能力:内容无关的页面容器、离屏预创建窗口、渲染实例上限、切换动画与焦点管理。
最近事件:—
示例
vue
<template>
<div class="page" :width="1920" :height="1080">
<qtx-tabs active="示例文本" animationDirection="horizontal" :animationDuration="24"></qtx-tabs>
</div>
</template>
<script setup lang="ts">
// QtxTabs 最小可运行示例;属性名来自 js/qtx/src/components/QtxTabs/QtxTabs.vue,
// 完整属性表见 ./api(由 npm run docs:gen 生成,禁止手写)。
</script>示例是仓库里真实的 .vue 文件,npm run docs:check 会用 qtx-tpl-loader 逐个编译,禁止占位写法。
Tab 内容子节点的布局契约
<qtx-tab-content> 是内容壳,接受任意数量的同级子 view(工具条、列表、纯文本等)。壳与 qtx-tabs 同高,每个子节点必须显式声明 width / height(px)。
「工具条 + 列表」不要用内容壳的默认纵向排布:qtx-list 是原生自定义 View,有内容时会覆盖排在前一位的同级子 view。两个子 view 的边界必须用 position-type: absolute 与 margin-top 显式声明:
vue
<qtx-tabs :width="1920" :height="CONTENT_H" :active="current" :offscreen="2" :maxInstances="4">
<qtx-tab-content tabKey="history" :width="1920" :height="CONTENT_H">
<div class="toolbar" :width="1920" :height="TOOLBAR_H"><!-- 该 Tab 的工具条 --></div>
<qtx-list class="feed" :width="1920" :height="CONTENT_H - TOOLBAR_H" :items="rows" :columns="3" />
</qtx-tab-content>
</qtx-tabs>css
.toolbar { position-type: absolute; left: 0px; top: 0px; width: 1920px; height: 72px; }
.feed { margin-top: 72px; }- 工具条绝对定位在内容壳顶部,固定高度、不随列表滚动;列表用
margin-top让出工具条高度,用CONTENT_H - TOOLBAR_H填满剩余高度,是唯一滚动容器。 CONTENT_H与TOOLBAR_H在进页时确定;换屏高只改CONTENT_H,列表高度由表达式推导,不逐处改像素。- 每个
qtx-tab-content可以声明自己的工具条与动作集,内容壳不感知业务字段。 - 屏高与工具条高度按进页时的静态配置传入,不要在运行时改变
qtx-tabs/qtx-tab-content的height。 - 已修复(
0.9.0-alpha.6起):此前qtx-tab-content内用标准 Vuev-if切换子节点类型(例如qtx-list↔ 空态div)、且切换发生在切 Tab 后的异步回填里时,Hippy 会把内容壳宽度当成高度,工具条被撑成height=1920、内容推到t=1992(Q9)。框架现已在TabsTabContentView的 View 层做 bounds 兜底,标准 Vue 条件渲染可以正常使用,不需要业务固定子节点。 - 仍推荐(不是必须)让
qtx-list常驻、只切换items:TV 端这样能避免反复销毁重建列表带来的跨桥开销与焦点丢失;首屏三态可直接用state状态能力,见 qtx-list 内容状态。
vue
<qtx-tab-content tabKey="history" :width="1920" :height="CONTENT_H">
<div class="toolbar" :width="1920" :height="TOOLBAR_H"><!-- 工具条 --></div>
<qtx-list class="feed" :width="1920" :height="CONTENT_H - TOOLBAR_H"
:items="rows.length ? rows : emptyRows" :columns="3" key-field="id" />
</qtx-tab-content>js
// 空态不是换成 div,而是给同一个列表喂一行 text 内置样式
const emptyRows = styledRows([
{ id: 'empty', type: 'text', title: '还没有记录', span: 3 },
]);首屏 loading / 空态 / 错误态请用 qtx-list 的 state 状态能力(见 qtx-list 内容状态),不必自己切节点类型。Q9 修复前的 [QTX-TABS] 编译期提示已随修复移除。
offscreen/maxInstances保留页面实例;切走再切回时,工具条状态、排序、布局、焦点与滚动位由使用方按tabId分桶保存,内容壳不重建。- 切换默认走 300ms 横向平移动画(
animationDuration默认300、animationDirection默认horizontal),写:animationDuration="0"才是硬切。切出动画期间 prev 页临时可见用于滑出,动画落地后稳态只有 active 页是VISIBLE:窗口内非 active 页为INVISIBLE(保留布局与列表数据)、超窗页GONE。业务不需要自己处理显隐,也不要依赖非 active 页参与绘制。 - 同一
qtx-tabs下的多个qtx-list默认共用全局缓存池,offscreen/maxInstances的回收语义不变;需要把不同页面的缓存池隔离开时,给同组列表传同一个poolKey、不同组用不同名称,同名列表共享一个池、异名列表互不影响。不传poolKey时行为与之前一致。
每个 Tab 自带工具条的完整可复制页面:
vue
<template>
<!-- 每个 Tab 自带工具条:工具条属于 tab content,不属于全局壳。
屏高与工具条高度只在进页时确定;列表高度由表达式推导,换屏高只改 QTX_TV.contentHeight。 -->
<div class="page" :width="1920" :height="1080">
<div class="top" :width="1920" :height="QTX_TV.navHeight">
<qtx-button text="返回" :width="120" :height="64" :autofocus="true" @click="onBackPressed" />
<qtx-channel-nav
:width="1800" :height="QTX_TV.navBarHeight" :items="channels" :active="current" :focusedKey="focusedKey"
orientation="horizontal" :itemWidth="160" :itemHeight="72" :gap="12"
@change="onChannelChange"
/>
</div>
<qtx-tabs :width="1920" :height="QTX_TV.contentHeight" :active="current"
:offscreen="2" :maxInstances="4" @change="onTabChange">
<!-- ① 历史:固定流式工具条,动作集只有清空 / 时间范围 -->
<qtx-tab-content tabKey="history" :width="1920" :height="QTX_TV.contentHeight">
<div class="toolbar" :width="1688" :height="TOOLBAR_H">
<qt-text text="历史工具条:清空 / 时间范围" :width="1000" :height="56" class="tb-t" />
<qtx-button text="清空历史" :width="220" :height="56" :focusable="true"
@click="onAction('history')" />
</div>
<qtx-list class="feed" :width="1920" :height="QTX_TV.contentHeight - TOOLBAR_H - NAV_TO_CONTENT"
:items="historyRows" :columns="3" key-field="id" @item-click="onItemClick">
<template #item="{ item }">
<div class="cell" :width="600" :height="200">
<qt-text :text="item.title" :width="600" :height="200" class="cell-t" />
</div>
</template>
</qtx-list>
</qtx-tab-content>
<!-- ② 本地目录:动作集完全不同,壳不认识任何业务词 -->
<qtx-tab-content tabKey="local" :width="1920" :height="QTX_TV.contentHeight">
<div class="toolbar" :width="1688" :height="TOOLBAR_H">
<qt-text text="本地工具条:面包屑 / 排序 / 视图" :width="1000" :height="56" class="tb-t" />
<qtx-button text="排序" :width="180" :height="56" :focusable="true" @click="onSort" />
<qtx-button text="网格⇄列表" :width="220" :height="56" :focusable="true" @click="onLayout" />
</div>
<qtx-list class="feed" :width="1920" :height="QTX_TV.contentHeight - TOOLBAR_H - NAV_TO_CONTENT"
:items="localRows" :columns="3" key-field="id" @item-click="onItemClick">
<template #item="{ item }">
<div class="cell" :width="600" :height="200">
<qt-text :text="item.title" :width="600" :height="200" class="cell-t" />
</div>
</template>
</qtx-list>
</qtx-tab-content>
</qtx-tabs>
</div>
</template>
<script setup lang="ts">
import { ref, reactive } from 'vue';
import { useESRouter } from '@extscreen/es3-router';
import { QTX_TV, QTX_TV_PAGE_RHYTHM } from '@quicktvui/qtx';
const router = useESRouter();
// 进页时确定,运行时不要改:换屏高只改 QTX_TV.contentHeight,列表高度自动推导
// 顶部安全区 / 导航行 / 内容区高度全部取自包级主题 QTX_TV。
// 这一页是「工具条放进 tab-content 随实例缓存」的另一种放法,才额外用到
// QTX_TV_PAGE_RHYTHM 的工具条高度与导航↔内容间距。
const TOOLBAR_H = QTX_TV_PAGE_RHYTHM.toolbarHeightPx;
const NAV_TO_CONTENT = QTX_TV_PAGE_RHYTHM.navToContentGapPx;
const channels = [
{ key: 'history', label: '历史' },
{ key: 'local', label: '本地目录' },
];
const current = ref('history');
const focusedKey = ref('history');
// 每个 Tab 的状态按 tabId 分桶;内容壳常驻,切回来不丢
const tabState = reactive<Record<string, { sort: string; layout: string }>>({
history: { sort: '时间↓', layout: '流式' },
local: { sort: '名称↑', layout: '网格' },
});
function makeRows(prefix: string): Array<{ id: string; title: string }> {
const rows: Array<{ id: string; title: string }> = [];
for (let i = 0; i < 12; i++) {
rows.push({ id: `${prefix}-${i}`, title: `${prefix} 卡片 ${i + 1}` });
}
return rows;
}
const historyRows = makeRows('历史');
const localRows = makeRows('本地');
function onChannelChange(key: string) {
current.value = key;
focusedKey.value = key;
}
function onTabChange(key: string) {
current.value = key;
}
function onBackPressed() {
router.back();
}
// 宿主按精确名字取页面级回调;<script setup> 必须显式暴露。
defineExpose({ onBackPressed });
function onItemClick(e: any) {
const id = e && e.item ? e.item.id : '';
if (id) router.push({ name: 'detail', params: { id: String(id) } });
}
function onSort() {
const s = tabState[current.value];
s.sort = s.sort === '名称↑' ? '名称↓' : '名称↑';
}
function onLayout() {
const s = tabState[current.value];
s.layout = s.layout === '网格' ? '列表' : '网格';
}
function onAction(key: string) {
tabState[key].layout = '流式';
}
</script>
<style scoped>
.page { width: 1920px; height: 1080px; background-color: #0B1016; flex-direction: column; padding-left: 0px; padding-right: 0px; padding-top: 0px; padding-bottom: 0px; }
.top { width: 1920px; height: 128px; flex-direction: row; align-items: center; padding-top: 56px; }
/* left 116 对齐 decoration 内容边界,top 32 对应 NAV_TO_CONTENT;标题↔动作 24 */
.toolbar { width: 1688px; height: 72px; flex-direction: row; align-items: center; background-color: #334155; position-type: absolute; left: 116px; top: 32px; }
.tb-t { width: 1000px; height: 56px; font-size: 26px; color: #FFFFFF; margin-right: 24px; }
/* margin-top 让出工具条与导航间距:qtx-list 有内容时会覆盖前一位同级子 view */
.feed { width: 1920px; margin-top: 104px; background-color: #1E293B; }
.cell { width: 600px; height: 200px; background-color: #2563EB; }
.cell-t { width: 600px; height: 200px; font-size: 30px; color: #FFFFFF; text-align: center; }
</style>与 qtx-channel-nav 的底层联动
qtx-tabs 与 qtx-channel-nav 写同一个 linkId 即在 native 层登记为一组:焦点移动到某个频道时,nav 直接驱动本容器切换,切换完成后再向页面派发 change。上层不必回写 active 来驱动内容区。
vue
<qtx-channel-nav :items="channels" :active="current" :focusedKey="focusedKey"
linkId="home" @change="onChannelChange" />
<qtx-tabs :active="current" linkId="home" :offscreen="2" :maxInstances="4">
<qtx-tab-content tabKey="history" :width="1920" :height="948"><!-- 该屏内容 --></qtx-tab-content>
</qtx-tabs>- 不写
linkId时:active仍是唯一的切换入口,行为与旧版一致。 - 上层
active与changeTab(key)保留;native 直驱后再回写同值active是幂等的。 - 上层仍需监听
change做数据加载、返回栈与按 tabId 的状态保存;载荷source区分focus/click/api/back(返回归位)。 - 与频道栏写同名
linkId后,三段式返回由 QtxChannelNav 指南 统一处理:当前 Tab 的列表滚离顶部时按返回先回顶并把焦点送回频道栏,再按才做频道归位。页面接线不变,不需要给列表绑定ref。 - 焦点进入 Tab 内容区后,页面要用
focusWithin=false告知频道栏,否则内容区的左右键仍会驱动频道栏切换、且频道栏会残留白色焦点胶囊;接线见 焦点区域与内容区方向键。 - 写同名
linkId后默认启用内容区焦点围栏。出界方向由 nav 与 tabs 的相对位置自动推导(横排频道栏在上方 → ↑,竖排在左侧 → ←,其它方位同理),不写死方向;只在该方向介入,其余方向完整交回qtx-list与平台原有逻辑(横向列表、嵌套 rail、行间移动、滚动与分页都不受影响),也不改候选集、不改 LayoutManager。优先级为「列表内部寻焦与滚动 > 跨界」:先让列表自己逐行移动并随焦点滚动,直到列表走到最边缘、该方向再无 item,下一次按键才允许跨界;列表在该方向还有数据时(即使 item view 尚未被创建)不会提前出界,列表会自行滚动把内容补出来。 - 跨界不指定固定落点:在「nav 子树 ∪ tabs 子树」这个整体内按方向做几何寻焦,nav、tab 内的工具条、列表 item 都是平等候选,谁离当前焦点最近就落到谁。因此
nav → 工具条 → 列表与反向都会自然逐层经过,不会跳过中间组件;没有中间层时落到内容壳,具体落点仍由列表自己决定。回到频道栏不派change、不切 Tab;频道栏内部方向键、顶部动作组,以及单个组件单独使用时的行为都不受影响。写:focusBoundary="false"可关闭;未写linkId时行为与旧版一致。 - 切换 Tab 时,上一个 Tab 的内容列表会自动滚回起点(含嵌套横向列表),下次切回从顶部开始;不写
linkId或写:focusBoundary="false"时保留各 Tab 原有滚动位置。单独使用的qtx-list不受影响,页面不需要自己调scrollToIndex(0)。 - 完整接线见 QtxChannelNav 指南 与
nav-tabs-link.vue示例。
使用约束与易错点
- active 是受控字符串 key:未建立
linkId时切换后要通过 @change 回写,否则视觉与状态不一致;建立linkId后切换由 native 完成,同值回写是幂等的 - changeTab(key) 是 defineExpose 暴露的方法,需要拿到组件 ref
- QtxTabContent 是配套内容组件,不在 index.ts 中单独导出目录
已知边界
- 内容壳的子节点需要确定尺寸:仅在样式中声明
flex: 1、不给height时,qtx-list不下发内容;剩余高度用CONTENT_H - TOOLBAR_H表达式传入。 - 「工具条 + 列表」不可只靠
flex-direction: column排列:qtx-list有内容时会覆盖前一位同级子 view,须按上文契约用绝对定位与margin-top锁死边界。 - 运行时改变
qtx-tabs/qtx-tab-content的高度会使已有子 view 布局错乱,屏高应作为进页时的静态配置。 - 与 SDUI Grid-Tabs 是两套不同实现,不应混用同一套数据结构
- Tab 数量没有硬性上限;连续切换与切 Tab 后异步回填的数据一致性由组件内部保证,业务不需要做切换节流或限制 Tab 数。
- animationDirection 只接受 horizontal / vertical
- 内容区焦点围栏依赖 native 与 npm 同源:只升级 npm 包、未同步原生 AAR / 宿主时,向上寻焦仍按平台几何就近处理。
- 依赖 createQtxGate 引导,未 ready 时显示骨架屏
验证状态
当前该组件由文档编译门禁覆盖;组件级真机断言与回归口径见 验证体系。
API 参考
以下表格由
npm run docs:gen从源码生成,禁止手工编辑;完整独立页见 Tabs 标签页 API。
属性(Props · 7)
| 属性名 | 类型 | 必填 | 默认值 | 说明 | 事实源 |
|---|---|---|---|---|---|
active | string | 否 | - | - | vue-sfc |
animationDirection | 'horizontal' | 'vertical' | 否 | - | - | vue-sfc |
animationDuration | number | 否 | - | - | vue-sfc |
focusBoundary | boolean | 否 | - | 建立 linkId 后启用内容区焦点围栏:向上只回配对频道栏当前选中项,左右下出界不外逃;显式写 false 才关闭 | vue-sfc |
linkId | string | 否 | - | 与 qtx-channel-nav 建立底层联动的关系标识,两边写同名即成组 | vue-sfc |
maxInstances | number | 否 | - | - | vue-sfc |
offscreen | number | 否 | - | - | vue-sfc |
事件(Events · 1)
| 事件名 | 载荷参数 | 说明 | 事实源 |
|---|---|---|---|
@change | key: string | - | vue-sfc |
方法(Methods / Expose · 3)
| 方法名 | 参数 | 返回值 | 说明 | 事实源 |
|---|---|---|---|---|
changeTab | — | — | - | vue-sfc |
getNativeNode | — | — | - | vue-sfc |
if | — | — | - | vue-sfc |
插槽(Slots · 2)
| 插槽名 | 说明 | 事实源 |
|---|---|---|
default | 默认内容插槽,承载组件子内容 | js/qtx/src/components/QtxTabs/QtxTabs.vue |
item | 列表行模板插槽,作用域参数包含 item / index | docs-site/components/qtx-tabs/examples/tab-toolbar.vue |