Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix focus listener not being invoked for parent RecyclerView #229

Merged
merged 7 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading