Skip to content

Commit

Permalink
Merge pull request #229 from rubensousa/nested_focus_listener
Browse files Browse the repository at this point in the history
Fix focus listener not being invoked for parent RecyclerView
  • Loading branch information
rubensousa authored Jun 19, 2024
2 parents d62dacd + d3f57da commit 8396d74
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ jobs:
./gradlew --build-cache dpadrecyclerview-compose:connectedDebugAndroidTest
./gradlew --build-cache sample:connectedDebugAndroidTest
./gradlew --build-cache dpadrecyclerview-testing:connectedDebugAndroidTest
./gradlew --build-cache dpadrecyclerview:connectedDebugAndroidTest --info
./gradlew --build-cache dpadrecyclerview:connectedDebugAndroidTest
- name: Upload artifacts
uses: actions/upload-artifact@v3
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ jobs:
matrix:
arch: [ x86 ]
target: [ android-tv ]
api-level: [27]
api-level: [22, 25, 27, 28, 29, 30, 31]
profile: [tv_1080p]
steps:
- name: checkout
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ package com.rubensousa.dpadrecyclerview.test.tests.focus
import androidx.core.view.get
import androidx.fragment.app.testing.FragmentScenario
import androidx.fragment.app.testing.launchFragmentInContainer
import androidx.test.espresso.Espresso
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.espresso.matcher.ViewMatchers.withTagValue
import com.google.common.truth.Truth.assertThat
import com.rubensousa.dpadrecyclerview.DpadRecyclerView
import com.rubensousa.dpadrecyclerview.test.TestNestedListFragment
Expand All @@ -29,7 +32,10 @@ import com.rubensousa.dpadrecyclerview.testfixtures.DpadFocusEvent
import com.rubensousa.dpadrecyclerview.testfixtures.recording.ScreenRecorderRule
import com.rubensousa.dpadrecyclerview.testing.KeyEvents
import com.rubensousa.dpadrecyclerview.testing.R
import com.rubensousa.dpadrecyclerview.testing.actions.DpadRecyclerViewActions
import com.rubensousa.dpadrecyclerview.testing.rules.DisableIdleTimeoutRule
import org.hamcrest.Matchers
import org.hamcrest.core.AllOf.allOf
import org.junit.Before
import org.junit.Rule
import org.junit.Test
Expand Down Expand Up @@ -127,6 +133,27 @@ class NestedFocusListenerTest {
}
}

@Test
fun testParentRecyclerViewStillReceivesFocusIfChildHasNoListener() {
// given
val focusEvents = 6
Espresso.onView(
allOf(
withId(com.rubensousa.dpadrecyclerview.test.R.id.nestedRecyclerView),
withTagValue(Matchers.`is`(0))
)
).perform(DpadRecyclerViewActions.execute("Clear listener") { recyclerView ->
recyclerView.clearOnViewFocusedListeners()
})

// when
KeyEvents.pressRight(times = focusEvents - 1)
waitForIdleScrollState()

// then
assertThat(getParentFocusEvents()).hasSize(focusEvents)
}

private fun getChildFocusEvents(): List<DpadFocusEvent> {
var events = listOf<DpadFocusEvent>()
fragmentScenario.onFragment { fragment ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ class PivotLayoutManager(properties: Properties) : RecyclerView.LayoutManager(),
// If we have focus, save it temporarily since the views will change and we might lose it
hadFocusBeforeLayout = hasFocus()
scroller.cancelSmoothScroller()
pivotSelector.onLayoutChildren(state)
pivotLayout.onLayoutChildren(recycler, state)
layoutCompletedListener?.onLayoutCompleted(state)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ internal class PivotSelector(

fun focus(view: View) {
view.requestFocus()
// Exit early if there's no one listening for focus events
if (focusListeners.isEmpty() || isRetainingFocus) {
// Do not notify listeners if we are retaining focus
if (isRetainingFocus) {
return
}
val currentRecyclerView = recyclerView ?: return
Expand Down Expand Up @@ -213,10 +213,6 @@ internal class PivotSelector(
return max(0, min(itemCount - 1, position))
}

fun onLayoutChildren(state: RecyclerView.State) {

}

fun onLayoutCompleted() {
if (isSelectionUpdatePending) {
isSelectionUpdatePending = false
Expand Down

0 comments on commit 8396d74

Please sign in to comment.