Skip to content

VirtualList: fixed-height virtual lists

Build fixed-height virtual lists with VirtualList. This page includes a runnable example, all props, and behavior constraints.

Complete example

Complete environment setup first. Copy this complete main.go and run go run ..

go
package main

import (
	"fmt"
	"github.com/dxui-org/dxui"
	"log"
)

func main() {
	items := make([]string, 10000)
	for i := range items {

		items[i] = fmt.Sprintf("item-%d", i)
	}
	app := dxui.NewApp(dxui.AppOptions{
		Title:      "VirtualList",
		Width:      640,
		Height:     480,
		Background: dxui.RGBA(248, 250, 252, 255),
	})
	if err := app.Run(func() dxui.View {
		return dxui.Box(
			dxui.BoxProps{
				Gap: 16,
				Style: dxui.Style{
					Padding: dxui.Padding(24),
				},
			},
			dxui.VirtualList(dxui.VirtualListProps{
				Key: "items",
				Style: dxui.Style{
					Height: dxui.Px(320),
					Width:  dxui.Px(400),
				},
				Count:     len(items),
				Version:   1,
				RowHeight: 36,
				Overscan:  3,
				ItemKey: func(i int) string {
					return items[i]
				},
				Build: func(i int) dxui.View {
					return dxui.Label(items[i])
				},
			}),
		)
	}); err != nil {

		log.Fatal(err)
	}
}

Parameters and API

go
func VirtualList(props VirtualListProps) View
FieldTypePurpose, defaults, and constraints
KeystringStable unique identity within one parent; empty by default. Use business IDs in dynamic lists, not changing indices.
StyleStyleLocal layout, paint, and text styling; zero uses intrinsic sizes and theme defaults.
TokenComponentTokenComponent theme entry; empty selects its default.
StatesStateStylesPaint patches for actual interaction states; cannot fabricate state or change layout.
PointerPointerBehaviorDefaults to PointerAuto; PointerNone excludes the entire subtree from pointer participation, unlike Disabled.
CountintSnapshot item count; 0 is empty, maximum MaxVirtualListItems.
Versionuint64Increment when content/keys change; do not modify inside Build.
RowHeightfloat32Complete finite positive logical row height, including spacing.
OverscanintRetained rows outside the viewport, 0..MaxVirtualListOverscan.
InitialOffsetOption[Point]Optional initial mount offset; later changes do not reset internal position.
OffsetOption[Point]Optional controlled scroll position.
ScrollbarScrollbarPolicyDefaults to ScrollbarAuto; Always always shows, Hidden hides the track but permits scrolling.
ItemKeyfunc(index int) stringReturns stable nonempty keys unique across the full list; a pure UI-thread function.
Buildfunc(index int) ViewBuilds a row by index only in the mounted range; must have no side effects.
OnScrollfunc(Point)Proposes a complete offset; uncontrolled internal scrolling does not depend on this callback.

Behavior and limitations

Vertical fixed-height rows only; supply a finite pixel Height. RowHeight includes spacing; each row is constrained and clipped to it. MaxVirtualListItems=10,000,000; MaxVirtualListOverscan=256. ItemKey must be nonempty and globally unique within the list. Count/Version describe an immutable snapshot; increment Version for content/key changes. Callbacks run on the UI thread and must be pure. Leaving overscan unmounts rows and discards focus, IME, editor, and overlay state; keep business values outside the list. Uncontrolled scrolling anchors to the first surviving visible key; controlled Offset takes precedence.

All components share identity, style, and pointer rules. See full declarations for referenced enums, structs, and comments. Running examples explains build verification.