问题
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