0

I'm using Accessibility to read the content in ViewPager, in the first page, everything is alright, Accessibility is reading the content of current page, But when I turn to next page, Accessibility still reading the the content of previous page.

For example:

Page1 -> TextView -> content1

Page2 -> TextView -> content2

Page3 -> TextView -> content3

Accessibility reads content1 when current page is Page1, then turn to Page2, Accessibility reads content1,Accessibility reads content2, this is so werid reading code is like this:

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

Any body help me?

lucher
  • 1
  • 1

1 Answers1

0

I had this problem with ViewPager, when manually dragged to a next page, TalkBack was OK, but when fake dragged by tapping on a button which had a fake drag code, then TalkBack on the ViewPager read position from the page before current page. So I turned off TalkBack on a fake drag and created new TalkBack event:

    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 */ }
    })

and

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)
            }
        }
    })
Dragan N
  • 21
  • 4