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

CarDrawerAdapter

public abstract class CarDrawerAdapter
extends Adapter<DrawerItemViewHolder> implements PagedListView.ItemCap, DrawerItemClickListener

java.lang.Object
   ↳ androidx.recyclerview.widget.RecyclerView.Adapter<androidx.car.drawer.DrawerItemViewHolder>
     ↳ androidx.car.drawer.CarDrawerAdapter


Base adapter for displaying items in the car navigation drawer, which uses a PagedListView.

Subclasses can optionally set the title that will be displayed when displaying the contents of the drawer via setTitle(CharSequence). The title can be updated at any point later on. The title of the root adapter will also be the main title showed in the toolbar when the drawer is closed. See CarDrawerController.setRootAdapter(CarDrawerAdapter) for more information.

This class also takes care of implementing the PageListView.ItemCamp contract and subclasses should implement getActualItemCount().

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

Summary

Inherited constants

Protected constructors

CarDrawerAdapter(Context context, boolean showDisabledListOnEmpty)

Public methods

void cleanup()

Called when this adapter has been popped off the stack and is no longer needed.

final int getItemCount()

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

final int getItemViewType(int position)

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

final void onBindViewHolder(DrawerItemViewHolder holder, int position)

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

final DrawerItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType)

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

final void setMaxItems(int maxItems)

Sets the maximum number of items available in the adapter.

final void setTitle(CharSequence title)

Updates the title to display in the toolbar for this Adapter.

void start()

Enables support for CarUxRestrictions.

void stop()

Disables support for CarUxRestrictions, and frees up resources.

Protected methods

abstract int getActualItemCount()

Returns the absolute number of items that can be displayed in the list.

abstract void populateViewHolder(DrawerItemViewHolder holder, int position)

Subclasses should set all elements in holder to populate the drawer-item.

boolean usesSmallLayout(int position)

Used to indicate the layout used for the Drawer item at given position.

Inherited methods

Protected constructors

CarDrawerAdapter

protected CarDrawerAdapter (Context context, 
                boolean showDisabledListOnEmpty)

Parameters
context Context

showDisabledListOnEmpty boolean

Public methods

cleanup

public void cleanup ()

Called when this adapter has been popped off the stack and is no longer needed. Subclasses can override to do any necessary cleanup.

getItemCount

public final 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

public final 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.

onBindViewHolder

public final void onBindViewHolder (DrawerItemViewHolder 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 DrawerItemViewHolder: 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

public final DrawerItemViewHolder 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
DrawerItemViewHolder A new ViewHolder that holds a View of the given view type.

setMaxItems

public final 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

setTitle

public final void setTitle (CharSequence title)

Updates the title to display in the toolbar for this Adapter.

Parameters
title CharSequence

start

public 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

public 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.

Protected methods

getActualItemCount

protected abstract int getActualItemCount ()

Returns the absolute number of items that can be displayed in the list.

A class should implement this method to supply the number of items to be displayed. Returning 0 from this method will cause an empty list icon to be displayed in the drawer.

A class should override this method rather than getItemCount() because that method is handling the logic of when to display the empty list icon. It will return 1 when getActualItemCount() returns 0.

Returns
int The number of items to be displayed in the list.

populateViewHolder

protected abstract void populateViewHolder (DrawerItemViewHolder holder, 
                int position)

Subclasses should set all elements in holder to populate the drawer-item. If some element is not used, it should be nulled out since these ViewHolder/View's are recycled.

Parameters
holder DrawerItemViewHolder

position int

usesSmallLayout

protected boolean usesSmallLayout (int position)

Used to indicate the layout used for the Drawer item at given position. Subclasses can override this to use normal layout which includes text element below title.

A small layout is presented by the layout R.layout.car_drawer_list_item_small. Otherwise, the layout R.layout.car_drawer_list_item_normal will be used.

Parameters
position int: Adapter position of item.

Returns
boolean Whether the item at this position will use a small layout (default) or normal layout.