Register now for Android Dev Summit 2019!

SwitchListItem

open class SwitchListItem : ListItem<SwitchListItem.ViewHolder!>
kotlin.Any
   ↳ androidx.car.widget.ListItem<androidx.car.widget.SwitchListItem.ViewHolder>
   ↳ androidx.car.widget.SwitchListItem

Class to build a list item with Switch.

An item supports primary action and a switch as supplemental action.

An item visually composes of 3 parts; each part may contain multiple views.

  • Primary Action: represented by an icon of following types.
    • Primary Icon - icon size could be large or small.
    • No Icon - no icon is shown.
    • Empty Icon - Text offsets start space as if there was an icon.
  • Text: supports any combination of the following text views.
    • Title
    • Body
  • Supplemental Action: represented by Switch.

SwitchListItem binds data to ViewHolder based on components selected.

When conflicting setter methods are called (e.g. setting primary action to both primary icon and no icon), the last called method wins.

Summary

Nested classes

Holds views of SwitchListItem.

Constants

static Int

Large sized icon is as tall as a list item with only title text.

static Int

Medium sized icon is slightly bigger than SMALL ones.

static Int

Small sized icon is the mostly commonly used size.

Public constructors

<init>(@NonNull context: Context)

Public methods

open static SwitchListItem.ViewHolder
createViewHolder(itemView: View!)

Creates a ViewHolder.

open Int

Used by ListItemAdapter to choose layout to inflate for view holder.

open Unit

Hides all views in ViewHolder then applies ViewBinders to adjust view layout params.

open Unit
setBody(@Nullable body: CharSequence?)

Sets the body text of item.

open Unit
setClickable(isClickable: Boolean)

Sets whether the item is clickable.

open Unit
setEnabled(enabled: Boolean)

open Unit

Sets Primary Action to be empty icon.

open Unit
setPrimaryActionIcon(@NonNull icon: Icon, size: Int)

Sets Primary Action to be represented by an icon.

open Unit

Sets Primary Action to have no icon.

open Unit
setShowSwitchDivider(showSwitchDivider: Boolean)

Sets whether to display a vertical bar between switch and text.

open Unit

Registers a callback to be invoked when the checked state of switch changes.

open Unit
setSwitchState(isChecked: Boolean)

Sets the state of Switch.

open Unit
setTitle(@Nullable title: CharSequence?)

Sets the title of item.

Protected methods

Context

open Unit

Calculates the layout params for views in ViewHolder.

Inherited functions

Constants

PRIMARY_ACTION_ICON_SIZE_LARGE

static val PRIMARY_ACTION_ICON_SIZE_LARGE: Int

Large sized icon is as tall as a list item with only title text. It is intended for album art.

Value: 2

PRIMARY_ACTION_ICON_SIZE_MEDIUM

static val PRIMARY_ACTION_ICON_SIZE_MEDIUM: Int

Medium sized icon is slightly bigger than SMALL ones. It is intended for profile pictures (avatar), in which case caller is responsible for passing in a circular image.

Value: 1

PRIMARY_ACTION_ICON_SIZE_SMALL

static val PRIMARY_ACTION_ICON_SIZE_SMALL: Int

Small sized icon is the mostly commonly used size. It's the same as supplemental action icon.

Value: 0

Public constructors

<init>

SwitchListItem(@NonNull context: Context)

Public methods

createViewHolder

@NonNull open static fun createViewHolder(itemView: View!): SwitchListItem.ViewHolder

Creates a ViewHolder.

getViewType

open fun getViewType(): Int

Used by ListItemAdapter to choose layout to inflate for view holder.

Return
Int: type of this ListItem.

onBind

open fun onBind(viewHolder: SwitchListItem.ViewHolder!): Unit

Hides all views in ViewHolder then applies ViewBinders to adjust view layout params.

setBody

open fun setBody(@Nullable body: CharSequence?): Unit

Sets the body text of item.

Text beyond length required by regulation will be truncated.

Parameters
body CharSequence?: text to be displayed.

setClickable

open fun setClickable(isClickable: Boolean): Unit

Sets whether the item is clickable. If true, clicking item toggles the switch.

setEnabled

open fun setEnabled(enabled: Boolean): Unit

setPrimaryActionEmptyIcon

open fun setPrimaryActionEmptyIcon(): Unit

Sets Primary Action to be empty icon.

Text would have a start margin as if Primary Action were set to primary icon.

setPrimaryActionIcon

open fun setPrimaryActionIcon(@NonNull icon: Icon, size: Int): Unit

Sets Primary Action to be represented by an icon.

Parameters
icon Icon: An icon to set as primary action.
size Icon: small/medium/large. Available as PRIMARY_ACTION_ICON_SIZE_SMALL, PRIMARY_ACTION_ICON_SIZE_MEDIUM, PRIMARY_ACTION_ICON_SIZE_LARGE.

setPrimaryActionNoIcon

open fun setPrimaryActionNoIcon(): Unit

Sets Primary Action to have no icon. Text would align to the start of item.

setShowSwitchDivider

open fun setShowSwitchDivider(showSwitchDivider: Boolean): Unit

Sets whether to display a vertical bar between switch and text.

setSwitchOnCheckedChangeListener

open fun setSwitchOnCheckedChangeListener(@Nullable listener: OnCheckedChangeListener?): Unit

Registers a callback to be invoked when the checked state of switch changes.

Parameters
listener OnCheckedChangeListener?: callback to be invoked when the checked state shown in the UI changes.

setSwitchState

open fun setSwitchState(isChecked: Boolean): Unit

Sets the state of Switch.

Parameters
isChecked Boolean: sets the "checked/unchecked, namely on/off" state of switch.

setTitle

open fun setTitle(@Nullable title: CharSequence?): Unit

Sets the title of item.

Primary text is Title by default. It can be set by setBody(CharSequence)

Title text is limited to one line, and ellipses at the end.

Parameters
title CharSequence?: text to display as title.

Protected methods

getContext

@NonNull protected fun getContext(): Context

resolveDirtyState

@CallSuper protected open fun resolveDirtyState(): Unit

Calculates the layout params for views in ViewHolder.