1

I built an endless horizontal recycler view similar to the one in the solution of this post: How do I create a circular (endless) RecyclerView? with 5 total items.

Currently because the count of the list is Int.MAX_INTEGER, TalkBack dictates the view as

"1073741820 of 2147483650, in list, 2147483650 items"

For Accessibility, what can I do to have TalkBack to describe the contents of each item as

"1 of 5, in list, 5 items" etc for each item.

Danielson
  • 93
  • 5

2 Answers2

1

This may help you

    private fun setupAccessibility() {
    val recyclerView = binding.viewPager.getChildAt(0) as? RecyclerView ?: return
    recyclerView.setAccessibilityDelegateCompat(object :
        RecyclerViewAccessibilityDelegate(recyclerView) {
        override fun onInitializeAccessibilityNodeInfo(
            host: View?,
            info: AccessibilityNodeInfoCompat?
        ) {
            super.onInitializeAccessibilityNodeInfo(host, info)
            info?.collectionInfo?.run {
                info.setCollectionInfo(
                    AccessibilityNodeInfoCompat.CollectionInfoCompat.obtain(
                        max(1, rowCount % ITEMS_COUNT),
                        max(1, columnCount % ITEMS_COUNT),
                        isHierarchical,
                        selectionMode
                    )
                )
            }
        }

        override fun sendAccessibilityEventUnchecked(host: View?, event: AccessibilityEvent?) {
            super.sendAccessibilityEventUnchecked(host, event)
            event?.apply {
                fromIndex %= ITEMS_COUNT
                toIndex %= ITEMS_COUNT
                itemCount = ITEMS_COUNT
            }
        }

        override fun onInitializeAccessibilityEvent(host: View?, event: AccessibilityEvent?) {
            super.onInitializeAccessibilityEvent(host, event)
            event?.apply {
                fromIndex %= ITEMS_COUNT
                toIndex %= ITEMS_COUNT
                itemCount = ITEMS_COUNT
            }
        }

        override fun getItemDelegate(): AccessibilityDelegateCompat {
            return object : ItemDelegate(this) {
                override fun onInitializeAccessibilityNodeInfo(
                    host: View?,
                    info: AccessibilityNodeInfoCompat?
                ) {
                    super.onInitializeAccessibilityNodeInfo(host, info)
                    info?.collectionItemInfo?.run {
                        info.setCollectionItemInfo(
                            AccessibilityNodeInfoCompat.CollectionItemInfoCompat.obtain(
                                rowIndex % ITEMS_COUNT,
                                rowSpan,
                                columnIndex % ITEMS_COUNT,
                                columnSpan,
                                info.isHeading,
                                isSelected
                            )
                        )
                    }
                }
            }
        }
    })
}

Where ITEMS_COUNT is actual number of items (data items count)

Gopal Gopi
  • 11,101
  • 1
  • 30
  • 43
0

The number can be changed using AccessibilityDelegateCompat, specifically, CollectionInfoCompat would allow to set rowCount to 5, instead of Int.MAX_INTEGER.

However, with this, custom circular navigation likely would not work anymore when using Talkback.

sigute
  • 1,143
  • 12
  • 21