Listy 高性能列表
面向长数据的列表组件,支持可变行高、虚拟滚动、分组吸顶、稳定行键、程序化滚动和加载更多。
何时使用
- 长列表的行高由内容决定,或同一列表中有长短不同的描述。
- 需要分组标题吸顶、按键定位或在滚动到底部时追加数据。
- 每行固定高度且只追求最小开销时用 VirtualList 虚拟列表;需要列结构、排序筛选时用 Table 表格。
在线体验
示例包含十万条分组数据、粘性分组标题、可变行高以及加载更多。
基本用法
下面代码放在应用的页面文件中;完整的 main 和窗口初始化见 打开自绘窗口。
import (
"fmt"
"github.com/tangoui/tango/ui"
)
var users = []string{"Ada", "Linus", "Grace"}
func page(c *ui.Context) {
ui.Listy(c, len(users), func(i int) {
ui.Text(c, fmt.Sprintf("%s 的资料", users[i]))
}).Show()
}Listy(c, n, item) 声明 n 个条目,item(i) 构建第 i 行,最后调用 Show()。没有 Height 时列表按普通列一次构建全部行;加上 Height 和 Virtual() 后只构建视口附近的行。
常见用法
以下片段沿用基本用法里的 users,省略了 import。
虚拟滚动与分组吸顶
Group 按首次出现的分组顺序生成标题,Sticky 把当前分组标题固定在视口顶部。虚拟滚动必须同时设置 Height。
var groupJump bool
func groupedList(c *ui.Context) {
if ui.Button(c, "跳到 G 组").Clicked() {
groupJump = true
}
list := ui.Listy(c, 100000, func(i int) {
ui.Text(c, fmt.Sprintf("用户 #%d", i+1))
}).Group(
func(i int) string { return string(rune('A' + i%26)) },
func(key string, count int) { ui.Text(c, fmt.Sprintf("%s · %d 人", key, count)) },
).Sticky().Virtual().Height(360)
if groupJump {
list.ScrollToGroup("G", ui.AlignTop, 0)
groupJump = false
}
list.Show()
}可变行高与稳定行键
默认会在绘制后测量行高;EstimatedItemHeight 是还没有测量到的行所使用的估计值。数据会插入、删除或重排时,用 RowKey 返回稳定键,避免行内状态跟着索引错位。
条目总数不变时,如果调整了数据顺序、RowKey 或 Group 的结果,递增并传入 DataVersion(version),让列表重算分组与滚动偏移。
type notice struct {
ID string
Title string
Details string
}
var notices = []notice{{"a", "短通知", ""}, {"b", "长通知", "这是一段较长的描述,用来演示 Listy 的可变行高。"}}
func variableRows(c *ui.Context) {
ui.Listy(c, len(notices), func(i int) {
ui.Text(c, notices[i].Title).Strong()
if notices[i].Details != "" {
ui.Paragraph(c, notices[i].Details)
}
}).RowKey(func(i int) any { return notices[i].ID }).
EstimatedItemHeight(52).Virtual().Height(320).Show()
}加载更多
LoadMore(hasMore, loading) 加入底部状态。有更多数据且未加载时,滚动到距底部 100px 以内会让 EndReached() 返回 true。发起请求时马上设置 loading,追加数据后设回 false。
var (
itemCount = 30
listLoading bool
listHasMore = true
)
func moreRows(c *ui.Context) {
list := ui.Listy(c, itemCount, func(i int) {
ui.Text(c, fmt.Sprintf("通知 %d", i+1))
}).Virtual().Height(320).LoadMore(listHasMore, listLoading).Show()
if list.EndReached() && !listLoading && listHasMore {
listLoading = true
itemCount += 30 // 实际应用中在请求完成后更新
listLoading = false
listHasMore = itemCount < 120
}
}点击与程序化滚动
Clicked 返回本次 Show 中点击的条目索引;ScrollToKey 需要先用 RowKey 建立稳定键,ScrollTo 则直接接受像素偏移。对分组列表还可以用 ScrollToGroup。
var focusUser = "b"
func navigableList(c *ui.Context) {
list := ui.Listy(c, len(users), func(i int) {
ui.Text(c, users[i])
}).RowKey(func(i int) any { return users[i] }).Height(240).Virtual()
list.ScrollToKey(focusUser, ui.AlignAuto, 0).Show()
if i, ok := list.Clicked(); ok {
ui.Text(c, fmt.Sprintf("点击了第 %d 项", i+1)).Tertiary()
}
}数据原地变化
如果切片长度和行键数量不变,但分组或行内容在原地修改,用 DataVersion 递增版本号,让列表重新计算分组、偏移和缓存。
var listVersion int
func refreshedList(c *ui.Context) {
ui.Listy(c, len(users), func(i int) { ui.Text(c, users[i]) }).
DataVersion(listVersion).Show()
}主题与样式
Ant Design 风格默认有行分隔线和悬停背景;Element Plus 使用更紧凑的行视觉;shadcn 保留分隔结构并使用较重的标题字重。RowHoverable(false) 和 Split(false) 可在实例级关闭悬停和分隔线。
ConfigProvider.Defaults("Listy", ...) 支持 "split" 与 "rowHoverable";实例上的 Split、RowHoverable 优先。列表外框、圆角和内边距仍由父布局或主题通用令牌控制。
注意事项
- 调用顺序:
Group、Sticky、Virtual、Height、LoadMore等配置要在Show前调用;Range、Clicked、EndReached在Show后读取。 - 状态放在哪:数据、加载状态、版本号和跳转目标放在页面变量或
ui.State中。插入、删除或排序时务必提供稳定RowKey。 - 虚拟化条件:
Virtual()需要Height;没有固定视口时它无法计算可见范围。行高未固定时让组件测量,全部固定时用ItemHeight提高速度。 - 性能:
Range的返回区间包含分组标题;EstimatedItemHeight过于偏离真实高度会造成初始滚动位置在测量后修正。 - 无障碍:列表及列表项使用列表语义;行内按钮、链接和输入框仍需可访问名称。分组标题会作为标题语义构建。
- RTL:行布局、分隔线和粘性标题遵循当前 RTL 方向。
示例代码
以下是上方在线演示运行的 Go 源码(来自官方 gallery),点击标题展开。函数接收的 c *ui.Context 由窗口的 UI 循环每帧传入,接入方式见 Go GUI 入门。
API
签名与说明由 site/tools/compdoc 从 Go 源码和文档注释提取;点击名称跳到 Go API 参考 的对应条目。
入口函数
ui.Listy
func Listy(c *Context, n int, item func(i int)) *ListyWListy starts a list of n items; item(i) builds the content of row i (padded 12/16 with a divider and hover background).
ui.Listy(c, len(users), func(i int) { ui.Text(c, users[i].Name) }).
Virtual().Height(400).Group(byLetter, letterHeader).Sticky().Show()
返回 *ListyW,可链式调用的方法见 ListyW 方法。
ListyW 方法
ui.ListyW:ListyW is a Listy list. Configure it and call Show.
| 方法 | 说明 | 签名 |
|---|---|---|
Clicked | Clicked returns the item clicked in Show. | Clicked() (int, bool) |
DataVersion | DataVersion tells the list that the data behind n, RowKey or Group changed without n changing (grouping and offsets are cached by n and this number). | DataVersion(v int) *ListyW |
EndReached | EndReached reports that the viewport is within 100px of the end while more data can be loaded (after Show). | EndReached() bool |
EstimatedItemHeight | EstimatedItemHeight is assumed for items not measured yet (default 46). | EstimatedItemHeight(px float32) *ListyW |
Group | Group groups items by key(i) (items with equal keys are gathered in order of first appearance) under headers built by title(key, count). | Group(key func(i int) string, title func(key string, count int)) *ListyW |
Height | Height fixes the viewport height; longer content scrolls. | Height(px float32) *ListyW |
ID | ID returns the viewport's ID (after Show; 0 without Height). | ID() ID |
ItemHeight | ItemHeight fixes every item's height (fast path; default: measured). | ItemHeight(px float32) *ListyW |
LoadMore | LoadMore adds a footer: a spinner while loading, "no more" when hasMore is false. | LoadMore(hasMore, loading bool) *ListyW |
Offset | Offset returns the scroll offset (0 without a viewport). | Offset() float64 |
Range | Range returns the rows [first, end) built by Show (headers count as rows). | Range() (first, end int) |
RowHoverable | RowHoverable toggles the row hover background (default on). | RowHoverable(on bool) *ListyW |
RowKey | RowKey gives rows stable identities (default: the index). | RowKey(fn func(i int) any) *ListyW |
ScrollTo | ScrollTo scrolls to a vertical pixel offset. | ScrollTo(px float64) *ListyW |
ScrollToGroup | ScrollToGroup scrolls to a group header. | ScrollToGroup(group string, align string, offset float64) *ListyW |
ScrollToKey | ScrollToKey scrolls to the item with key (RowKey) using align (AlignTop, AlignBottom or AlignAuto) plus offset pixels. | ScrollToKey(key any, align string, offset float64) *ListyW |
Show | Show builds the list. | Show() *ListyW |
Split | Split toggles the row dividers (default on). | Split(on bool) *ListyW |
Sticky | Sticky pins the current group header to the top while scrolling. | Sticky() *ListyW |
Virtual | Virtual builds only the visible rows (needs Height). | Virtual() *ListyW |