使用onPopulateAccessibilityEvent和AccessibilityDelegate在recyclerview中自定义可访问性 [英] Customising accessibility in recyclerview using onPopulateAccessibilityEvent and AccessibilityDelegate
问题描述
我正在尝试为RecyclerView中的项目自定义对讲输出.我修改了onBindViewHolder
方法以包括以下代码
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!")
}
}
}
})
不幸的是,对讲输出仅针对RecyclerView
中的最后一个可见项进行修改.
Unfortunately, the talkback output is only modified for the last visible item in the RecyclerView
.
我不明白为什么代表要为某些ViewHolders
而不是其他人工作.
I don't understand why the delegate works for some ViewHolders
and not others.
推荐答案
RecyclerView
向其子级添加AccessibilityDelegates
,以向其子级添加自己的操作.由于一个View最多只能有一个委托,因此,最先添加的那个委托(您或RecyclerView
的那个委托)都会被破坏.
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
具有其自己的包装器适用于其子代的AccessibilityDelegate
.那就是你想要的.
RecyclerView
has its own wrapper of AccessibilityDelegate
that works for its children. That's what you want.
这篇关于使用onPopulateAccessibilityEvent和AccessibilityDelegate在recyclerview中自定义可访问性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!