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.
- Implements
PagedListView.ItemCap- defaults to unlimited item count. - Implements
PagedListView.DividerVisibilityManager- to control dividers after individualListItem.
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(Context context, ListItemProvider itemProvider, int backgroundStyle)
|
|
Public methods | |
|---|---|
Context
|
getContext()
|
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 |
boolean
|
getShowDivider(int position)
Given an item position, returns whether the divider below that item should be shown. |
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 |
void
|
registerListItemViewType(int viewType, int layoutResId, Function<View, ListItem.ViewHolder> function)
Registers a custom |
void
|
setMaxItems(int maxItems)
Sets the maximum number of items available in the adapter. |
void
|
start()
Enables support for |
void
|
stop()
Disables support for |
Inherited methods | |
|---|---|
Public constructors
ListItemAdapter
public ListItemAdapter (Context context, ListItemProvider itemProvider)
Defaults ListItemAdapter.BackgroundStyle to ListItemAdapter.BackgroundStyle.NONE.
| Parameters | |
|---|---|
context |
Context |
itemProvider |
ListItemProvider |
ListItemAdapter
public ListItemAdapter (Context context, ListItemProvider itemProvider, int backgroundStyle)
| Parameters | |
|---|---|
context |
Context |
itemProvider |
ListItemProvider |
backgroundStyle |
int |
Public methods
getItemCount
public 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 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.
|
getShowDivider
public boolean getShowDivider (int position)
Given an item position, returns whether the divider below that item should be shown.
| Parameters | |
|---|---|
position |
int: item position inside the adapter. |
| Returns | |
|---|---|
boolean |
true if divider is to be shown; false if hidden.
|
onAttachedToRecyclerView
public 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
public 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
public 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
public void registerListItemViewType (int viewType,
int layoutResId,
Function<View, ListItem.ViewHolder> function)
Registers a custom ListItem that this adapter will handle. The custom list item will
be identified by the unique view id that is passed to this method. The function
should a reference to the method that will create the ViewHolder that houses the
custom ListItem.
int viewType = -1;
registerListItemViewType(
viewType,
R.layout.custom_view_layout,
CustomListItem::createViewHolder);
The function will receive a view as RecyclerView.ViewHolder.itemView. This view
uses a background defined by ListItemAdapter.BackgroundStyle.
Subclasses of ListItem in package androidx.car.widget are already
registered.
| Parameters | |
|---|---|
viewType |
int: A unique id for the custom view. Use negative values for custom view type.Value is between -2147483648 and -1 inclusive. |
layoutResId |
int: The layout structure that will bs used for the custom view type. |
function |
Function: function to create ViewHolder for viewType.
|
setMaxItems
public 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 |
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.
Content and code samples on this page are subject to the licenses described in the Content License. Java is a registered trademark of Oracle and/or its affiliates.