Modify accessibility focus order

2019-04-05 00:25发布

Is it possible to change the accessibility focus order? For example, if I have 3 views side by side, with ids view1, view2, and view3, is there a simple way to make the accessibility focus go to view3 when the user swipes right from view1?

Here is what I've tried:

I have the following in a linear layout.

<ImageView
    android:layout_width="30dp"
    android:layout_height="30dp"
    android:id="@+id/imageView"
    android:focusable="true"
    android:nextFocusUp="@+id/imageView3"
    android:nextFocusDown="@+id/imageView3"
    android:nextFocusRight="@+id/imageView3"
    android:nextFocusLeft="@+id/imageView3"
    android:nextFocusForward="@+id/imageView3"
    android:src="@drawable/ic_launcher"/>

<ImageView
    android:layout_width="30dp"
    android:layout_height="30dp"
    android:id="@+id/imageView2"
    android:focusable="true"
    android:src="@drawable/ic_launcher"/>

<ImageView
    android:layout_width="30dp"
    android:layout_height="30dp"
    android:id="@+id/imageView3"
    android:focusable="true"
    android:nextFocusUp="@+id/imageView2"
    android:nextFocusDown="@+id/imageView2"
    android:nextFocusRight="@+id/imageView2"
    android:nextFocusLeft="@+id/imageView2"
    android:nextFocusForward="@+id/imageView2"
    android:src="@drawable/ic_launcher"/>

Then, I launch this and place accessibility focus on the first imageView. Upon swiping right to move to the next item, I expect it to move accessibility focus to imageView3, but instead it goes to imageView2.

1条回答
SAY GOODBYE
2楼-- · 2019-04-05 00:55

Looks like you can now set the TalkBack traversal order for API 22+! So the code would look something like this:

<ImageView
    android:layout_width="30dp"
    android:layout_height="30dp"
    android:id="@+id/imageView"
    android:focusable="true"
    android:src="@drawable/ic_launcher"/>

<ImageView
    android:layout_width="30dp"
    android:layout_height="30dp"
    android:id="@+id/imageView2"
    android:focusable="true"
    android:accessibilityTraversalAfter="@id/imageView3"
    android:src="@drawable/ic_launcher"/>

<ImageView
    android:layout_width="30dp"
    android:layout_height="30dp"
    android:id="@+id/imageView3"
    android:focusable="true"
    android:accessibilityTraversalAfter="@id/imageView"
    android:src="@drawable/ic_launcher"/>

http://developer.android.com/reference/android/R.attr.html#accessibilityTraversalAfter

accessibilityTraversalAfter: Sets the id of a view after which this one is visited in accessibility traversal.

查看更多
登录 后发表回答