Skip to content

Mobile List 列表 ​

用分组容器和 Cell 行展示移动端设置、菜单、摘要与可操作列表。

何时使用 ​

  • 需要带标题的分组行、描述、尾部值、箭头或开关时使用 List 与 Cell。
  • 需要滑动操作时使用 SwipeCell 包裹 Cell。
  • 复杂表格、列排序或桌面密集数据应使用桌面端 Table,不要把 List 当表格。
  • 超长列表把 List 放进 ScrollView,固定高度行可使用 Rows。

在线体验 ​

真实 gallery 展示分组列表、卡片列表、开关和 SwipeCell 操作。

Mobile · 列表:List / Cell / SwipeCell / Badge / Tag · 在线体验真实 Go 组件
在独立页面打开 ↗

查看此场景的 Go 源码 ↗

基本用法 ​

List 通过 Body 包含多个 Cell;Cell 的状态指针应由页面持有。

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

var wifi = true

func page(c *ui.Context) {
    mobile.List(c, "网络").Body(func() {
        mobile.Cell(c, "无线局域网").Switch(&wifi)
        mobile.Cell(c, "蜂窝网络").Extra("已开启").Arrow()
    })
    _ = ui.ID(0)
}

常见用法 ​

描述和尾部值 ​

Desc 展示补充信息,Extra 展示右侧值,Arrow 表示可以进入详情。

go
func summary(c *ui.Context) {
    mobile.List(c, "账号").Body(func() {
        mobile.Cell(c, "个人资料").Desc("头像、昵称、简介").Arrow()
        mobile.Cell(c, "通知").Extra("3 条未读").Arrow()
    })
}

可点击行 ​

调用 Clickable 后读取 Clicked;Changed 用于开关等值变化。

go
func actions(c *ui.Context) {
    cell := mobile.Cell(c, "清理缓存").Clickable()
    if cell.Clicked() {
        ui.Text(c, "正在清理")
    }
}

开关和禁用 ​

Switch 绑定 bool 指针,Disabled(true) 禁止交互但仍保留说明。

go
var enabled bool

func switches(c *ui.Context) {
    mobile.List(c, "功能").Body(func() {
        mobile.Cell(c, "通知").Switch(&enabled)
        mobile.Cell(c, "实验功能").Disabled(true).Switch(&enabled)
    })
}

卡片模式 ​

Card 给分组增加独立卡片外观,适合页面中多个互相独立的设置组。

go
func cards(c *ui.Context) {
    mobile.List(c, "账单").Card().Body(func() {
        mobile.Cell(c, "本月支出").Extra("¥ 128.00")
        mobile.Cell(c, "发票").Arrow()
    })
}

滑动操作 ​

SwipeCell 的操作项使用 SwipeAction,正文仍用 Body 构建。

go
func swipe(c *ui.Context) {
    mobile.SwipeCell(c).
        Right(mobile.SwipeAction{Text: "置顶", Color: "primary"}, mobile.SwipeAction{Text: "删除", Color: "danger"}).
        Body(func() { mobile.Cell(c, "向左滑动这一行") })
}

主题与样式 ​

List、Cell 和 SwipeCell 使用 Provider 的移动令牌;Card、分隔线和操作色会随 Ant Mobile、Vant、shadcn 改变。ConfigProvider.Defaults("mobile.List", ...) 和 mobile.Cell 的组件默认键可用于统一页面设置,具体键以 API 注释为准。

注意事项 ​

列表中的开关、输入和选择状态放在稳定的页面变量或闭包中;动态行必须用 ui.Key 保持身份。Cell 的点击和长按是逐帧状态,需在当前帧处理。操作文字应明确,危险操作使用 Color: "danger" 并提供确认流程。RTL 下尾部槽位和箭头会自动处理。

示例代码 ​

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

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

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

加载中…

API ​

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

入口函数 ​

mobile.List ​

go
func List(c *ui.Context, header string) *ListW

List starts a cell group titled header ("" for none); add cells in Body.

ConfigProvider defaults ("mobile.List"): "mode" ("default" or "card").

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

mobile.Cell ​

go
func Cell(c *ui.Context, title string) *CellW

Cell adds a list row titled title.

ConfigProvider defaults ("mobile.Cell"): "arrow", "clickable" (bool).

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

mobile.SwipeCell ​

go
func SwipeCell(c *ui.Context) *SwipeCellW

SwipeCell starts a swipeable row; set actions, then build the content with Body (typically one Cell).

ConfigProvider defaults ("mobile.SwipeCell"): "closeOnAction" (bool).

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

ListW 方法 ​

mobile.ListW:ListW is a group of cells with an optional header.

方法说明签名
BodyBody builds the cells.Body(fn func())
CardCard insets the group with rounded corners (Ant Mobile mode="card", Vant inset).Card() *ListW
LayoutLayout applies layout options to the outer box.Layout(opts ...layout.Option) *ListW

CellW 方法 ​

mobile.CellW:CellW is a list row: optional icon, title with description, value at the end, arrow, or switch (the end is the right; the left in RTL, where the arrow points left).

方法说明签名
ArrowArrow shows the navigation arrow and pressed feedback.Arrow() *CellW
ChangedChanged reports a switch toggle this frame.Changed() bool
ClickableClickable shows pressed feedback without an arrow.Clickable() *CellW
ClickedClicked reports a tap on the row (not on its switch).Clicked() bool
DescDesc shows a description line under the title.Desc(s string) *CellW
DisabledDisabled greys the row out and ignores taps.Disabled(on bool) *CellW
ExtraExtra shows a value at the end.Extra(s string) *CellW
IDID returns the row's ID.ID() ui.ID
IconIcon shows an icon before the title.Icon(ic *icon.Icon) *CellW
LongPressedLongPressed reports a long press on the row.LongPressed() bool
RequiredRequired marks the title with a red asterisk (forms).Required() *CellW
SwitchSwitch shows a switch bound to *on; tapping it toggles.Switch(on *bool) *CellW

SwipeCellW 方法 ​

mobile.SwipeCellW:SwipeCellW wraps content that can be swiped sideways to reveal actions.

Sides are logical: the Right actions sit at the end of the row and are revealed by swiping towards the start (to the left; in RTL the actions sit on the left and a swipe to the right reveals them), the Left ones at the start.

方法说明签名
ActionAction reports a tapped action this frame: side (1 Right / end, -1 Left / start), index into that side's actions.Action() (side, index int, ok bool)
ActionKeyActionKey returns the Key (or Text) of the tapped action, "" if none.ActionKey() string
BodyBody builds the row content.Body(fn func()) *SwipeCellW
CloseClose closes the cell.Close() *SwipeCellW
IsOpenIsOpen reports whether actions are (being) shown.IsOpen() bool
KeepOpenKeepOpen keeps the cell open after an action tap.KeepOpen() *SwipeCellW
LeftLeft sets the actions at the start, revealed by swiping right (left in RTL).Left(a ...SwipeAction) *SwipeCellW
OpenOpen opens the Right (end, side > 0) or Left (start, side < 0) actions.Open(side int) *SwipeCellW
RightRight sets the actions at the end, revealed by swiping left (right in RTL).Right(a ...SwipeAction) *SwipeCellW

相关链接 ​

以 MIT 许可发布