DpadComposeViewHolder
class DpadComposeViewHolder<T>(parent: ViewGroup, onClick: (item: T) -> Unit? = null, onLongClick: (item: T) -> Boolean? = null, isFocusable: Boolean = true, compositionStrategy: ViewCompositionStrategy = RecyclerViewCompositionStrategy.DisposeOnRecycled, content: @Composable (item: T, isFocused: Boolean) -> Unit = { _, _ -> }) : RecyclerView.ViewHolder
A basic ViewHolder that forwards content to a ComposeView and handles focus and clicks inside the View system.
Focus is kept inside the internal ComposeView to ensure that it behaves correctly and to workaround the following issues:
Focus is not sent correctly from Views to Composables: b/268248352 This is solved by just holding the focus in ComposeView
Clicking on a focused Composable does not trigger the standard audio feedback: b/268268856 This is solved by just handling the click on ComposeView directly
This allows inline definition of ViewHolders in onCreateViewHolder
:
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DpadComposeViewHolder<Int> {
return DpadComposeViewHolder(parent) { item, isFocused ->
ItemComposable(item, isFocused)
}
}
Content copied to clipboard
To update the current item, override onBindViewHolder
and call setItemState:
override fun onBindViewHolder(holder: DpadComposeViewHolder<Int>, position: Int) {
holder.setItemState(getItem(position))
}
Content copied to clipboard
Constructors
Link copied to clipboard
constructor(parent: ViewGroup, onClick: (item: T) -> Unit? = null, onLongClick: (item: T) -> Boolean? = null, isFocusable: Boolean = true, compositionStrategy: ViewCompositionStrategy = RecyclerViewCompositionStrategy.DisposeOnRecycled, content: @Composable (item: T, isFocused: Boolean) -> Unit = { _, _ -> })