The Android Developer Challenge is back! Submit your idea before December 2.

SwitchListItem

public class SwitchListItem
extends ListItem<SwitchListItem.ViewHolder>

java.lang.Object
   ↳ 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 SwitchListItem.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

class SwitchListItem.ViewHolder

Holds views of SwitchListItem. 

Constants

int PRIMARY_ACTION_ICON_SIZE_LARGE

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

int PRIMARY_ACTION_ICON_SIZE_MEDIUM

Medium sized icon is slightly bigger than SMALL ones.

int PRIMARY_ACTION_ICON_SIZE_SMALL

Small sized icon is the mostly commonly used size.

Public constructors

SwitchListItem(Context context)

Public methods

static SwitchListItem.ViewHolder createViewHolder(View itemView)

Creates a SwitchListItem.ViewHolder.

int getViewType()

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

void onBind(SwitchListItem.ViewHolder viewHolder)

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

void setBody(CharSequence body)

Sets the body text of item.

void setClickable(boolean isClickable)

Sets whether the item is clickable.

void setEnabled(boolean enabled)

Sets the enabled state of the bound ListItem.ViewHolder.

void setPrimaryActionEmptyIcon()

Sets Primary Action to be empty icon.

void setPrimaryActionIcon(Icon icon, int size)

Sets Primary Action to be represented by an icon.

void setPrimaryActionNoIcon()

Sets Primary Action to have no icon.

void setShowSwitchDivider(boolean showSwitchDivider)

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

void setSwitchOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener listener)

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

void setSwitchState(boolean isChecked)

Sets the state of Switch.

void setTitle(CharSequence title)

Sets the title of item.

Protected methods

final Context getContext()
void resolveDirtyState()

Calculates the layout params for views in SwitchListItem.ViewHolder.

Inherited methods

Constants

PRIMARY_ACTION_ICON_SIZE_LARGE

public static final int PRIMARY_ACTION_ICON_SIZE_LARGE

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

Constant Value: 2 (0x00000002)

PRIMARY_ACTION_ICON_SIZE_MEDIUM

public static final int PRIMARY_ACTION_ICON_SIZE_MEDIUM

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.

Constant Value: 1 (0x00000001)

PRIMARY_ACTION_ICON_SIZE_SMALL

public static final int PRIMARY_ACTION_ICON_SIZE_SMALL

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

Constant Value: 0 (0x00000000)

Public constructors

SwitchListItem

public SwitchListItem (Context context)

Parameters
context Context

Public methods

createViewHolder

public static SwitchListItem.ViewHolder createViewHolder (View itemView)

Creates a SwitchListItem.ViewHolder.

Parameters
itemView View

Returns
SwitchListItem.ViewHolder

getViewType

public int getViewType ()

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

Returns
int type of this ListItem.

onBind

public void onBind (SwitchListItem.ViewHolder viewHolder)

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

Parameters
viewHolder SwitchListItem.ViewHolder

setBody

public void setBody (CharSequence body)

Sets the body text of item.

Text beyond length required by regulation will be truncated.

Parameters
body CharSequence: text to be displayed.

setClickable

public void setClickable (boolean isClickable)

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

Parameters
isClickable boolean

setEnabled

public void setEnabled (boolean enabled)

Sets the enabled state of the bound ListItem.ViewHolder.

All visible children views of ViewHolder should be set to enabled. Caller is responsible for notifying ListItemAdapter about data change.

Disabled items are usually styled at 50% opacity. Consider similar styling for consistency.

Parameters
enabled boolean

setPrimaryActionEmptyIcon

public void setPrimaryActionEmptyIcon ()

Sets Primary Action to be empty icon.

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

setPrimaryActionIcon

public void setPrimaryActionIcon (Icon icon, 
                int size)

Sets Primary Action to be represented by an icon.

Parameters
icon Icon: An icon to set as primary action.

size int: small/medium/large. Available as PRIMARY_ACTION_ICON_SIZE_SMALL, PRIMARY_ACTION_ICON_SIZE_MEDIUM, PRIMARY_ACTION_ICON_SIZE_LARGE.

setPrimaryActionNoIcon

public void setPrimaryActionNoIcon ()

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

setShowSwitchDivider

public void setShowSwitchDivider (boolean showSwitchDivider)

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

Parameters
showSwitchDivider boolean

setSwitchOnCheckedChangeListener

public void setSwitchOnCheckedChangeListener (CompoundButton.OnCheckedChangeListener listener)

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

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

setSwitchState

public void setSwitchState (boolean isChecked)

Sets the state of Switch.

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

setTitle

public void setTitle (CharSequence title)

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

protected final Context getContext ()

Returns
Context

resolveDirtyState

protected void resolveDirtyState ()

Calculates the layout params for views in SwitchListItem.ViewHolder.