Skip to content

Mobile ScrollView 滚动视图 ​

提供触摸滚动、惯性、回弹、下拉刷新、上拉加载和定高行虚拟构建的移动端滚动容器。

何时使用 ​

  • 页面内容超出可视区域时使用 ScrollView。
  • 下拉刷新使用 PullToRefresh,分页加载使用 InfiniteList。
  • 行高固定且数量很大时使用 Rows,让组件只构建可见行。
  • 横向轮播、滚轮或可吸附控件应使用 Swiper、PickerView,不要把 ScrollView 当作通用分页器。

在线体验 ​

真实 gallery 同时展示下拉刷新和上拉加载,拖动列表观察状态变化。

Mobile · PullToRefresh / InfiniteList · 在线体验真实 Go 组件
在独立页面打开 ↗

查看此场景的 Go 源码 ↗

基本用法 ​

ScrollView 的内容通过 Body 构建;在即时模式下,滚动位置由组件按稳定 ID 保存。

go
import (
    "fmt"
    "github.com/tangoui/tango/ui"
    "github.com/tangoui/tango/ui/layout"
    "github.com/tangoui/tango/ui/mobile"
)

func page(c *ui.Context) {
    mobile.ScrollView(c).Layout(layout.Grow(1)).Body(func() {
        mobile.List(c, "消息").Body(func() {
            for i := 0; i < 30; i++ {
                i := i
                ui.Key(c, i, func() {
                    mobile.Cell(c, fmt.Sprintf("消息 %d", i+1))
                })
            }
        })
    })
}

常见用法 ​

下拉刷新 ​

用户下拉超过阈值并松手时,组件将 refreshing 设为 true,Refreshed() 在这一帧返回 true。用它启动刷新,数据加载完成后将 refreshing 设回 false。

go
var refreshing bool

func refreshPage(c *ui.Context, startRefresh func()) {
    w := mobile.PullToRefresh(c, &refreshing).Layout(layout.Grow(1))
    w.Body(func() { mobile.List(c, "").Body(func() { mobile.Cell(c, "最新内容") }) })
    if w.Refreshed() {
        startRefresh() // 数据更新到 UI 线程后将 refreshing 设回 false。
    }
}

无限加载 ​

列表进入末端阈值且可以加载下一批时,组件将 loading 设为 true,并在这一帧通过 LoadMore() 报告。追加数据后将 loading 设回 false,全部加载完时设置 Finished(true)。Offset(px) 调整触发距离,默认 50px。

go
var loading bool

func infinitePage(c *ui.Context, startLoad func()) {
    w := mobile.InfiniteList(c, &loading).Layout(layout.Grow(1))
    w.Body(func() { mobile.List(c, "更多").Body(func() { mobile.Cell(c, "列表项") }) })
    if w.LoadMore() {
        startLoad() // 在 UI 线程追加数据并将 loading 设回 false。
    }
}

定高行 ​

Rows 只构建可视窗口附近的行,内置键为下标。数据会插入或重排时,可在回调中为行内控件设置 ui.Key。

go
func virtualPage(c *ui.Context) {
    mobile.ScrollView(c).Layout(layout.Grow(1)).Rows(10000, 48, func(i int) {
        mobile.Cell(c, fmt.Sprintf("第 %d 行", i+1))
    })
}

程序控制滚动 ​

用 ScrollOffset 读取当前位置,用 ScrollTo 跳到指定偏移;不要通过重建内容来模拟滚动。

go
func controlled(c *ui.Context, top bool) {
    w := mobile.ScrollView(c).Body(func() { mobile.Cell(c, "内容") })
    if top {
        w.ScrollTo(0)
    }
    _ = w.ScrollOffset()
}

注意事项 ​

滚动容器和内容的布局必须给出可用高度,页面常用 layout.Grow(1)。刷新和加载的 bool 指针放在稳定状态中;网络请求不要阻塞 UI 构建。大数据量使用 Rows 或分页,不要在每帧为数万行创建控件。滚动容器中的交互控件依赖手势仲裁,避免再套一层自定义垂直 Area。

示例代码 ​

以下是上方在线演示运行的 Go 源码(来自官方 gallery),点击标题展开。函数接收的 c *ui.Context 由窗口的 UI 循环每帧传入,接入方式见 Go GUI 入门。

Mobile · PullToRefresh / InfiniteList

gallery 演示 MobilePull 的 Render 函数,源码 page_mobile_gallery.go#L134。

加载中…

Mobile · 列表:List / Cell / SwipeCell / Badge / Tag

gallery 演示 MobileList 的 Render 函数,源码 page_mobile_gallery.go#L134。

加载中…

API ​

签名与说明由 site/tools/compdoc 从 Go 源码和文档注释提取;点击名称跳到 Go API 参考 的对应条目。

入口函数 ​

mobile.ScrollView ​

go
func ScrollView(c *ui.Context) *ScrollViewW

ScrollView creates a vertical touch scroll container. Give it a bounded height (Layout(layout.Grow(1)) in a sized column, or layout.H), set options, then build the content with Body.

返回 *ScrollViewW,可链式调用的方法见 ScrollViewW 方法。

mobile.PullToRefresh ​

go
func PullToRefresh(c *ui.Context, refreshing *bool) *ScrollViewW

PullToRefresh creates a ScrollView with a pull-down refresh header. Dragging past the threshold and releasing sets *refreshing = true and Refreshed reports it; set it back to false when the data has loaded.

返回 *ScrollViewW,可链式调用的方法见 ScrollViewW 方法。

mobile.InfiniteList ​

go
func InfiniteList(c *ui.Context, loading *bool) *ScrollViewW

InfiniteList creates a ScrollView that loads more when scrolled near the end: it sets *loading = true and LoadMore reports it. Set *loading back to false after appending rows; call Finished(true) when nothing is left.

返回 *ScrollViewW,可链式调用的方法见 ScrollViewW 方法。

相关类型与成员 ​

mobile.RefreshMachine ​

RefreshMachine is the pull-to-refresh state machine (pure).

ScrollViewW 方法 ​

mobile.ScrollViewW:ScrollViewW is a touch scroll container: finger tracking, inertial fling, rubber-band edges and bounce back, with optional pull-to-refresh header and infinite-load footer.

方法说明签名
BgBg sets the background color.Bg(col paintColor) *ScrollViewW
BodyBody builds the scrolling content.Body(fn func()) *ScrollViewW
ContentIDContentID returns the ID of the moving content box.ContentID() ui.ID
FailedFailed binds a load error flag: the footer offers a retry tap.Failed(failed *bool) *ScrollViewW
FinishedFinished marks the list complete (footer shows "no more").Finished(on bool) *ScrollViewW
IDID returns the container's ID.ID() ui.ID
InfiniteInfinite enables the load-more footer bound to *loading.Infinite(loading *bool) *ScrollViewW
LayoutLayout applies layout options to the container.Layout(opts ...layout.Option) *ScrollViewW
LoadMoreLoadMore reports that the list asked for more rows this frame.LoadMore() bool
OffsetOffset sets how close (px) to the end loading starts (default 50).Offset(px float32) *ScrollViewW
PullPull enables the pull-to-refresh header bound to *refreshing.Pull(refreshing *bool) *ScrollViewW
PullTextsPullTexts overrides the header texts (pulling, can release, refreshing, complete); empty entries keep the localized defaults.PullTexts(pulling, release, refreshing, complete string) *ScrollViewW
RefreshedRefreshed reports that the user pulled to refresh this frame.Refreshed() bool
RowsRows builds a long list of n rows of a fixed height rowH inside Body, creating only the rows in or near the visible part of the view: the rest is a blank of the right height, so the scroll range, the fling and the infinite-load trigger behave as if every row were there.Rows(n int, rowH float32, row func(i int))
ScrollOffsetOffset returns the scroll offset (negative while pulled down).ScrollOffset() float64
ScrollToScrollTo jumps to offset y (clamped).ScrollTo(y float64) *ScrollViewW
ScrollingScrolling reports whether the view is being dragged or still moving.Scrolling() bool
StatusStatus returns the pull-to-refresh status.Status() RefreshStatus

RefreshMachine 方法 ​

mobile.RefreshMachine:RefreshMachine is the pull-to-refresh state machine (pure).

字段说明类型
StatusRefreshStatus
Thresholdpull distance that arms the refreshfloat64
Delayhow long RefreshComplete staystime.Duration

RefreshMachine 的方法:

方法说明签名
FinishFinish ends a refresh at now.Finish(now time.Time)
HoldingHolding reports whether the header is held open.Holding() bool
PullPull updates the status for a drag at distance d (displayed px).Pull(d float64)
ReleaseRelease ends the drag; it reports whether a refresh starts.Release() bool
StartStart enters the refreshing state programmatically.Start()
TickTick advances timed transitions; it returns the time of the next one.Tick(now time.Time) (next time.Time)

相关链接 ​

以 MIT 许可发布