epoxy-adapter / com.airbnb.epoxy / EpoxyRecyclerView

EpoxyRecyclerView

open class EpoxyRecyclerView : RecyclerView

A RecyclerView implementation that makes for easier integration with Epoxy. The goal of this class is to reduce boilerplate in setting up a RecyclerView by applying common defaults. Additionally, several performance optimizations are made.

Improvements in this class are:

  1. A single view pool is automatically shared between all EpoxyRecyclerView instances in the same activity. This should increase view recycling potential and increase performance when nested RecyclerViews are used. See .initViewPool.
  2. A layout manager is automatically added with assumed defaults. See createLayoutManager
  3. Fixed size is enabled if this view's size is MATCH_PARENT
  4. If a GridLayoutManager is used this will automatically sync the span count with the EpoxyController. See syncSpanCount
  5. Helper methods like withModels, setModels, buildModelsWith make it simpler to set up simple RecyclerViews.
  6. Set an EpoxyController and build models in one step - setControllerAndBuildModels or withModels
  7. Support for automatic item spacing. See .setItemSpacingPx
  8. Defaults for usage as a nested recyclerview are provided in Carousel.
  9. setClipToPadding is set to false by default since that behavior is commonly desired in a scrolling list

Types

ModelBuilderCallback

interface ModelBuilderCallback

A callback for creating models without needing a custom EpoxyController class. Used with buildModelsWith

Constructors

<init>

EpoxyRecyclerView(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0)

A RecyclerView implementation that makes for easier integration with Epoxy. The goal of this class is to reduce boilerplate in setting up a RecyclerView by applying common defaults. Additionally, several performance optimizations are made.

Properties

spacingDecorator

val spacingDecorator: EpoxyItemSpacingDecorator

Functions

addPreloader

fun <T : EpoxyModel<*>, U : ViewMetadata?, P : PreloadRequestHolder> addPreloader(maxPreloadDistance: Int = 3, errorHandler: PreloadErrorHandler, preloader: EpoxyModelPreloader<T, U, P>, requestHolderFactory: () -> P): Unit

Setup a preloader to fetch content for a model's view before it is bound. This can be called multiple times if you would like to add separate preloaders for different models or content types.

buildModelsWith

fun buildModelsWith(callback: EpoxyRecyclerView.ModelBuilderCallback): Unit

Allows you to build models via a callback instead of needing to create a new EpoxyController class. This is useful if your models are simple and you would like to simply declare them in your activity/fragment.

clear

open fun clear(): Unit

Clear the currently set EpoxyController or Adapter as well as any models that are displayed.

clearPreloaders

fun clearPreloaders(): Unit

Clears all preloaders added with addPreloader

createLayoutManager

open fun createLayoutManager(): LayoutManager

Create a new androidx.recyclerview.widget.RecyclerView.LayoutManager instance to use for this RecyclerView.

createViewPool

open fun createViewPool(): RecycledViewPool

Create a new instance of a view pool to use with this recyclerview. By default a [ ] is used.

dpToPx

fun dpToPx(dp: Int): Int

init

open fun init(): Unit

onAttachedToWindow

open fun onAttachedToWindow(): Unit

onDetachedFromWindow

open fun onDetachedFromWindow(): Unit

requestLayout

open fun requestLayout(): Unit

requestModelBuild

fun requestModelBuild(): Unit

Request that the currently set EpoxyController has its models rebuilt. You can use this to avoid saving your controller as a field.

resToPx

fun resToPx(itemSpacingRes: Int): Int

setAdapter

open fun setAdapter(adapter: Adapter<*>?): Unit

setController

fun setController(controller: EpoxyController): Unit

Set an EpoxyController to populate this RecyclerView. This does not make the controller build its models, that must be done separately via requestModelBuild.

setControllerAndBuildModels

fun setControllerAndBuildModels(controller: EpoxyController): Unit

Set an EpoxyController to populate this RecyclerView, and tell the controller to build models.

setDelayMsWhenRemovingAdapterOnDetach

fun setDelayMsWhenRemovingAdapterOnDetach(delayMsWhenRemovingAdapterOnDetach: Int): Unit

If .setRemoveAdapterWhenDetachedFromWindow is set to true, this is the delay in milliseconds between when .onDetachedFromWindow is called and when the adapter is actually removed.

setItemSpacingDp

fun setItemSpacingDp(dp: Int): Unit

setItemSpacingPx

open fun setItemSpacingPx(spacingPx: Int): Unit

Set a pixel value to use as spacing between items. If this is a positive number an item decoration will be added to space all items this far apart from each other. If the value is 0 or negative no extra spacing will be used, and any previous spacing will be removed.

setItemSpacingRes

fun setItemSpacingRes(itemSpacingRes: Int): Unit

setLayoutManager

open fun setLayoutManager(layout: LayoutManager?): Unit

setLayoutParams

open fun setLayoutParams(params: LayoutParams): Unit

setModels

open fun setModels(models: List<EpoxyModel<*>>): Unit

Set a list of EpoxyModel's to show in this RecyclerView.

setRemoveAdapterWhenDetachedFromWindow

fun setRemoveAdapterWhenDetachedFromWindow(removeAdapterWhenDetachedFromWindow: Boolean): Unit

If set to true, any adapter set on this recyclerview will be removed when this view is detached from the window. This is useful to prevent leaking a reference to this RecyclerView. This is useful in cases where the same adapter can be used across multiple views (views which can be destroyed and recreated), such as with fragments. In that case the adapter is not necessarily cleared from previous RecyclerViews, so the adapter will continue to hold a reference to those views and leak them. More details at https://github.com/airbnb/epoxy/wiki/Avoiding-Memory-Leaks#parent-view

shouldShareViewPoolAcrossContext

open fun shouldShareViewPoolAcrossContext(): Boolean

To maximize view recycling by default we share the same view pool across all instances in the same Activity. This behavior can be disabled by returning false here.

swapAdapter

open fun swapAdapter(adapter: Adapter<*>?, removeAndRecycleExistingViews: Boolean): Unit

withModels

fun withModels(buildModels: EpoxyController.() -> Unit): Unit

The simplest way to add models to the RecyclerView without needing to create an EpoxyController. This is intended for Kotlin usage, and has the EpoxyController as the lambda receiver so models can be added easily.

Extension Functions

addEpoxyPreloader

fun <T : EpoxyModel<*>, U : ViewMetadata?, P : PreloadRequestHolder> RecyclerView.addEpoxyPreloader(epoxyController: EpoxyController, maxPreloadDistance: Int = 3, errorHandler: PreloadErrorHandler = { context, err -> if (!context.isDebuggable) throw err }, preloader: EpoxyModelPreloader<T, U, P>, requestHolderFactory: () -> P): Unit

Helper to create and add an EpoxyPreloader to this RecyclerView.

Inheritors

Carousel

open class Carousel : EpoxyRecyclerView

This feature is in Beta - please report bugs, feature requests, or other feedback at https://github.com/airbnb/epoxy by creating a new issue. Thanks!