-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Collection Widgets
Andy Williams edited this page Apr 2, 2024
·
5 revisions
Note
This document is archived as it refers to a design process.
Collection widgets are used to efficiently display and interact with a collection of data.
- List - 1 Dimensional - []binding.Binding
- Table - 2 Dimensional - [][]binding.Binding
- Tree - Hierarchical - []binding.Binding or map[string]binding.Binding
// TODO
// TODO
- Collection widget asks the data binding how many items it has
- Collection widget asks the adapter how many types of items will be in the widget (eg. 1 for a simple list, or more for lists with header/footers/dividers) and creates a canvas.Pool for each type.
- For each visible index;
- Collection widget asks the adapter what the type of the item is.
- Collection widget asks the pool for a fyne.CanvasObject of that type (item/header/footer/divider), if the pool is empty a fyne.CanvasObject is created by the adapter.
- If the item is a data item;
- Collection widget asks the data binding for the item.
- Collection widget asks the adapter to bind the item to the fyne.CanvasObject.
There are four main components involved with displaying a list of items;
- binding.List - provides access to an underlying list of data and triggers listeners when it changes.
- canvas.Adapter - converts the specific data item from the binder into a fyne.CanvasObject for the list to display.
- canvas.Pool - maintains a collection of fyne.CanvasObject to be reused rather than allocating a new one.
- widget.List - renders a range of items from the data binding and receives events.
See Data API 2
type Adapter interface {
// Return the number of different types of cells.
Types() int
// Returns the type of the cell at the given index.
TypeOf(index int) int
// Creates a cell of the given type.
Create(type int) fyne.CanvasObject
// Binds the given canvas object to the binding.
Bind(fyne.CanvasObject, binding.Binding)
}
type Pool interface {
Obtain() fyne.CanvasObject
Release(fyne.CanvasObject)
}
list := &widget.List{}
list.BindItems(binding.List)
list.SetAdapter(canvas.Adapter)
list.OnSelected = func(index int) {
log.Println("Selected:", index)
}