Customising accessibility in recyclerview using onPopulateAccessibilityEvent and AccessibilityDelegate

不羁岁月 提交于 2021-02-07 10:38:19

问题


I'm trying to customise the talkback output for items in a RecyclerView. I have modified the onBindViewHolder method to include the following code

       itemView?.setAccessibilityDelegate(object: View.AccessibilityDelegate() {
            override fun onPopulateAccessibilityEvent(host: View?, event: AccessibilityEvent?) {
                super.onPopulateAccessibilityEvent(host, event)
                event?.let {event ->
                    if (event.eventType == AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED) {
                        event.text.add("Hello world!")
                    }
                }
            }
        })

Unfortunately, the talkback output is only modified for the last visible item in the RecyclerView.

I don't understand why the delegate works for some ViewHolders and not others.


回答1:


RecyclerView adds AccessibilityDelegates to its children to add its own actions to them. Since a View may have at most one delegate, whichever one is added first (yours or the one for RecyclerView) gets clobbered.

RecyclerView has its own wrapper of AccessibilityDelegate that works for its children. That's what you want.



来源:https://stackoverflow.com/questions/48446769/customising-accessibility-in-recyclerview-using-onpopulateaccessibilityevent-and

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!