ListView

The ListView element implements a list.The ListView is created using the function:

func NewListView(session Session, params Params) ListView

ListView is implemented on top of ListLayout and therefore supports all ListLayout properties:"orientation", "list-wrap", "vertical-align", "horizontal-align", "list-row-gap", and "list-column-gap".

In addition to these properties ListView has the following:

The "items" property

List items are set using the "items" property (Items constant).The main value of the "items" property is the ListAdapter interface:

type ListAdapter interface {
	ListSize() int
	ListItem(index int, session Session) View
	IsListItemEnabled(index int) bool
}

Accordingly, the functions of this interface must return the number of elements,the View of the i-th element and the status of the i-th element (allowed/denied).

You can implement this interface yourself or use helper functions:

func NewTextListAdapter(items []string, params Params) ListAdapter
func NewViewListAdapter(items []View) ListAdapter

NewTextListAdapter creates an adapter from an array of strings, the second argumentis the parameters of the TextView used to display the text.NewViewListAdapter creates an adapter from the View array.

The "items" property can be assigned the following data types:

  • ListAdapter;
  • [] View, when assigned, is converted to a ListAdapter using the NewViewListAdapter function;
  • [] string, when assigned, is converted to a ListAdapter using the NewTextListAdapter function;
  • [] interface {} which can contain elements of type View, string, fmt.Stringer, bool, rune,float32, float64, int, int8 ... int64, uint, uint8 ... uint64.When assigning, all types except View and string are converted to string, then all string in TextViewand from the resulting View array using the NewViewListAdapter function, a ListAdapter is obtained.

If the list items change during operation, then after the change, either the ReloadListViewData()function of the ListView interface or the global ReloadListViewData(view View, subviewID ...string) function must be called.These functions update the displayed list items.

"Orientation" property

List items can be arranged both vertically (in columns) and horizontally (in rows).The "orientation" property (Orientation constant) of int type specifies how the list itemswill be positioned relative to each other. The property can take the following values:

Value Constant Location
0 TopDownOrientation Items are arranged in a column from top to bottom.
1 StartToEndOrientation Elements are laid out on a line from beginning to end.
2 BottomUpOrientation Items are arranged in a column from bottom to top.
3 EndToStartOrientation Elements are arranged in a row from end to beginning.

The start and end positions for StartToEndOrientation and EndToStartOrientation dependon the value of the "text-direction" property. For languages written from right to left(Arabic, Hebrew), the beginning is on the right, for other languages - on the left.

You can get the value of this property using the function

func GetListOrientation(view View, subviewID ...string) int

"wrap" property

The "wrap" int property (Wrap constant) defines the position of elementsin case of reaching the border of the container. There are three options:

  • WrapOff (0) - the column/row of elements continues and goes beyond the bounds of the visible area.
  • WrapOn (1) - starts a new column/row of items. The new column is positioned towards the end(for the position of the beginning and end, see above), the new line is at the bottom.
  • WrapReverse (2) - starts a new column/row of elements. The new column is positioned towardsthe beginning (for the position of the beginning and end, see above), the new line is at the top.

You can get the value of this property using the function

func GetListWrap(view View, subviewID ...string) int

"item-width" and "item-height" properties

By default, the height and width of list items are calculated based on their content.This leads to the fact that the elements of the vertical list can have different heights,and the elements of the horizontal - different widths.

You can set a fixed height and width of the list item. To do this, use the SizeUnitproperties "item-width" and "item-height"

You can get the values of these properties using the functions

func GetListItemWidth(view View, subviewID ...string) SizeUnit
func GetListItemHeight(view View, subviewID ...string) SizeUnit

"item-vertical-align" property

The "item-vertical-align" int property (ItemVerticalAlign constant) sets the vertical alignmentof the contents of the list items. Valid values:

Value Constant Name Alignment
0 TopAlign "top" Top alignment
1 BottomAlign "bottom" Bottom alignment
2 CenterAlign "center" Center alignment
3 StretchAlign "stretch" Height alignment

You can get the value of this property using the function

func GetListItemVerticalAlign(view View, subviewID ...string) int

"item-horizontal-align" property

The "item-horizontal-align" int property (ItemHorizontalAlign constant) sets thehorizontal alignment of the contents of the list items. Valid values:

Value Constant Name Alignment
0 LeftAlign "left" Left alignment
1 RightAlign "right" Right alignment
2 CenterAlign "center" Center alignment
3 StretchAlign "stretch" Height alignment

You can get the value of this property using the function

GetListItemHorizontalAlign(view View, subviewID ...string) int

"current" property

ListView allows you to select list items with the "allowed" status (see ListAdapter).The item can be selected both interactively and programmatically.To do this, use the int property "current" (constant Current).The value "current" is less than 0 means that no item is selected

You can get the value of this property using the function

func GetCurrent(view View, subviewID ...string) int

"list-item-style", "current-style", and "current-inactive-style" properties

These three properties are responsible for the background style and text properties of each list item.

Property Constant Style
"list-item-style" ListItemStyle Unselected element style
"current-style" CurrentStyle The style of the selected item. ListView in focus
"current-inactive-style" CurrentInactiveStyle The style of the selected item. ListView is out of focus

"checkbox", "checked", "checkbox-horizontal-align", and "checkbox-vertical-align" properties

The "current" property allows you to select one item in the list.The "checkbox" properties allow you to add a checkbox to each item in the list with whichyou can select several items in the list. The "checkbox" int property (ItemCheckbox constant)can take the following values

Value Constant Name Checkbox view
0 NoneCheckbox "none" There is no checkbox. Default value
1 SingleCheckbox "single" ◉ A checkbox that allows you to mark only one item
2 MultipleCheckbox "multiple" ☑ A checkbox that allows you to mark several items

You can get the value of this property using the function

func GetListViewCheckbox(view View, subviewID ...string) int

You can get/set the list of checked items using the "checked" property (Checked constant).This property is of type []int and stores the indexes of the marked elements.You can get the value of this property using the function

func GetListViewCheckedItems(view View, subviewID ...string) []int

You can check if a specific element is marked using the function

func IsListViewCheckedItem(view View, subviewID string, index int) bool

By default, the checkbox is located in the upper left corner of the element.You can change its position using int properties "checkbox-horizontal-align" and"checkbox-vertical-align" (CheckboxHorizontalAlign and CheckboxVerticalAlign constants)

The "checkbox-horizontal-align" int property can take the following values:

Value Constant Name Checkbox location
0 LeftAlign "left" At the left edge. Content on the right
1 RightAlign "right" At the right edge. Content on the left
2 CenterAlign "center" Center horizontally. Content below or above

The "checkbox-vertical-align" int property can take the following values:

Value Constant Name Checkbox location
0 TopAlign "top" Top alignment
1 BottomAlign "bottom" Bottom alignment
2 CenterAlign "center" Center alignment

Special case where both "checkbox-horizontal-align" and "checkbox-vertical-align" are CenterAlign (2).In this case, the checkbox is centered horizontally, the content is below

You can get property values for "checkbox-horizontal-align" and "checkbox-vertical-align" using the functions

func GetListViewCheckboxHorizontalAlign(view View, subviewID ...string) int
func GetListViewCheckboxVerticalAlign(view View, subviewID ...string) int

ListView events

There are three specific events for ListView

  • "list-item-clicked" (ListItemClickedEvent constant) event occurs when the user clicks on a list item.The main listener for this event has the following format: func(ListView, int).Where the second argument is the index of the element.
  • "list-item-selected" (ListItemSelectedEvent constant) event occurs when the user selects a list item.The main listener for this event has the following format: func(ListView, int).Where the second argument is the index of the element.
  • "list-item-checked" (ListItemCheckedEvent constant) event occurs when the user checks/unchecks the checkbox of a list item.The main listener for this event has the following format: func(ListView, []int).Where the second argument is an array of indexes of the tagged items.

You can get lists of listeners for these events using the functions:

func GetListItemClickedListeners(view View, subviewID ...string) []func(ListView, int)
func GetListItemSelectedListeners(view View, subviewID ...string) []func(ListView, int)
func GetListItemCheckedListeners(view View, subviewID ...string) []func(ListView, []int)