Android辅助功能总是在ViewPager中获取上一页的内容

jyztefdp  于 2022-12-16  发布在  Android
关注(0)|答案(1)|浏览(143)

我正在使用Accessibility阅读ViewPager中的内容,在第一页,一切正常,Accessibility正在阅读当前页的内容,但当我转到下一页时,Accessibility仍在阅读上一页的内容。
例如:
页面1-〉文本视图-〉内容1
第2页-〉文本视图-〉内容2
第3页-〉文本视图-〉内容3
当当前页面为Page 1时,无障碍读取content 1,然后转到Page 2,无障碍读取content 1,无障碍读取content 2,这是如此,werid阅读代码如下:

List<AccessibilityNodeInfo> nodeInfoList = accessibilityNodeInfo.findAccessibilityNodeInfosByViewId(id);
    if (nodeInfoList != null && !nodeInfoList.isEmpty()) {
        for (AccessibilityNodeInfo nodeInfo : nodeInfoList) {
            if (nodeInfo != null) {
                return nodeInfo.text;
            }
        }
    }

有人能帮帮我吗?

o2rvlv0m

o2rvlv0m1#

我有这个问题与ViewPager,当手动拖动到下一个页面,TalkBack是确定的,但当假拖动点击按钮,其中有一个假拖动代码,然后TalkBack的ViewPager读取位置从页面之前的当前页面.所以我关闭了TalkBack的假拖动,并创建了新的TalkBack事件:

animator.addListener(object : Animator.AnimatorListener {
        override fun onAnimationStart(animation: Animator?) {
            isFakeDrag = true
            viewPager.importantForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_NO
            viewPager.beginFakeDrag()
        }
        override fun onAnimationEnd(animation: Animator?) {
            viewPager.endFakeDrag()
            viewPager.importantForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_YES
            isFakeDrag = false
        }
        override fun onAnimationCancel(animation: Animator?) { /* Ignored */ }
        override fun onAnimationRepeat(animation: Animator?) { /* Ignored */ }
    })

以及

viewPager.registerOnPageChangeCallback( object : ViewPager2.OnPageChangeCallback() {
        override fun onPageScrolled(
            position: Int,
            positionOffset: Float,
            positionOffsetPixels: Int
        ) {
            //some code
        }

        override fun onPageSelected(position: Int) {
            super.onPageSelected(position)
            if(isFakeDrag) {
                val event = AccessibilityEvent.obtain()
                event.eventType = AccessibilityEvent.TYPE_ANNOUNCEMENT
                val positionString = //create a string to be announced by TalkBack
                positionString.also { event.text.add(it) }
                val accessibilityManager = requireContext().getSystemService(Context.ACCESSIBILITY_SERVICE) as AccessibilityManager
                accessibilityManager.sendAccessibilityEvent(event)
            }
        }
    })

相关问题