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

ListItem

abstract class ListItem<VH : ListItem.ViewHolder!>
kotlin.Any
   ↳ androidx.car.widget.ListItem

Definition of items that can be inserted into ListItemAdapter.

Summary

Nested classes

abstract

Functional interface to provide a way to interact with views in ViewHolder.

abstract

ViewHolder that supports ViewBinder.

Public constructors

Definition of items that can be inserted into ListItemAdapter.

Public methods

Unit

Same as addViewBinder(ViewBinder, ViewBinder) when cleanUp ViewBinder is null.

Unit
addViewBinder(binder: ListItem.ViewBinder<VH>!, @Nullable cleanUp: ListItem.ViewBinder<VH>?)

Adds ViewBinder to interact with sub-views in ViewHolder.

open Boolean

Returns whether or not the divider that comes after this ListItem should be shown.

abstract Int

Classes that extends ListItem should register its view type in ListItemAdapter#registerListItemViewType(int, int, Function).

open Boolean

Removes the first occurrence of the specified item.

abstract Unit
setEnabled(enabled: Boolean)

Sets the enabled state of the bound ViewHolder.

open Unit
setShowDivider(showDivider: Boolean)

Whether to show the item divider coming after this ListItem.

Protected methods

open Boolean

open Unit

Marks this item as not dirty.

open Unit

Marks this item as dirty so resolveDirtyState() is required in next bind() call.

abstract Unit
onBind(viewHolder: VH)

Binds this ListItem to viewHolder by applying data in ListItem to sub-views.

abstract Unit

Does the work that moves the ListItem from dirty state to clean state, i.

Public constructors

<init>

ListItem()

Definition of items that can be inserted into ListItemAdapter.

Public methods

addViewBinder

fun addViewBinder(binder: ListItem.ViewBinder<VH>!): Unit

Same as addViewBinder(ViewBinder, ViewBinder) when cleanUp ViewBinder is null.

Parameters
binder ListItem.ViewBinder<VH>!: to interact with subviews in ViewHolder.

addViewBinder

fun addViewBinder(binder: ListItem.ViewBinder<VH>!, @Nullable cleanUp: ListItem.ViewBinder<VH>?): Unit

Adds ViewBinder to interact with sub-views in ViewHolder. These ViewBinders will always be applied after onBind(ViewHolder).

To interact with a foobar sub-view in ViewHolder, make sure to first set its visibility, or call setFoobar() setter method.

Example:

<code>TextListItem item = new TextListItem(context);
      item.setTitle("title");
      item.addViewBinder((viewHolder) -&gt; {
          viewHolder.getTitle().doFoobar();
      }, (viewHolder) -&gt; {
          viewHolder.getTitle().revertFoobar();
      });
      </code>
Parameters
binder ListItem.ViewBinder<VH>!: to interact with subviews in ViewHolder.
cleanUp ListItem.ViewBinder<VH>!: view binder to revert the effect of binder. cleanUp binders will be stored in ListItem.ViewHolder and should be invoked via ViewHolder#cleanUp() before ViewHolder is recycled. This is to avoid changed made to ViewHolder lingers around when ViewHolder is recycled. Pass in null to skip.

getShowDivider

open fun getShowDivider(): Boolean

Returns whether or not the divider that comes after this ListItem should be shown.

Return
Boolean: true if the divider should be shown. Defaults to true.

getViewType

abstract fun getViewType(): Int

Classes that extends ListItem should register its view type in ListItemAdapter#registerListItemViewType(int, int, Function).

Return
Int: type of this ListItem.

removeViewBinder

open fun removeViewBinder(binder: ListItem.ViewBinder<VH>!): Boolean

Removes the first occurrence of the specified item.

Parameters
binder ListItem.ViewBinder<VH>!: to be removed.
Return
Boolean: true if binder exists. false otherwise.

setEnabled

abstract fun setEnabled(enabled: Boolean): Unit

Sets the enabled state of the bound 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.

setShowDivider

open fun setShowDivider(showDivider: Boolean): Unit

Whether to show the item divider coming after this ListItem.

Note: For this to work, one must invoke PagedListView.setDividerVisibilityManager(adapter for ListItemAdapter and have dividers enabled on PagedListView.

Protected methods

isDirty

protected open fun isDirty(): Boolean
Return
Boolean: true if next bind() should call resolveDirtyState().

markClean

protected open fun markClean(): Unit

Marks this item as not dirty. No need to call resolveDirtyState() in next bind().

markDirty

protected open fun markDirty(): Unit

Marks this item as dirty so resolveDirtyState() is required in next bind() call.

This method should be called in each setter.

onBind

protected abstract fun onBind(viewHolder: VH): Unit

Binds this ListItem to viewHolder by applying data in ListItem to sub-views. Assume ViewHolder#cleanUp() has already been invoked.

resolveDirtyState

protected abstract fun resolveDirtyState(): Unit

Does the work that moves the ListItem from dirty state to clean state, i.e. the work required the first time this ListItem binds to ListItem.ViewHolder. This method will transition ListItem to clean state. ListItem in clean state should move to dirty state when it is modified by calling markDirty().