Search code examples
androidandroid-recyclerviewtalkbackaccessibility

How to shift accessibility focus on RecyclerView's first element on some action?


I am new with android accessibility - TalkBack. I am aware of basic things like contentDescription, importantForAaccessibility and how they make node tree, etc.

In my problem, I want to shift accessibility focus on RecyclerView's first element on some action.

Usually someView.requestFocus() or someView.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED) works. But it seems to not working as expected in RecyclerView.

I have tried to solve it by:

  1. android:accessibilityLiveRegion="polite" but it only announce that list is there. But it doesn't go on an element with accessibility focus.

  2. By focusing the first element from the adapter - but it's a bad idea to do so!

Other than that, android:accessibilityTraversalAfter and android:accessibilityTraversalBefore also don't work well with a list view in android.

someView.requestFocus()
someView.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED)

Solution

  • RecyclerView's getChildAt() method worked for me.

    recyclerView.getChildAt(0).requestFocus()
    recyclerView.getChildAt(0).sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED)