belongs to Maven artifact androidx.car:car:1.0.0-alpha1

ListItemAdapter

public class ListItemAdapter
extends Adapter<ListItem.ViewHolder> implements PagedListView.ItemCap, PagedListView.DividerVisibilityManager

java.lang.Object
   ↳ androidx.recyclerview.widget.RecyclerView.Adapter<androidx.car.widget.ListItem.ViewHolder>
     ↳ androidx.car.widget.ListItemAdapter


Adapter for PagedListView to display ListItem.

To enable support for CarUxRestrictions, call start() in your Activity's Activity.onCreate(Bundle), and stop() in Activity.onStop().

Summary

Nested classes

class ListItemAdapter.BackgroundStyle

Constant class for background style of items. 

Inherited constants

Public constructors

ListItemAdapter(Context context, ListItemProvider itemProvider)

Defaults ListItemAdapter.BackgroundStyle to ListItemAdapter.BackgroundStyle.SOLID.

ListItemAdapter(Context context, ListItemProvider itemProvider, int backgroundStyle)

Public methods

int getItemCount()

Returns the total number of items in the data set held by the adapter.

int getItemViewType(int position)

Return the view type of the item at position for the purposes of view recycling.

void onAttachedToRecyclerView(RecyclerView recyclerView)

Called by RecyclerView when it starts observing this Adapter.

void onBindViewHolder(ListItem.ViewHolder holder, int position)

Called by RecyclerView to display the data at the specified position.

ListItem.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)

Called when RecyclerView needs a new RecyclerView.ViewHolder of the given type to represent an item.

void registerListItemViewType(int viewType, int layoutResId, Function<ViewListItem.ViewHolder> function)

Registers a function that returns RecyclerView.ViewHolder for its matching view type returned by ListItem.getViewType().

void setMaxItems(int maxItems)

Sets the maximum number of items available in the adapter.

boolean shouldHideDivider(int position)

Given an item position, returns whether the divider below that item should be hidden.

void start()

Enables support for CarUxRestrictions.

void stop()

Disables support for CarUxRestrictions, and frees up resources.

Inherited methods

Public constructors

ListItemAdapter

ListItemAdapter (Context context, 
                ListItemProvider itemProvider)

Defaults ListItemAdapter.BackgroundStyle to ListItemAdapter.BackgroundStyle.SOLID.

Parameters
context Context

itemProvider ListItemProvider

ListItemAdapter

ListItemAdapter (Context context, 
                ListItemProvider itemProvider, 
                int backgroundStyle)

Parameters
context Context

itemProvider ListItemProvider

backgroundStyle int

Public methods

getItemCount

int getItemCount ()

Returns the total number of items in the data set held by the adapter.

Returns
int The total number of items in this adapter.

getItemViewType

int getItemViewType (int position)

Return the view type of the item at position for the purposes of view recycling.

The default implementation of this method returns 0, making the assumption of a single view type for the adapter. Unlike ListView adapters, types need not be contiguous. Consider using id resources to uniquely identify item view types.

Parameters
position int: position to query

Returns
int integer value identifying the type of the view needed to represent the item at position. Type codes need not be contiguous.

onAttachedToRecyclerView

void onAttachedToRecyclerView (RecyclerView recyclerView)

Called by RecyclerView when it starts observing this Adapter.

Keep in mind that same adapter may be observed by multiple RecyclerViews.

Parameters
recyclerView RecyclerView: The RecyclerView instance which started observing this adapter.

onBindViewHolder

void onBindViewHolder (ListItem.ViewHolder holder, 
                int position)

Called by RecyclerView to display the data at the specified position. This method should update the contents of the RecyclerView.ViewHolder.itemView to reflect the item at the given position.

Note that unlike ListView, RecyclerView will not call this method again if the position of the item changes in the data set unless the item itself is invalidated or the new position cannot be determined. For this reason, you should only use the position parameter while acquiring the related data item inside this method and should not keep a copy of it. If you need the position of an item later on (e.g. in a click listener), use RecyclerView.ViewHolder.getAdapterPosition() which will have the updated adapter position. Override onBindViewHolder(ViewHolder, int, List) instead if Adapter can handle efficient partial bind.

Parameters
holder ListItem.ViewHolder: The ViewHolder which should be updated to represent the contents of the item at the given position in the data set.

position int: The position of the item within the adapter's data set.

onCreateViewHolder

ListItem.ViewHolder onCreateViewHolder (ViewGroup parent, 
                int viewType)

Called when RecyclerView needs a new RecyclerView.ViewHolder of the given type to represent an item.

This new ViewHolder should be constructed with a new View that can represent the items of the given type. You can either create a new View manually or inflate it from an XML layout file.

The new ViewHolder will be used to display items of the adapter using onBindViewHolder(ViewHolder, int, List). Since it will be re-used to display different items in the data set, it is a good idea to cache references to sub views of the View to avoid unnecessary View.findViewById(int) calls.

Parameters
parent ViewGroup: The ViewGroup into which the new View will be added after it is bound to an adapter position.

viewType int: The view type of the new View.

Returns
ListItem.ViewHolder A new ViewHolder that holds a View of the given view type.

registerListItemViewType

void registerListItemViewType (int viewType, 
                int layoutResId, 
                Function<ViewListItem.ViewHolder> function)

Registers a function that returns RecyclerView.ViewHolder for its matching view type returned by ListItem.getViewType().

The function will receive a view as RecyclerView.ViewHolder.itemView. This view uses background defined by ListItemAdapter.BackgroundStyle.

Subclasses of ListItem in package androidx.car.widget are already registered.

Parameters
viewType int: use negative value for custom view type.

layoutResId int

function Function: function to create ViewHolder for viewType.

setMaxItems

void setMaxItems (int maxItems)

Sets the maximum number of items available in the adapter. A value less than '0' means the list should not be capped.

Parameters
maxItems int

shouldHideDivider

boolean shouldHideDivider (int position)

Given an item position, returns whether the divider below that item should be hidden.

Parameters
position int: item position inside the adapter.

Returns
boolean true if divider is to be hidden, false if divider should be shown.

start

void start ()

Enables support for CarUxRestrictions.

This method can be called from Activity's Activity.onStart(), or at the time of construction.

This method must be accompanied with a matching stop() to avoid leak.

stop

void stop ()

Disables support for CarUxRestrictions, and frees up resources.

This method should be called from Activity's Activity.onStop(), or at the time of this adapter being discarded.