-
public abstract class EpoxyModel<T>Helper to bind data to a view using a builder style. The parameterized type should extend Android's View or EpoxyHolder.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description public interfaceEpoxyModel.AddPredicatepublic interfaceEpoxyModel.SpanSizeOverrideCallback
-
Field Summary
Fields Modifier and Type Field Description private intlayout
-
Constructor Summary
Constructors Constructor Description EpoxyModel()
-
Method Summary
Modifier and Type Method Description final intgetLayout()ViewbuildView(@NonNull() ViewGroup parent)Create and return a new instance of a view for this model. voidpreBind(@NonNull() T view, @Nullable() EpoxyModel<out Object> previouslyBoundModel)Hook that is called before bind. voidbind(@NonNull() T view)Binds the current data to the given view. voidbind(@NonNull() T view, @NonNull() List<Object> payloads)Similar to bind, but provides a non null, non empty list of payloadsdescribing what changed. voidbind(@NonNull() T view, @NonNull() EpoxyModel<out Object> previouslyBoundModel)Similar to bind, but provides a non null model which was previously bound tothis view. voidunbind(@NonNull() T view)Called when the view bound to this model is recycled. voidonVisibilityStateChanged(int visibilityState, @NonNull() T view)TODO link to the wiki voidonVisibilityChanged(@FloatRange(from = 0.0f, to = 100.0f) float percentVisibleHeight, @FloatRange(from = 0.0f, to = 100.0f) float percentVisibleWidth, @Px() int visibleHeight, @Px() int visibleWidth, @NonNull() T view)TODO link to the wiki longid()EpoxyModel<T>id(long id)Override the default id in cases where the data subject naturally has an id, like an objectfrom a database. EpoxyModel<T>id(@Nullable() Array<Number> ids)Use multiple numbers as the id for this model. EpoxyModel<T>id(long id1, long id2)Use two numbers as the id for this model. EpoxyModel<T>id(@Nullable() CharSequence key)Use a string as the model id. EpoxyModel<T>id(@Nullable() CharSequence key, @Nullable() Array<CharSequence> otherKeys)Use several strings to define the id of the model. EpoxyModel<T>id(@Nullable() CharSequence key, long id)Set an id that is namespaced with a string. EpoxyModel<T>layout(@LayoutRes() int layoutRes)EpoxyModel<T>reset()Sets fields of the model to default ones. voidaddTo(@NonNull() EpoxyController controller)Add this model to the given controller. voidaddIf(boolean condition, @NonNull() EpoxyController controller)Add this model to the given controller if the condition is true. voidaddIf(@NonNull() EpoxyModel.AddPredicate predicate, @NonNull() EpoxyController controller)Add this model to the given controller if the AddPredicate return true. booleanequals(Object o)inthashCode()intgetSpanSize(int totalSpanCount, int position, int itemCount)Subclasses can override this if they want their view to take up more than one span in a gridlayout. EpoxyModel<T>spanSizeOverride(@Nullable() EpoxyModel.SpanSizeOverrideCallback spanSizeCallback)final intspanSize(int totalSpanCount, int position, int itemCount)Returns the actual span size of this model, using the SpanSizeOverrideCallback if onewas set, otherwise using the value from getSpanSize EpoxyModel<T>show()Change the visibility of the model so that it's view is shown. EpoxyModel<T>show(boolean show)Change the visibility of the model's view. EpoxyModel<T>hide()Change the visibility of the model so that it's view is hidden. booleanisShown()Whether the model's view should be shown on screen. booleanshouldSaveViewState()Whether the adapter should save the state of the view bound to this model. booleanonFailedToRecycleView(@NonNull() T view)Called if the RecyclerView failed to recycle this model's view. voidonViewAttachedToWindow(@NonNull() T view)Called when this model's view is attached to the window. voidonViewDetachedFromWindow(@NonNull() T view)Called when this model's view is detached from the the window. StringtoString()-
-
Method Detail
-
buildView
View buildView(@NonNull() ViewGroup parent)
Create and return a new instance of a view for this model. By default a view is created byinflating the layout resource.
-
preBind
void preBind(@NonNull() T view, @Nullable() EpoxyModel<out Object> previouslyBoundModel)
Hook that is called before bind. This is similar to handlePreBind, but is intended forsubclasses of EpoxyModel to hook into rather than for generated code to hook into.Overriding preBind is useful to capture state before the view changes, e.g. for animations.
- Parameters:
previouslyBoundModel- This is a model with the same id that was previously bound.
-
bind
void bind(@NonNull() T view)
Binds the current data to the given view. You should bind all fields including unset/emptyfields to ensure proper recycling.
-
bind
void bind(@NonNull() T view, @NonNull() List<Object> payloads)
Similar to bind, but provides a non null, non empty list of payloadsdescribing what changed. This is the payloads list specified in the adapter's notifyItemChangedmethod. This is a useful optimization to allow you to only change part of a view instead ofupdating the whole thing, which may prevent unnecessary layout calls. If there are no payloadsthen bind is called instead. This will only be used if the model is used withan EpoxyAdapter
-
bind
void bind(@NonNull() T view, @NonNull() EpoxyModel<out Object> previouslyBoundModel)
Similar to bind, but provides a non null model which was previously bound tothis view. This will only be called if the model is used with an EpoxyController.
- Parameters:
previouslyBoundModel- This is a model with the same id that was previously bound.
-
unbind
void unbind(@NonNull() T view)
Called when the view bound to this model is recycled. Subclasses can override this if theirview should release resources when it's recycled.
Note that bind can be called multiple times without an unbind call in betweenif the view has remained on screen to be reused across item changes. This means that you shouldnot rely on unbind to clear a view or model's state before bind is called again.
-
onVisibilityStateChanged
void onVisibilityStateChanged(int visibilityState, @NonNull() T view)
TODO link to the wiki
-
onVisibilityChanged
void onVisibilityChanged(@FloatRange(from = 0.0f, to = 100.0f) float percentVisibleHeight, @FloatRange(from = 0.0f, to = 100.0f) float percentVisibleWidth, @Px() int visibleHeight, @Px() int visibleWidth, @NonNull() T view)
TODO link to the wiki
-
id
long id()
-
id
EpoxyModel<T> id(long id)
Override the default id in cases where the data subject naturally has an id, like an objectfrom a database. This id can only be set before the model is added to the adapter, it is anerror to change the id after that.
-
id
EpoxyModel<T> id(@Nullable() Array<Number> ids)
Use multiple numbers as the id for this model. Useful when you don't have a single long thatrepresents a unique id.
This hashes the numbers, so there is a tiny risk of collision with other ids.
-
id
EpoxyModel<T> id(long id1, long id2)
Use two numbers as the id for this model. Useful when you don't have a single long thatrepresents a unique id.
This hashes the two numbers, so there is a tiny risk of collision with other ids.
-
id
EpoxyModel<T> id(@Nullable() CharSequence key)
Use a string as the model id. Useful for models that don't clearly map to a numerical id. Thisis preferable to using hashCode because that is a 32 bit hash and this is a 64bit hash, giving better spread and less chance of collision with other ids.
Since this uses a hashcode method to convert the String to a long there is a very small chancethat you may have a collision with another id. Assuming an even spread of hashcodes, andseveral hundred models in the adapter, there would be roughly 1 in 100 trillion chance of acollision. (http://preshing.com/20110504/hash-collision-probabilities/)
-
id
EpoxyModel<T> id(@Nullable() CharSequence key, @Nullable() Array<CharSequence> otherKeys)
Use several strings to define the id of the model.
Similar to id, but with additional strings.
-
id
EpoxyModel<T> id(@Nullable() CharSequence key, long id)
Set an id that is namespaced with a string. This is useful when you need to show models ofmultiple types, side by side and don't want to risk id collisions.
Since this uses a hashcode method to convert the String to a long there is a very small chancethat you may have a collision with another id. Assuming an even spread of hashcodes, andseveral hundred models in the adapter, there would be roughly 1 in 100 trillion chance of acollision. (http://preshing.com/20110504/hash-collision-probabilities/)
-
layout
@NonNull() EpoxyModel<T> layout(@LayoutRes() int layoutRes)
-
reset
@NonNull() EpoxyModel<T> reset()
Sets fields of the model to default ones.
-
addTo
void addTo(@NonNull() EpoxyController controller)
Add this model to the given controller. Can only be called from inside .
-
addIf
void addIf(boolean condition, @NonNull() EpoxyController controller)
Add this model to the given controller if the condition is true. Can only be called from inside buildModels.
-
addIf
void addIf(@NonNull() EpoxyModel.AddPredicate predicate, @NonNull() EpoxyController controller)
Add this model to the given controller if the AddPredicate return true. Can only becalled from inside buildModels.
-
hashCode
int hashCode()
-
getSpanSize
int getSpanSize(int totalSpanCount, int position, int itemCount)
Subclasses can override this if they want their view to take up more than one span in a gridlayout.
- Parameters:
totalSpanCount- The number of spans in the gridposition- The position of the modelitemCount- The total number of items in the adapter
-
spanSizeOverride
EpoxyModel<T> spanSizeOverride(@Nullable() EpoxyModel.SpanSizeOverrideCallback spanSizeCallback)
-
spanSize
final int spanSize(int totalSpanCount, int position, int itemCount)
Returns the actual span size of this model, using the SpanSizeOverrideCallback if onewas set, otherwise using the value from getSpanSize
-
show
@NonNull() EpoxyModel<T> show()
Change the visibility of the model so that it's view is shown. This only works if the model isused in EpoxyAdapter or a EpoxyModelGroup, but is not supported in
-
show
@NonNull() EpoxyModel<T> show(boolean show)
Change the visibility of the model's view. This only works if the model isused in EpoxyAdapter or a EpoxyModelGroup, but is not supported in
-
hide
@NonNull() EpoxyModel<T> hide()
Change the visibility of the model so that it's view is hidden. This only works if the model isused in EpoxyAdapter or a EpoxyModelGroup, but is not supported in
-
isShown
boolean isShown()
Whether the model's view should be shown on screen. If false it won't be inflated and drawn,and will be like it was never added to the recycler view.
-
shouldSaveViewState
boolean shouldSaveViewState()
Whether the adapter should save the state of the view bound to this model.
-
onFailedToRecycleView
boolean onFailedToRecycleView(@NonNull() T view)
Called if the RecyclerView failed to recycle this model's view. You can take this opportunityto clear the animation(s) that affect the View's transient state and return
trueso that the View can be recycled. Keep in mind that the View in question is already removedfrom the RecyclerView.
-
onViewAttachedToWindow
void onViewAttachedToWindow(@NonNull() T view)
Called when this model's view is attached to the window.
-
onViewDetachedFromWindow
void onViewDetachedFromWindow(@NonNull() T view)
Called when this model's view is detached from the the window.
-
-
-
-