InfiniteScroll 无限滚动
在滚动内容接近底部时通知应用加载更多数据,并提供加载中和没有更多内容的页脚状态。
何时使用
- 消息流、动态、通知等需要连续向下浏览的内容。
- 数据按页从服务端追加,并且用户不需要跳到某个固定页码时。
- 需要页码导航、回到某一页时用 Pagination 分页;内容量巨大且行高固定时用 VirtualList 虚拟列表。
在线体验
示例包含同步追加、加载中、没有更多内容以及接近底部的触发距离。
基本用法
下面代码放在应用的页面文件中;完整的 main 和窗口初始化见 打开自绘窗口。
import (
"fmt"
"github.com/tangoui/tango/ui"
)
var shown = 12
func page(c *ui.Context) {
inf := ui.InfiniteScroll(c, func() {
for i := 0; i < shown; i++ {
ui.Text(c, fmt.Sprintf("消息 %d", i+1))
}
}).Height(320).Distance(24).Done(shown >= 60)
if inf.NeedMore() && shown < 60 {
shown += 12
}
}InfiniteScroll 的回调负责构建整个滚动内容。给它一个有界高度后,NeedMore() 会在距离底部不超过 Distance 时返回一次 true;应用追加数据后,内容高度变化会重新允许下一次触发。
常见用法
以下片段省略了 import。
真实请求的加载状态
触发后先把 loading 设为 true,请求完成后追加数据并设回 false。Loading(true) 会显示状态页脚并抑制新的 NeedMore,避免同一批数据被重复请求。
var (
messages []string
loading bool
finished bool
)
func loadMessages() {
// 在实际应用中发起请求,回到 UI 线程后更新 messages。
}
func asyncList(c *ui.Context) {
inf := ui.InfiniteScroll(c, func() {
for _, message := range messages {
ui.Text(c, message)
}
}).Height(360).Loading(loading).Done(finished)
if inf.NeedMore() && !loading && !finished {
loading = true
loadMessages()
}
}自定义触发距离和页脚文案
Distance(px) 让应用在离底部还有一段距离时就开始准备下一页。LoadingText 和 DoneText 接受空字符串以隐藏对应文字;状态仍保留无障碍语义。
func labeledInfinite(c *ui.Context) {
inf := ui.InfiniteScroll(c, func() {
for i := 0; i < shown; i++ {
ui.Text(c, fmt.Sprintf("项目 %d", i+1))
}
}).Height(300).Distance(80).
Loading(loading).Done(finished).
LoadingText("正在同步下一页").DoneText("已经到底了")
if inf.NeedMore() && !loading && !finished {
loading = true
}
}暂停加载与程序化滚动
Disabled(true) 暂停触发 NeedMore()。页脚仍由 Loading 和 Done 控制。ScrollTo 接受垂直像素偏移,Offset 读取当前偏移。
var pauseLoading bool
func controlledInfinite(c *ui.Context) {
inf := ui.InfiniteScroll(c, func() {
for i := 0; i < shown; i++ {
ui.Text(c, fmt.Sprintf("项目 %d", i+1))
}
}).Height(300).Disabled(pauseLoading)
if ui.Button(c, "回到顶部").Clicked() {
inf.ScrollTo(0)
}
ui.Text(c, fmt.Sprintf("偏移 %.0f px", inf.Offset())).Tertiary()
}短内容与已结束状态
当首屏内容不足以填满视口时,组件会继续检查是否需要加载;全部数据返回后要把 Done(true) 设上,避免短列表持续触发。Done 和 Loading 都应来自页面真实状态,而不是在回调中临时声明。
func finishedInfinite(c *ui.Context) {
ui.InfiniteScroll(c, func() {
ui.Text(c, "最后一条记录")
}).Height(300).Done(true).DoneText("没有更多记录")
}主题与样式
InfiniteScroll 使用滚动容器、文本和加载指示器的主题令牌;没有单独的组件令牌。ConfigProvider.Defaults("InfiniteScroll", ...) 支持 "distance"(像素)作为实例默认触发距离,单实例的 Distance 优先。
注意事项
- 触发语义:停留在触发范围内时,同一内容高度只触发一次。内容高度变化,或滚离触发范围后重返底部,都可再次触发。数据已到末尾时设置
Done(true)。 - 状态放在哪:
loading、finished、已加载条目和请求游标放在页面变量或ui.State中,不能在渲染函数里重置。 - 并发请求:先设置
loading再发请求,回调结果在 UI 线程写回数据;不要在多个NeedMore分支中同时启动同一页请求。 - 性能:组件会构建滚动区域内的全部条目;条目非常多且不需要连续追加时改用
VirtualList或Listy.Virtual。 - 无障碍:加载中页脚是状态区域并标记忙碌;自定义文字应准确描述请求状态。条目内按钮、图片仍要提供自己的可访问名称。
- RTL:页脚和滚动内容遵循当前方向,文字和行布局自动镜像。
示例代码
以下是上方在线演示运行的 Go 源码(来自官方 gallery),点击标题展开。函数接收的 c *ui.Context 由窗口的 UI 循环每帧传入,接入方式见 Go GUI 入门。
API
签名与说明由 site/tools/compdoc 从 Go 源码和文档注释提取;点击名称跳到 Go API 参考 的对应条目。
入口函数
ui.InfiniteScroll
func InfiniteScroll(c *Context, fn func(), opts ...layout.Option) *InfiniteScrollWInfiniteScroll builds fn (the items) inside a vertical scroll area. Give it a bounded height with Height or opts (layout.H / MaxH / Grow), then poll NeedMore:
inf := ui.InfiniteScroll(c, func() { for _, it := range items { ... } }).
Height(400).Loading(loading).Done(len(items) >= total)
if inf.NeedMore() { loading = true; go fetch() }
返回 *InfiniteScrollW,可链式调用的方法见 InfiniteScrollW 方法。
InfiniteScrollW 方法
ui.InfiniteScrollW:InfiniteScrollW is a scrolling column that asks for more content when the user nears its bottom.
| 方法 | 说明 | 签名 |
|---|---|---|
Disabled | Disabled stops NeedMore without showing a footer. | Disabled(on bool) *InfiniteScrollW |
Distance | Distance is how close (px) to the bottom NeedMore starts firing (Element infinite-scroll-distance; default 0). | Distance(px float32) *InfiniteScrollW |
Done | Done shows the "no more" footer and stops NeedMore. | Done(on bool) *InfiniteScrollW |
DoneText | DoneText(s string) *InfiniteScrollW | |
Height | Height fixes the viewport height. | Height(px float32) *InfiniteScrollW |
ID | ID returns the scroll container's ID. | ID() ID |
Layout | Layout applies layout options to the viewport. | Layout(opts ...layout.Option) *InfiniteScrollW |
Loading | Loading shows the loading footer and suppresses NeedMore. | Loading(on bool) *InfiniteScrollW |
LoadingText | LoadingText / DoneText replace the footer texts ("" hides the text). | LoadingText(s string) *InfiniteScrollW |
NeedMore | NeedMore reports, once per content height, that the viewport is within Distance of the bottom (or not yet filled) and more items should load. | NeedMore() bool |
Offset | Offset returns the vertical scroll offset. | Offset() float64 |
ScrollTo | ScrollTo sets the vertical scroll offset. | ScrollTo(y float64) *InfiniteScrollW |