Fragment not getting accessibility focus after replace

…衆ロ難τιáo~ 提交于 2021-02-10 06:31:31

问题


I have an activity that has a fragment. That fragment gets the accessibility focus correctly. But when a replace the first fragment with another one, the second one is not getting the focuse automatically, I have to touch the fragment to get the focus.

This is the way a do the replace:

val fragmentTransaction = supportFragmentManager.beginTransaction()
fragmentTransaction.replace(R.id.container, myFragment)
fragmentTransaction.commit()
container.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED)

I try to add the android:importantForAccessibility and the android:clickable="true" to the second fragment (the one that is not getting accessibility focus)

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:orientation="vertical"
    android:clickable="true"
    android:focusable="true"
    android:importantForAccessibility="yes">

    ...

</LinearLayout>

I also try to use the add fragment instead of replace with the same result.

Thanks!


回答1:


An alternate solution - you can simply notify a user about screen change. If your fragment contains a toolbar, then you can set it focused while fragment is being displayed. or you could focus on the first/main reasonable element. In this way, It will announce a new screen to the user and it will be more helpful to users.

toolbar.requestFocus()
toolbar.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED)



回答2:


Please use below code In first fragment before you do the fragment transaction.

rootView = inflater.inflate(R.layout.first_fragment, null, false);
rootView.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);

Hope this will help.




回答3:


You can perform accessibility action in fragment onActivityCreated method like below

getParentActivity().getToolbar().
          performAccessibilityAction(AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS, null);



来源:https://stackoverflow.com/questions/51375401/fragment-not-getting-accessibility-focus-after-replace

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!