epoxy-adapter / com.airbnb.epoxy / EpoxyController

EpoxyController

abstract class EpoxyController : ModelCollector, StickyHeaderCallbacks

A controller for easily combining EpoxyModel instances in a RecyclerView.Adapter. Simply implement #buildModels() to declare which models should be used, and in which order. Call #requestModelBuild() whenever your data changes, and the controller will call #buildModels(), update the adapter with the new models, and notify any changes between the new and old models.

The controller maintains a androidx.recyclerview.widget.RecyclerView.Adapter with the latest models, which you can get via #getAdapter() to set on your RecyclerView.

All data change notifications are applied automatically via Epoxy's diffing algorithm. All of your models must have a unique id set on them for diffing to work. You may choose to use annotations to have the controller create models with unique ids for you automatically.

Once a model is created and added to the controller in #buildModels() it should be treated as immutable and never modified again. This is necessary for adapter updates to be accurate.

Types

ExceptionHandler

interface ExceptionHandler

Interceptor

interface Interceptor

A callback that is run after #buildModels() completes and before diffing is run.

Constructors

<init>

EpoxyController()
EpoxyController(modelBuildingHandler: Handler!, diffingHandler: Handler!)

Properties

defaultDiffingHandler

static var defaultDiffingHandler: Handler!

defaultModelBuildingHandler

static var defaultModelBuildingHandler: Handler!

Functions

add

open fun add(model: EpoxyModel<*>): Unit

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

open fun add(vararg modelsToAdd: EpoxyModel<*>!): Unit
open fun add(modelsToAdd: MutableList<out EpoxyModel<*>!>): Unit

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

addInterceptor

open fun addInterceptor(interceptor: EpoxyController.Interceptor): Unit

Add an interceptor callback to be run after models are built, to make any last changes before they are set on the adapter. Interceptors are run in the order they are added.

addModelBuildListener

open fun addModelBuildListener(listener: OnModelBuildFinishedListener!): Unit

Add a listener that will be called every time #buildModels() has finished running and changes have been dispatched to the RecyclerView.

buildModels

abstract fun buildModels(): Unit

Subclasses should implement this to describe what models should be shown for the current state. Implementations should call either #add(EpoxyModel), , or EpoxyModel#addIf(boolean, EpoxyController) with the models that should be shown, in the order that is desired.

cancelPendingModelBuild

open fun cancelPendingModelBuild(): Unit

Cancels a pending call to #buildModels() if one has been queued by .

getAdapter

open fun getAdapter(): EpoxyControllerAdapter

Get the underlying adapter built by this controller. Use this to get the adapter to set on a RecyclerView, or to get information about models currently in use.

getModelCountBuiltSoFar

open fun getModelCountBuiltSoFar(): Int

Get the number of models added so far during the #buildModels() phase. It is only valid to call this from within that method.

getSpanCount

open fun getSpanCount(): Int

getSpanSizeLookup

open fun getSpanSizeLookup(): SpanSizeLookup

For use with a grid layout manager - use this to get the SpanSizeLookup for models in this controller. This will delegate span look up calls to each model's . Make sure to also call #setSpanCount(int) so the span count is correct.

hasPendingModelBuild

open fun hasPendingModelBuild(): Boolean

Whether an update to models is currently pending. This can either be because #requestModelBuild() was called, or because models are currently being built or diff on a background thread.

isBuildingModels

open fun isBuildingModels(): Boolean

True if the current callstack originated from the buildModels call, on the same thread.

isDebugLoggingEnabled

open fun isDebugLoggingEnabled(): Boolean

isDuplicateFilteringEnabled

open fun isDuplicateFilteringEnabled(): Boolean

isMultiSpan

open fun isMultiSpan(): Boolean

isStickyHeader

open fun isStickyHeader(position: Int): Boolean

Called to check if the item at the position is a sticky item, by default returns false. The sub-classes should override the function if they are using sticky header feature.

moveModel

open fun moveModel(fromPosition: Int, toPosition: Int): Unit

An optimized way to move a model from one position to another without rebuilding all models. This is intended to be used with androidx.recyclerview.widget.ItemTouchHelper to allow for efficient item dragging and rearranging. It cannot be

notifyModelChanged

open fun notifyModelChanged(position: Int): Unit

An way to notify the adapter that a model has changed. This is intended to be used with androidx.recyclerview.widget.ItemTouchHelper to allow revert swiping a model.

onAttachedToRecyclerView

open fun onAttachedToRecyclerView(recyclerView: RecyclerView): Unit

Called when the controller's adapter is attach to a recyclerview.

onDetachedFromRecyclerView

open fun onDetachedFromRecyclerView(recyclerView: RecyclerView): Unit

Called when the controller's adapter is detached from a recyclerview.

onExceptionSwallowed

open fun onExceptionSwallowed(exception: RuntimeException): Unit

This is called when recoverable exceptions occur at runtime. By default they are ignored and Epoxy will recover, but you can override this to be aware of when they happen.

onModelBound

open fun onModelBound(holder: EpoxyViewHolder, boundModel: EpoxyModel<*>, position: Int, previouslyBoundModel: EpoxyModel<*>?): Unit

Called immediately after a model is bound to a view holder. Subclasses can override this if they want alerts on when a model is bound. Alternatively you may attach a listener directly to a generated model with model.onBind(...)

onModelUnbound

open fun onModelUnbound(holder: EpoxyViewHolder, model: EpoxyModel<*>): Unit

Called immediately after a model is unbound from a view holder. Subclasses can override this if they want alerts on when a model is unbound. Alternatively you may attach a listener directly to a generated model with model.onUnbind(...)

onRestoreInstanceState

open fun onRestoreInstanceState(inState: Bundle?): Unit

onSaveInstanceState

open fun onSaveInstanceState(outState: Bundle): Unit

onViewAttachedToWindow

open fun onViewAttachedToWindow(holder: EpoxyViewHolder, model: EpoxyModel<*>): Unit

Called when the given viewholder is attached to the window, along with the model it is bound to.

onViewDetachedFromWindow

open fun onViewDetachedFromWindow(holder: EpoxyViewHolder, model: EpoxyModel<*>): Unit

Called when the given viewholder is detechaed from the window, along with the model it is bound to.

removeInterceptor

open fun removeInterceptor(interceptor: EpoxyController.Interceptor): Unit

Remove an interceptor that was added with #addInterceptor(Interceptor).

removeModelBuildListener

open fun removeModelBuildListener(listener: OnModelBuildFinishedListener!): Unit

Remove a listener added with #addModelBuildListener(OnModelBuildFinishedListener). This is safe to call from inside the callback OnModelBuildFinishedListener#onModelBuildFinished(DiffResult)

requestDelayedModelBuild

open fun requestDelayedModelBuild(delayMs: Int): Unit

Call this to request a delayed model update. The controller will schedule a call to so that models can be rebuilt for the current data.

requestModelBuild

open fun requestModelBuild(): Unit

Call this to request a model update. The controller will schedule a call to so that models can be rebuilt for the current data. Once a build is requested all subsequent requests are ignored until the model build runs. Therefore, the calling code need not worry about calling this multiple times in a row.

setDebugLoggingEnabled

open fun setDebugLoggingEnabled(enabled: Boolean): Unit

If enabled, DEBUG logcat messages will be printed to show when models are rebuilt, the time taken to build them, the time taken to diff them, and the item change outcomes from the differ. The tag of the logcat message is the class name of your EpoxyController.

setFilterDuplicates

open fun setFilterDuplicates(filterDuplicates: Boolean): Unit

If set to true, Epoxy will search for models with duplicate ids added during and remove any duplicates found. If models with the same id are found, the first one is left in the adapter and any subsequent models are removed. will be called for each duplicate removed.

setGlobalDebugLoggingEnabled

open static fun setGlobalDebugLoggingEnabled(globalDebugLoggingEnabled: Boolean): Unit

Similar to #setDebugLoggingEnabled(boolean), but this changes the global default for all EpoxyControllers.

setGlobalDuplicateFilteringDefault

open static fun setGlobalDuplicateFilteringDefault(filterDuplicatesByDefault: Boolean): Unit

#setFilterDuplicates(boolean) is disabled in each EpoxyController by default. It can be toggled individually in each controller, or alternatively you can use this to change the default value for all EpoxyControllers.

setGlobalExceptionHandler

open static fun setGlobalExceptionHandler(globalExceptionHandler: EpoxyController.ExceptionHandler): Unit

Set a callback to be notified when a recoverable exception occurs at runtime. By default these are ignored and Epoxy will recover, but you can override this to be aware of when they happen.

setSpanCount

open fun setSpanCount(spanCount: Int): Unit

If you are using a grid layout manager you must call this to set the span count of the grid. This span count will be passed on to the models so models can choose which span count to be.

setupStickyHeaderView

open fun setupStickyHeaderView(stickyHeader: View): Unit

Optional callback to setup the sticky view, by default it doesn't do anything. The sub-classes should override the function if they are using sticky header feature.

teardownStickyHeaderView

open fun teardownStickyHeaderView(stickyHeader: View): Unit

Optional callback to perform tear down operation on the sticky view, by default it doesn't do anything. The sub-classes should override the function if they are using sticky header feature.

Inheritors

AsyncEpoxyController

abstract class AsyncEpoxyController : EpoxyController

A subclass of EpoxyController that makes it easy to do model building and diffing in the background.

SimpleEpoxyController

open class SimpleEpoxyController : EpoxyController

A small wrapper around com.airbnb.epoxy.EpoxyController that lets you set a list of models directly.

Typed2EpoxyController

abstract class Typed2EpoxyController<T : Any!, U : Any!> : EpoxyController

This is a wrapper around com.airbnb.epoxy.EpoxyController to simplify how data is accessed. Use this if the data required to build your models is represented by two objects.

Typed3EpoxyController

abstract class Typed3EpoxyController<T : Any!, U : Any!, V : Any!> : EpoxyController

This is a wrapper around com.airbnb.epoxy.EpoxyController to simplify how data is accessed. Use this if the data required to build your models is represented by three objects.

Typed4EpoxyController

abstract class Typed4EpoxyController<T : Any!, U : Any!, V : Any!, W : Any!> : EpoxyController

This is a wrapper around com.airbnb.epoxy.EpoxyController to simplify how data is accessed. Use this if the data required to build your models is represented by four objects.

TypedEpoxyController

abstract class TypedEpoxyController<T : Any!> : EpoxyController

This is a wrapper around com.airbnb.epoxy.EpoxyController to simplify how data is accessed. Use this if the data required to build your models is represented by a single object.