Skip to content

Commit

Permalink
Merge pull request #278 from rubensousa/focus_disabled
Browse files Browse the repository at this point in the history
Fix focus not leaving current RecyclerView
  • Loading branch information
rubensousa authored Oct 9, 2024
2 parents 8b363e9 + e4519c0 commit cca238e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,40 @@ class NestedFocusDirectionTest {
}
}

@Test
fun testFocusStillLeavesRecyclerViewWhenFocusSearchIsDisabled() = report {
step("Disable focus searches for the first recyclerview") {
Espresso.onView(
allOf(
withId(
com.rubensousa.dpadrecyclerview.test.R.id.nestedRecyclerView
),
withTagValue(Matchers.`is`(0))
)
).perform(DpadRecyclerViewActions.execute("Disable focus searches") { recyclerView ->
recyclerView.setFocusSearchDisabled(true)
})
}

step("Press down") {
KeyEvents.pressDown()
waitForIdleScrollState()
}

step("Focus should be in second list") {
Espresso.onView(
allOf(
withId(
com.rubensousa.dpadrecyclerview.test.R.id.nestedRecyclerView
),
withTagValue(Matchers.`is`(1))
)
).check(
DpadRecyclerViewAssertions.isFocused(position = 0)
)
}
}

private fun launchFragment(): FragmentScenario<NestedFocusDirectionFragment> {
return launchFragmentInContainer<NestedFocusDirectionFragment>(
themeResId = R.style.DpadRecyclerViewTestTheme
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ internal class LayoutConfiguration(properties: Properties) {
var isFocusSearchDisabled = false
private set

private var isFocusSearchEnabledDuringAnimations = false
var isFocusSearchEnabledDuringAnimations = false
private set

// Number of items to prefetch when first coming on screen with new data
var initialPrefetchItemCount = 4
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,15 @@ internal class FocusDispatcher(
): View? {
val currentRecyclerView = recyclerView ?: return focused

if (!isFocusSearchEnabled(currentRecyclerView)) {
// If the parent RecyclerView does not allow focusing children,
// just delegate focus to its parent
if (currentRecyclerView.descendantFocusability == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
return currentRecyclerView.parent?.focusSearch(focused, direction)
}

if (!configuration.isFocusSearchEnabledDuringAnimations
&& currentRecyclerView.isAnimating
) {
return focused
}

Expand Down Expand Up @@ -175,13 +183,6 @@ internal class FocusDispatcher(
}
}


// If the parent RecyclerView does not allow focusing children,
// just delegate focus to its parent
if (currentRecyclerView.descendantFocusability == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
return currentRecyclerView.parent?.focusSearch(focused, direction)
}

val isScrolling = currentRecyclerView.scrollState != RecyclerView.SCROLL_STATE_IDLE
when (focusDirection) {
FocusDirection.NEXT_ROW -> {
Expand Down

0 comments on commit cca238e

Please sign in to comment.