Masonry 瀑布流
把不等高的项目依次放入当前最短列,形成支持响应式列数和虚拟化的瀑布流布局。
何时使用
- 卡片、图片或内容块高度不同,又希望自动填充多列时使用。
- 需要按断点改变列数,或需要向已有列表追加项目时使用。
- 项目天然是一行一行对齐、需要表头或列排序时,使用 Table 表格 或 List 列表 更合适。
- 单列内容只需要滚动时,直接使用 Scroll 滚动容器。
在线体验
基本用法
Masonry 接收项目数量和按索引构建项目的回调;设置已知高度后,布局可以在首帧直接稳定分列。
import (
"fmt"
"github.com/tangoui/tango/ui"
"github.com/tangoui/tango/ui/layout"
)
var cardHeights = []float32{96, 140, 112, 168}
func masonryPage(c *ui.Context) {
ui.Masonry(c, len(cardHeights), func(i int) {
ui.Column(c, func() {
ui.Text(c, fmt.Sprintf("卡片 %d", i+1)).Strong()
ui.Text(c, "内容块")
}, layout.Pad(12)).Border(1, c.Theme.BorderSecondary).Radius(c.Theme.RadiusLG)
}).Columns(2).Gutter(16).
ItemHeight(func(i int) float32 { return cardHeights[i] }).Show()
}常见用法
响应式列数与不等间距
ColumnsAt 按当前窗口断点覆盖默认列数;Gutters 可以分别设置水平和垂直间距。
import "github.com/tangoui/tango/ui"
func responsiveMasonry(c *ui.Context) {
ui.Masonry(c, 12, func(i int) {
ui.Text(c, "内容卡片")
}).Columns(3).
ColumnsAt(ui.XS, 1).
ColumnsAt(ui.SM, 2).
ColumnsAt(ui.MD, 4).
Gutters(16, 12).
ItemHeight(func(i int) float32 { return float32(80 + i%3*24) }).Show()
}固定项目身份与列
ItemKey 在项目顺序可能变化时提供稳定身份;ItemColumn 返回从 0 开始的列号,返回 -1 让项目继续自动落到最短列。
import "github.com/tangoui/tango/ui"
var masonryIDs = []string{"a", "b", "c", "d"}
func stableMasonry(c *ui.Context) {
ui.Masonry(c, len(masonryIDs), func(i int) {
ui.Text(c, masonryIDs[i])
}).Columns(2).
ItemKey(func(i int) any { return masonryIDs[i] }).
ItemColumn(func(i int) int {
if masonryIDs[i] == "a" { return 0 }
return -1
}).
ItemHeight(func(i int) float32 { return 100 }).Show()
}未知高度与估计值
不提供 ItemHeight 时,TanGo 会在绘制后测量项目高度;首帧使用 EstimatedHeight 排布,实测后再修正。
import (
"fmt"
"github.com/tangoui/tango/ui"
)
func measuredMasonry(c *ui.Context) {
ui.Masonry(c, 24, func(i int) {
ui.Text(c, fmt.Sprintf("第 %d 项:内容长度可能不同", i+1))
}).Columns(4).Gutter(12).EstimatedHeight(110).Show()
}虚拟化大列表
Virtual(height) 为瀑布流建立固定视口,只构建视口附近项目;Built() 可用于显示本帧实际构建数量。
Show() 后可用 Assignment() 读取每个项目所在的列。项目数量或分列结果有变化时,LayoutChanged() 会报告变化。
import (
"fmt"
"github.com/tangoui/tango/ui"
)
func virtualMasonry(c *ui.Context) {
m := ui.Masonry(c, 100000, func(i int) {
ui.Text(c, fmt.Sprintf("卡片 %d", i+1))
}).Columns(5).Gutter(12).EstimatedHeight(96).Virtual(360).Show()
ui.Text(c, fmt.Sprintf("本帧构建 %d 项", m.Built())).Tertiary()
}主题与样式
Masonry 只负责分列和滚动,不替项目绘制背景。项目卡片可使用 c.Theme.BgContainer、c.Theme.BorderSecondary、c.Theme.RadiusLG 等当前主题令牌;Ant、Element Plus 和 shadcn 的差异来自这些令牌,而不是 Masonry 自己的变体。
注意事项
- 状态:项目状态放在页面函数外或
ui.State中;虚拟化会反复构建项目,不能把业务状态只放在一次回调调用的局部变量里。 - 稳定性:追加项目时用
ItemKey和必要的ItemColumn,避免已有项目身份或列位置随数据重排。 - 性能:大量项目优先提供
EstimatedHeight并使用Virtual;已知固定高度时使用ItemHeight可减少测量和重排。 - 无障碍:每个项目会以列表项语义构建;项目内部的图标按钮仍需
A11yLabel,不要只用序号作为唯一可读名称。 - RTL:列的内联起点会遵循当前方向;
ItemColumn仍使用从 0 开始的逻辑列号。
示例代码
以下是上方在线演示运行的 Go 源码(来自官方 gallery),点击标题展开。函数接收的 c *ui.Context 由窗口的 UI 循环每帧传入,接入方式见 Go GUI 入门。
API
签名与说明由 site/tools/compdoc 从 Go 源码和文档注释提取;点击名称跳到 Go API 参考 的对应条目。
入口函数
ui.Masonry
func Masonry(c *Context, n int, render func(i int)) *MasonryWMasonry lays out n items built by render(i) in columns, each item going to the shortest column so far.
ui.Masonry(c, len(cards), func(i int) { card(i) }).Columns(4).Gutter(16).Show()
返回 *MasonryW,可链式调用的方法见 MasonryW 方法。
MasonryW 方法
ui.MasonryW:MasonryW is a masonry (waterfall) layout. Configure it and call Show.
| 方法 | 说明 | 签名 |
|---|---|---|
Assignment | Assignment returns the column of every item (after Show). | Assignment() []int |
Built | Built returns how many items were built by Show (virtual mode builds only the visible ones). | Built() int |
Columns | Columns sets the column count (default 3). | Columns(n int) *MasonryW |
ColumnsAt | ColumnsAt sets the column count from breakpoint bp upwards (Ant columns={{ xs: 1, sm: 2, md: 3 }}); the largest set breakpoint not above the window width wins, Columns is the fallback. | ColumnsAt(bp Breakpoint, n int) *MasonryW |
EstimatedHeight | EstimatedHeight is the height assumed for items not measured yet. | EstimatedHeight(px float32) *MasonryW |
Gutter | Gutter sets the horizontal and vertical spacing. | Gutter(px float32) *MasonryW |
Gutters | Gutters sets horizontal and vertical spacing separately. | Gutters(h, v float32) *MasonryW |
ID | ID returns the viewport's ID in virtual mode (after Show). | ID() ID |
ItemColumn | ItemColumn pins items to a column (0-based; return -1 for automatic), so existing items keep their column when more are added. | ItemColumn(fn func(i int) int) *MasonryW |
ItemHeight | ItemHeight gives known item heights (Ant item.height); items then get that fixed height. | ItemHeight(fn func(i int) float32) *MasonryW |
ItemKey | ItemKey gives items stable identities (default: the index). | ItemKey(fn func(i int) any) *MasonryW |
LayoutChanged | LayoutChanged reports that Show placed some item in another column than the previous Show, or the item count changed (Ant onLayoutChange; read the columns with Assignment). | LayoutChanged() bool |
Show | Show builds the layout. | Show() *MasonryW |
Virtual | Virtual scrolls the layout inside a viewport of the given height and builds only the items near it. | Virtual(height float32) *MasonryW |