public abstract class RepositoryPresenter<T>
extends java.lang.Object
Repository.| Constructor and Description |
|---|
RepositoryPresenter() |
| Modifier and Type | Method and Description |
|---|---|
abstract void |
bind(T data,
int index,
android.support.v7.widget.RecyclerView.ViewHolder holder)
Binds the
index-th item to present the data into the item view held in the given
holder. |
abstract int |
getItemCount(T data)
Returns the number of adapter items needed to present the data.
|
long |
getItemId(T data,
int index)
Returns the stable ID for the
index-th item to present the data. |
abstract int |
getLayoutResId(T data,
int index)
Returns the layout resource ID to inflate the view for the
index-th item to present the
data. |
boolean |
getUpdates(T oldData,
T newData,
android.support.v7.util.ListUpdateCallback listUpdateCallback)
Produces a sequence of fine-grained events (addition, removal, changing and moving of
individual items) to the given
listUpdateCallback capturing the changes of data to be
presented by this RepositoryPresenter, in response to an update. |
void |
recycle(android.support.v7.widget.RecyclerView.ViewHolder holder)
Called when the given
holder is recycled. |
public abstract int getItemCount(@NonNull
T data)
public long getItemId(@NonNull
T data,
int index)
index-th item to present the data. Called only if stable
IDs are enabled with RepositoryAdapter.setHasStableIds(true), and therefore this method is optional with a default
implementation of returning RecyclerView.NO_ID. If stable IDs are enabled, this ID and
the item's layout resource ID should together uniquely identify
this item in the whole RecyclerView throughout all changes.index - The item index between 0 (incl.) and getItemCount(T) (excl.).@LayoutRes
public abstract int getLayoutResId(@NonNull
T data,
int index)
index-th item to present the
data.index - The item index between 0 (incl.) and getItemCount(T) (excl.).public abstract void bind(@NonNull
T data,
int index,
@NonNull
android.support.v7.widget.RecyclerView.ViewHolder holder)
index-th item to present the data into the item view held in the given
holder. The view is inflated from the layout resource specified by
getLayoutResId(T, int), but may have been previously bound to a different index, different
data, and/or with a different presenter. Therefore, implementation should take care of
resetting the view state.index - The item index between 0 (incl.) and getItemCount(T) (excl.).holder - The view holder that holds the view. If a subclass of RepositoryAdapter
is used, which returns a custom view holder for this item's layout resource ID, then this
object will be of that custom type.public void recycle(@NonNull
android.support.v7.widget.RecyclerView.ViewHolder holder)
holder is recycled.holder - The view holder that holds the view. If a subclass of RepositoryAdapter
is used, which returns a custom view holder for this item's layout resource ID, then this
object will be of that custom type.public boolean getUpdates(@NonNull
T oldData,
@NonNull
T newData,
@NonNull
android.support.v7.util.ListUpdateCallback listUpdateCallback)
listUpdateCallback capturing the changes of data to be
presented by this RepositoryPresenter, in response to an update. Implementation should
do either of the following:
While a RepositoryAdapter is in use (for
example, attached to a RecyclerView), when it receives an update from a presented
Repository, the associated RepositoryPresenter will be asked to produce a
sequence of fine-grained events capturing the update; when the RepositoryAdapter
receives an update from one of the additional
observables, then all RepositoryPresenters will be asked. If all affected
RepositoryPresenters produced fine-grained events, then RepositoryAdapter will
apply the new data and notify the RecyclerView of the aggregated sequence of events;
otherwise the adapter will call RecyclerView.Adapter.notifyDataSetChanged(), and pause all update
processing (both data and events) until fresh data is needed, hinted by the next call to
RepositoryAdapter.getItemCount().
Notes for implementation:
oldData is the previously
presented repository value, and newData is the value newly obtained from the
repository. The new value has not been applied and is not exposed through the adapter.
Note that repositories are allowed to dispatch updates while maintaining
oldData.equals(newData) or even oldData == newData, depending on the
repository implementation.
oldData == newData, because the value is not reloaded from the repository.
RepositoryPresenter may not have a chance to produce fine-grained events in
response to an update. This can happen when an earlier presenter returns false from this
method, or the adapter is not in use (for example,
not attached to any RecyclerView).
RepositoryAdapter guarantees that, if this method is not called, then
getItemCount(T) will be called before this RepositoryPresenter is asked to
present any item from the new data. getItemCount(T) may be called again when there is
a chance that the last data passed into the method may not be the latest.
RepositoryPresenter presenting a static item (added with RepositoryAdapter.Builder.addItem(T, com.google.android.agera.rvadapter.RepositoryPresenter<T>))
will not be able to produce any events for any item views presented for the static item.
oldData - The data previously presented by this RepositoryPresenter.newData - The data to be presented by this RepositoryPresenter following the
update.listUpdateCallback - A callback to record the sequence of fine-grained events to be
dispatched via the RepositoryAdapter to the RecyclerView. To be used within
this method call only. Positions and counts are relative to this presenter.listUpdateCallback during the execution of
this method has completely and accurately described all changes.