epoxy-adapter / com.airbnb.epoxy / EpoxyModelWithView

EpoxyModelWithView

abstract class EpoxyModelWithView<T : View!> : EpoxyModel<T>

A model that allows its view to be built programmatically instead of by inflating a layout resource. Just implement #buildView so the adapter can create a new view for this model when needed.

#getViewType() is used by the adapter to know how to reuse views for this model. This means that all models that return the same type should be able to share the same view, and the view won't be shared with models of any other type.

If it is left unimplemented then at runtime a unique view type will be created to use for all models of that class. The generated view type will be negative so that it cannot collide with values from resource files, which are used in normal Epoxy models. If you would like to share the same view between models of different classes you can have those classes return the same view type. A good way to manually create a view type value is by creating an R.id. value in an ids resource file.

Constructors

<init>

EpoxyModelWithView()

A model that allows its view to be built programmatically instead of by inflating a layout resource. Just implement #buildView so the adapter can create a new view for this model when needed.

Functions

buildView

abstract fun buildView(parent: ViewGroup): T

Create and return a new instance of a view for this model. If no layout params are set on the returned view then default layout params will be used.

getDefaultLayout

fun getDefaultLayout(): Int

getViewType

open fun getViewType(): Int

Get the view type associated with this model's view. Any models with the same view type will have views recycled between them.

layout

open fun layout(layoutRes: Int): EpoxyModel<T>

Inherited Functions

addIf

open fun addIf(condition: Boolean, controller: EpoxyController): Unit

Add this model to the given controller if the condition is true. Can only be called from inside EpoxyController#buildModels().

open fun addIf(predicate: EpoxyModel.AddPredicate, controller: EpoxyController): Unit

Add this model to the given controller if the AddPredicate return true. Can only be called from inside EpoxyController#buildModels().

addTo

open fun addTo(controller: EpoxyController): Unit

Add this model to the given controller. Can only be called from inside .

addWithDebugValidation

fun addWithDebugValidation(controller: EpoxyController): Unit

This is used internally by generated models to turn on validation checking when "validateEpoxyModelUsage" is enabled and the model is used with an EpoxyController.

bind

open fun bind(view: T): Unit

Binds the current data to the given view. You should bind all fields including unset/empty fields to ensure proper recycling.

open fun bind(view: T, payloads: MutableList<Any!>): Unit

Similar to #bind(Object), but provides a non null, non empty list of payloads describing what changed. This is the payloads list specified in the adapter's notifyItemChanged method. This is a useful optimization to allow you to only change part of a view instead of updating the whole thing, which may prevent unnecessary layout calls. If there are no payloads then #bind(Object) is called instead. This will only be used if the model is used with an EpoxyAdapter

open fun bind(view: T, previouslyBoundModel: EpoxyModel<*>): Unit

Similar to #bind(Object), but provides a non null model which was previously bound to this view. This will only be called if the model is used with an EpoxyController.

equals

open fun equals(other: Any?): Boolean

getLayout

fun getLayout(): Int

getSpanSize

open fun getSpanSize(totalSpanCount: Int, position: Int, itemCount: Int): Int

Subclasses can override this if they want their view to take up more than one span in a grid layout.

hashCode

open fun hashCode(): Int

hide

open fun hide(): EpoxyModel<T>

Change the visibility of the model so that it's view is hidden. This only works if the model is used in EpoxyAdapter or a EpoxyModelGroup, but is not supported in

id

open fun id(): Longopen fun id(id: Long): EpoxyModel<T>!

Override the default id in cases where the data subject naturally has an id, like an object from a database. This id can only be set before the model is added to the adapter, it is an error to change the id after that.

open fun id(vararg ids: Number!): EpoxyModel<T>!

Use multiple numbers as the id for this model. Useful when you don't have a single long that represents a unique id.

open fun id(id1: Long, id2: Long): EpoxyModel<T>!

Use two numbers as the id for this model. Useful when you don't have a single long that represents a unique id.

open fun id(key: CharSequence?): EpoxyModel<T>!

Use a string as the model id. Useful for models that don't clearly map to a numerical id. This is preferable to using String#hashCode() because that is a 32 bit hash and this is a 64 bit hash, giving better spread and less chance of collision with other ids.

open fun id(key: CharSequence?, vararg otherKeys: CharSequence!): EpoxyModel<T>!

Use several strings to define the id of the model.

open fun id(key: CharSequence?, id: Long): EpoxyModel<T>!

Set an id that is namespaced with a string. This is useful when you need to show models of multiple types, side by side and don't want to risk id collisions.

isShown

open fun isShown(): Boolean

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.

onFailedToRecycleView

open fun onFailedToRecycleView(view: T): Boolean

Called if the RecyclerView failed to recycle this model's view. You can take this opportunity to clear the animation(s) that affect the View's transient state and return true so that the View can be recycled. Keep in mind that the View in question is already removed from the RecyclerView.

onMutation

fun onMutation(): Unit

This is used internally by generated models to do validation checking when "validateEpoxyModelUsage" is enabled and the model is used with an EpoxyController. This method validates that it is ok to change this model. It is only valid if the model hasn't yet been added, or the change is being done from an EpoxyController.Interceptor callback.

onViewAttachedToWindow

open fun onViewAttachedToWindow(view: T): Unit

Called when this model's view is attached to the window.

onViewDetachedFromWindow

open fun onViewDetachedFromWindow(view: T): Unit

Called when this model's view is detached from the the window.

onVisibilityChanged

open fun onVisibilityChanged(percentVisibleHeight: Float, percentVisibleWidth: Float, visibleHeight: Int, visibleWidth: Int, view: T): Unit

TODO link to the wiki

onVisibilityStateChanged

open fun onVisibilityStateChanged(visibilityState: Int, view: T): Unit

TODO link to the wiki

reset

open fun reset(): EpoxyModel<T>

Sets fields of the model to default ones.

shouldSaveViewState

open fun shouldSaveViewState(): Boolean

Whether the adapter should save the state of the view bound to this model.

show

open fun show(): EpoxyModel<T>

Change the visibility of the model so that it's view is shown. This only works if the model is used in EpoxyAdapter or a EpoxyModelGroup, but is not supported in

open fun show(show: Boolean): EpoxyModel<T>

Change the visibility of the model's view. This only works if the model is used in EpoxyAdapter or a EpoxyModelGroup, but is not supported in

spanSize

fun spanSize(totalSpanCount: Int, position: Int, itemCount: Int): Int

Returns the actual span size of this model, using the SpanSizeOverrideCallback if one was set, otherwise using the value from #getSpanSize(int, int, int)

spanSizeOverride

open fun spanSizeOverride(spanSizeCallback: EpoxyModel.SpanSizeOverrideCallback?): EpoxyModel<T>!

toString

open fun toString(): String

unbind

open fun unbind(view: T): Unit

Called when the view bound to this model is recycled. Subclasses can override this if their view should release resources when it's recycled.

validateStateHasNotChangedSinceAdded

fun validateStateHasNotChangedSinceAdded(descriptionOfChange: String!, modelPosition: Int): Unit

This is used internally by generated models to do validation checking when "validateEpoxyModelUsage" is enabled and the model is used with a EpoxyController. This method validates that the model's hashCode hasn't been changed since it was added to the controller. This is similar to #onMutation(), but that method is only used for specific model changes such as calling a setter. By checking the hashCode, this method allows us to catch more subtle changes, such as through setting a field directly or through changing an object that is set on the model.