ViewProvider

class ViewProvider<T>(val lifecycleOwner: LifecycleOwner, val itemViewFactoryProvider: (T) -> ViewFactory<T>, val supportsRebind: Boolean = false)

Provides sub views for items in a View that supports multiple items.

itemViewFactoryProvider is used to provide a ViewFactory for an item.

See the secondary constructor to inflate the same layout for each item.

By default, rebinding an item to a previously created view is not supported, since it is not guaranteed that the view for the item is the same as the previously created view. However, if this can be guaranteed, set supportsRebind to true. This can prevent the expensive creation of views in situations where the number of items stays the same, but the content of the items changes.

Parameters

T

the type of the items.

Constructors

Link copied to clipboard
constructor(lifecycleOwner: LifecycleOwner, @LayoutRes layoutId: Int)

Convenience constructor to create a view provider that provides the same layout ID for all items.

constructor(lifecycleOwner: LifecycleOwner, itemViewFactoryProvider: (T) -> ViewFactory<T>, supportsRebind: Boolean = false)

Properties

Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
val supportsRebind: Boolean = false

Functions

Link copied to clipboard
fun createView(context: Context, parent: ViewGroup, item: T, subLifecycleOwner: SubLifecycleOwner, addViewToParent: (ViewGroup, View) -> Unit = { parentView, view -> parentView.addView(view) }): View

Creates a bound view for the item using the provided subLifecycleOwner, the view needs to be added to the parent using the addViewToParent function. The subLifecycleOwner should be derived from the lifecycleOwner and therefore guarantees that its lifecycle state is at most the lifecycle state of the lifecycleOwner.

Link copied to clipboard
fun rebindView(view: View, item: T)

Rebinds an earlier created view to the new item.