使用onPopulateAccessibilityEvent和AccessibilityDelegate在recyclerview中自定义可访问性 [英] Customising accessibility in recyclerview using onPopulateAccessibilityEvent and AccessibilityDelegate

查看:328
本文介绍了使用onPopulateAccessibilityEvent和AccessibilityDelegate在recyclerview中自定义可访问性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆