Skip to content

Commit

Permalink
Allow disabling focus for Compose ViewHolders
Browse files Browse the repository at this point in the history
  • Loading branch information
rubensousa committed Jun 3, 2024
1 parent 9f0be51 commit baeb541
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
5 changes: 3 additions & 2 deletions dpadrecyclerview-compose/api/dpadrecyclerview-compose.api
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ public final class com/rubensousa/dpadrecyclerview/compose/DpadComposeExtensions

public final class com/rubensousa/dpadrecyclerview/compose/DpadComposeFocusViewHolder : androidx/recyclerview/widget/RecyclerView$ViewHolder {
public static final field $stable I
public fun <init> (Landroid/view/ViewGroup;Landroidx/compose/ui/platform/ViewCompositionStrategy;Lkotlin/jvm/functions/Function3;)V
public synthetic fun <init> (Landroid/view/ViewGroup;Landroidx/compose/ui/platform/ViewCompositionStrategy;Lkotlin/jvm/functions/Function3;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun <init> (Landroid/view/ViewGroup;Landroidx/compose/ui/platform/ViewCompositionStrategy;ZLkotlin/jvm/functions/Function3;)V
public synthetic fun <init> (Landroid/view/ViewGroup;Landroidx/compose/ui/platform/ViewCompositionStrategy;ZLkotlin/jvm/functions/Function3;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public final fun getItem ()Ljava/lang/Object;
public final fun setFocusable (Z)V
public final fun setItemState (Ljava/lang/Object;)V
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package com.rubensousa.dpadrecyclerview.compose
import android.view.ViewGroup
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.platform.ViewCompositionStrategy
import androidx.recyclerview.widget.RecyclerView

Expand All @@ -46,17 +45,16 @@ import androidx.recyclerview.widget.RecyclerView
class DpadComposeFocusViewHolder<T>(
parent: ViewGroup,
compositionStrategy: ViewCompositionStrategy = RecyclerViewCompositionStrategy.DisposeOnRecycled,
isFocusable: Boolean = true,
private val content: @Composable (item: T) -> Unit
) : RecyclerView.ViewHolder(ComposeView(parent.context)) {
) : RecyclerView.ViewHolder(DpadComposeView(parent.context)) {

private val itemState = mutableStateOf<T?>(null)
private val composeView = itemView as ComposeView
private val composeView = itemView as DpadComposeView

init {
composeView.apply {
isFocusable = true
isFocusableInTouchMode = true
descendantFocusability = ViewGroup.FOCUS_AFTER_DESCENDANTS
this@DpadComposeFocusViewHolder.setFocusable(isFocusable)
setViewCompositionStrategy(compositionStrategy)
setContent {
itemState.value?.let { item ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,10 @@ class GridSpanHeaderFragment : Fragment(R.layout.screen_recyclerview) {
}

else -> {
DpadComposeFocusViewHolder<Int>(parent) { _ ->
DpadComposeFocusViewHolder(
parent,
isFocusable = false
) { _ ->
Text(
modifier = Modifier.padding(
horizontal = 48.dp,
Expand All @@ -113,8 +116,6 @@ class GridSpanHeaderFragment : Fragment(R.layout.screen_recyclerview) {
color = Color.White,
style = MaterialTheme.typography.titleLarge,
)
}.also {
it.setFocusable(false)
}
}
}
Expand Down

0 comments on commit baeb541

Please sign in to comment.