public abstract class AdapterModule extends Object
Adapter. AdapterModule also specifies API supporting state saving
via saveInstanceState() and its restoring via restoreInstanceState(Parcelable)
and also allows its inheritance hierarchies to notify the adapter to which are these modules
attached via notifyAdapter().
public class SampleModule extends AdapterModule {
// ...
@NonNull
@Override
public Parcelable saveInstanceState() {
final SavedState state = new SavedState(super.onSaveInstanceState());
// ...
// Pass here all data of this module which need to be saved to the state.
// ...
return state;
}
@Override
public void restoreInstanceState(@NonNull Parcelable savedState) {
if (!(savedState instanceof SavedState)) {
// Passed savedState is not our state, let super to process it.
super.restoreInstanceState(savedState);
return;
}
final SavedState state = (SavedState) savedState;
// Pass super state to super to process it.
super.restoreInstanceState(savedState.getSuperState());
// ...
// Set here all data of this module which need to be restored from the savedState.
// ...
}
// ...
// Implementation of AdapterSavedState for this module.
public static class SavedState extends AdapterSavedState {
// Each implementation of saved state need to have its own CREATOR provided.
public static final Creator<SavedState> CREATOR = new Creator<SavedState> {
@Override
public SavedState createFromParcel(@NonNull Parcel source) {
return new SavedState(source);
}
@Override
public SavedState[] newArray(int size) {
return new SavedState[size];
}
}
// Constructor used to chain the state of inheritance hierarchies.
SavedState(@NonNull Parcelable superState) {
super(superState);
}
SavedState(@NonNull Parcel source) {
super(source);
// Restore here state's data.
}
@Override
public void writeToParcel(@NonNull Parcel dest, int flags) {
super.writeToParcel(dest, flags);
// Save here state's data.
}
}
}
| Modifier and Type | Class and Description |
|---|---|
static interface |
AdapterModule.ModuleAdapter
Simple interface for "module based" adapter.
|
| Constructor and Description |
|---|
AdapterModule() |
| Modifier and Type | Method and Description |
|---|---|
protected void |
assertAttachedToAdapterOrThrow()
Asserts that this module is attached to its associated adapter, if not an exception is thrown.
|
void |
attachToAdapter(AdapterModule.ModuleAdapter adapter)
Called to attach this module to the given adapter.
|
boolean |
isAdapterNotificationEnabled()
Returns a boolean flag indicating whether the adapter to which is this module attached should
be notified in case, when its data set has been changed due to changes made by this module.
|
protected void |
notifyAdapter()
Notifies the adapter to which is this module attached via
AdapterModule.ModuleAdapter.notifyDataSetChanged(). |
protected void |
onAttachedToAdapter(AdapterModule.ModuleAdapter adapter)
Invoked immediately after
attachToAdapter(ModuleAdapter). |
boolean |
requiresStateSaving()
Returns a boolean flag indicating whether this module requires state saving or not.
|
void |
setAdapterNotificationEnabled(boolean enabled)
Sets a boolean flag indicating whether the adapter to which is this module attached should be
notified in case, when its data set has been changed due to changes made by this module.
|
public final void attachToAdapter(@NonNull AdapterModule.ModuleAdapter adapter)
adapter - Instance of the adapter by which will be this module used.protected void onAttachedToAdapter(@NonNull AdapterModule.ModuleAdapter adapter)
attachToAdapter(ModuleAdapter).adapter - Instance of the adapter to which was this module just attached.protected final void assertAttachedToAdapterOrThrow()
NullPointerException - If this module is not attached to any adapter.attachToAdapter(ModuleAdapter)public void setAdapterNotificationEnabled(boolean enabled)
Default value: true
enabled - True to enable adapter auto-notification, false to disable it.isAdapterNotificationEnabled()public boolean isAdapterNotificationEnabled()
True when adapter auto-notification is enabled, false otherwise.setAdapterNotificationEnabled(boolean)protected final void notifyAdapter()
AdapterModule.ModuleAdapter.notifyDataSetChanged().
This should be called whenever the data set of the attached adapter should be reloaded due to changes made by this module.
This method does nothing if adapter's notifications are disabled for this module.
isAdapterNotificationEnabled()public boolean requiresStateSaving()
This implementation by default returns false.
True to call saveInstanceState() upon this module to save its current
state and restore it via restoreInstanceState(Parcelable) later, false otherwise.