epoxy-adapter / com.airbnb.epoxy / Carousel

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!

This is intended as a plug and play "Carousel" view - a Recyclerview with horizontal scrolling. It comes with common defaults and performance optimizations and can be either used as a top level RecyclerView, or nested within a vertical recyclerview.

This class provides:

1. Automatic integration with Epoxy. A CarouselModel_ is generated from this class, which you can use in your EpoxyController. Just call #setModels(List) to provide the list of models to show in the carousel.

2. Default padding for carousel peeking, and an easy way to change this padding -

3. Easily control how many items are shown on screen in the carousel at a time -

4. Easy snap support. By default a LinearSnapHelper is used, but you can set a global default for all Carousels with #setDefaultGlobalSnapHelperFactory(SnapHelperFactory)

5. All of the benefits of EpoxyRecyclerView

If you need further flexibility you can subclass this view to change its width, height, scrolling direction, etc. You can annotate a subclass with ModelView to generate a new EpoxyModel.

Types

Padding

open class Padding

Used to specify individual padding values programmatically.

SnapHelperFactory

abstract class SnapHelperFactory

Provide a SnapHelper implementation you want to use with a Carousel.

Constructors

<init>

Carousel(context: Context!)
Carousel(context: Context!, attrs: AttributeSet?)
Carousel(context: Context!, attrs: AttributeSet?, defStyle: Int)

Properties

NO_VALUE_SET

static val NO_VALUE_SET: Int

Inherited Properties

spacingDecorator

val spacingDecorator: EpoxyItemSpacingDecorator

Functions

clear

open fun clear(): Unit

getDefaultSpacingBetweenItemsDp

open fun getDefaultSpacingBetweenItemsDp(): Int

Return the item spacing to use in this carousel, or 0 for no spacing.

getNumViewsToShowOnScreen

open fun getNumViewsToShowOnScreen(): Float

getSnapHelperFactory

open fun getSnapHelperFactory(): Carousel.SnapHelperFactory?

Return a SnapHelperFactory instance to use with this Carousel. The SnapHelper created by the factory will be attached to this Carousel on view creation. Return null for no snap helper to be attached automatically.

init

open fun init(): Unit

onChildAttachedToWindow

open fun onChildAttachedToWindow(child: View): Unit

onChildDetachedFromWindow

open fun onChildDetachedFromWindow(child: View): Unit

setDefaultGlobalSnapHelperFactory

open static fun setDefaultGlobalSnapHelperFactory(factory: Carousel.SnapHelperFactory?): Unit

Set a SnapHelperFactory instance to use with all Carousels by default. The created by the factory will be attached to each Carousel on view creation. Set null for no snap helper to be attached automatically.

setDefaultItemSpacingDp

open static fun setDefaultItemSpacingDp(dp: Int): Unit

Set a global default to use as the item spacing for all Carousels. Set to 0 for no item spacing.

setHasFixedSize

open fun setHasFixedSize(hasFixedSize: Boolean): Unit

setInitialPrefetchItemCount

open fun setInitialPrefetchItemCount(numItemsToPrefetch: Int): Unit

If you are using a Linear or Grid layout manager you can use this to set the item prefetch count. Only use this if you are not using #setNumViewsToShowOnScreen(float)

setModels

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

setNumViewsToShowOnScreen

open fun setNumViewsToShowOnScreen(viewCount: Float): Unit

Set the number of views to show on screen in this carousel at a time, partial numbers are allowed.

setPadding

open fun setPadding(padding: Carousel.Padding?): Unit

Use the Padding class to specify individual padding values for each side of the carousel, as well as item spacing.

setPaddingDp

open fun setPaddingDp(paddingDp: Int): Unit

Set a DP value to use as the padding on each side of the carousel and in between carousel items.

setPaddingRes

open fun setPaddingRes(paddingRes: Int): Unit

Set a dimension resource to specify the padding value to use on each side of the carousel and in between carousel items.

Inherited 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.

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

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

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.