T
- type of RealmModel
stored in the adapter.S
- type of RecyclerView.ViewHolder used in the adapter.public abstract class RealmRecyclerViewAdapter<T extends io.realm.RealmModel,S extends android.support.v7.widget.RecyclerView.ViewHolder>
extends android.support.v7.widget.RecyclerView.Adapter<S>
This adapter will automatically handle any updates to its data and call notifyDataSetChanged()
,
notifyItemInserted()
, notifyItemRemoved()
or notifyItemRangeChanged()
as appropriate.
The RealmAdapter will stop receiving updates if the Realm instance providing the OrderedRealmCollection
is
closed.
If the adapter contains Realm model classes with a primary key that is either an int
or a long
, call
setHasStableIds(true)
in the constructor and override RecyclerView.Adapter.getItemId(int)
as described by the Javadoc in that method.
RecyclerView.Adapter#setHasStableIds(boolean)
,
RecyclerView.Adapter#getItemId(int)
Constructor and Description |
---|
RealmRecyclerViewAdapter(io.realm.OrderedRealmCollection<T> data,
boolean autoUpdate)
This is equivalent to
RealmRecyclerViewAdapter(data, autoUpdate, true) . |
RealmRecyclerViewAdapter(io.realm.OrderedRealmCollection<T> data,
boolean autoUpdate,
boolean updateOnModification) |
Modifier and Type | Method and Description |
---|---|
io.realm.OrderedRealmCollection<T> |
getData()
Returns data associated with this adapter.
|
T |
getItem(int index)
Returns the item in the underlying data associated with the specified position.
|
int |
getItemCount() |
void |
onAttachedToRecyclerView(android.support.v7.widget.RecyclerView recyclerView) |
void |
onDetachedFromRecyclerView(android.support.v7.widget.RecyclerView recyclerView) |
void |
updateData(io.realm.OrderedRealmCollection<T> data)
Updates the data associated to the Adapter.
|
bindViewHolder, createViewHolder, getItemId, getItemViewType, hasObservers, hasStableIds, notifyDataSetChanged, notifyItemChanged, notifyItemChanged, notifyItemInserted, notifyItemMoved, notifyItemRangeChanged, notifyItemRangeChanged, notifyItemRangeInserted, notifyItemRangeRemoved, notifyItemRemoved, onBindViewHolder, onBindViewHolder, onCreateViewHolder, onFailedToRecycleView, onViewAttachedToWindow, onViewDetachedFromWindow, onViewRecycled, registerAdapterDataObserver, setHasStableIds, unregisterAdapterDataObserver
public RealmRecyclerViewAdapter(@Nullable io.realm.OrderedRealmCollection<T> data, boolean autoUpdate)
RealmRecyclerViewAdapter(data, autoUpdate, true)
.public RealmRecyclerViewAdapter(@Nullable io.realm.OrderedRealmCollection<T> data, boolean autoUpdate, boolean updateOnModification)
data
- collection data to be used by this adapter.autoUpdate
- when it is false
, the adapter won't be automatically updated when collection data
changes.updateOnModification
- when it is true
, this adapter will be updated when deletions, insertions or
modifications happen to the collection data. When it is false
, only
deletions and insertions will trigger the updates. This param will be ignored if
autoUpdate
is false
.public void onAttachedToRecyclerView(android.support.v7.widget.RecyclerView recyclerView)
onAttachedToRecyclerView
in class android.support.v7.widget.RecyclerView.Adapter<S extends android.support.v7.widget.RecyclerView.ViewHolder>
public void onDetachedFromRecyclerView(android.support.v7.widget.RecyclerView recyclerView)
onDetachedFromRecyclerView
in class android.support.v7.widget.RecyclerView.Adapter<S extends android.support.v7.widget.RecyclerView.ViewHolder>
public int getItemCount()
getItemCount
in class android.support.v7.widget.RecyclerView.Adapter<S extends android.support.v7.widget.RecyclerView.ViewHolder>
@Nullable public T getItem(int index)
null
if the Realm instance has been closed or the index
is outside the range of valid adapter data (which e.g. can happen if getItemCount()
is modified to account for header or footer views.
Also, this method does not take into account any header views. If these are present, modify
the index
parameter accordingly first.index
- index of the item in the original collection backing this adapter.null
if the position does not exists or
the adapter data are no longer valid.@Nullable public io.realm.OrderedRealmCollection<T> getData()
public void updateData(@Nullable io.realm.OrderedRealmCollection<T> data)
data
- the new OrderedRealmCollection
to display.