ExploreByTouchHelper
abstract class ExploreByTouchHelper : AccessibilityDelegateCompat
| kotlin.Any | ||
| ↳ | androidx.core.view.AccessibilityDelegateCompat | |
| ↳ | androidx.customview.widget.ExploreByTouchHelper |
ExploreByTouchHelper is a utility class for implementing accessibility support in custom Views that represent a collection of View-like logical items. It extends AccessibilityNodeProviderCompat and simplifies many aspects of providing information to accessibility services and managing accessibility focus.
Clients should override abstract methods on this class and attach it to the host view using setAccessibilityDelegate.
The host view should also override the events in the following code snippet so that the view's logical items are detected properly by the framework:
class MyCustomView extends View { private MyExploreByTouchHelper mExploreByTouchHelper; public MyCustomView(Context context, ...) { ... mExploreByTouchHelper = new MyExploreByTouchHelper(this); ViewCompat.setAccessibilityDelegate(this, mExploreByTouchHelper); } @Override public boolean dispatchHoverEvent(MotionEvent event) { return mExploreByTouchHelper.dispatchHoverEvent(event) || super.dispatchHoverEvent(event); } @Override public boolean dispatchKeyEvent(KeyEvent event) { return mExploreByTouchHelper.dispatchKeyEvent(event) || super.dispatchKeyEvent(event); } @Override public void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) { super.onFocusChanged(gainFocus, direction, previouslyFocusedRect); mExploreByTouchHelper.onFocusChanged(gainFocus, direction, previouslyFocusedRect); } }
Summary
Constants |
|
|---|---|
const Int |
HOST_ID = -1Virtual node identifier value for the host view's node. |
const Int |
INVALID_ID = -2147483648Virtual node identifier value for invalid nodes. |
Public constructors |
|---|
ExploreByTouchHelper(host: View)Constructs a new helper that can expose a virtual view hierarchy for the specified host view. |
Public functions |
|
|---|---|
Boolean |
clearKeyboardFocusForVirtualView(virtualViewId: Int)Attempts to clear keyboard focus from a virtual view. |
Boolean |
dispatchHoverEvent(event: MotionEvent)Delegates hover events from the host view. |
Boolean |
dispatchKeyEvent(event: KeyEvent)Delegates key events from the host view. |
Int |
|
AccessibilityNodeProviderCompat! |
getAccessibilityNodeProvider(host: View!)Gets the provider for managing a virtual view hierarchy rooted at this View and reported to |
Int |
This function is deprecated. |
Int |
|
Unit |
Notifies the accessibility framework that the properties of the parent view have changed. |
Unit |
invalidateVirtualView(virtualViewId: Int)Notifies the accessibility framework that the properties of a particular item have changed. |
Unit |
invalidateVirtualView(virtualViewId: Int, changeTypes: Int)Notifies the accessibility framework that the properties of a particular item have changed. |
Unit |
onFocusChanged(Delegates focus changes from the host view. |
Unit |
onInitializeAccessibilityEvent(host: View!, event: AccessibilityEvent!)Initializes an |
Unit |
onInitializeAccessibilityNodeInfo(Initializes an |
Boolean |
requestKeyboardFocusForVirtualView(virtualViewId: Int)Attempts to give keyboard focus to a virtual view. |
Boolean |
sendEventForVirtualView(virtualViewId: Int, eventType: Int)Populates an event of the specified type with information about an item and attempts to send it up through the view hierarchy. |
Unit |
setBoundsInScreenFromBoundsInParent(Calculates and assigns screen-relative bounds based on bounds in parent. |
Protected functions |
|
|---|---|
abstract Int |
getVirtualViewAt(x: Float, y: Float)Provides a mapping between view-relative coordinates and logical items. |
abstract Unit |
getVisibleVirtualViews(virtualViewIds: (Mutable)List<Int!>!)Populates a list with the view's visible items. |
abstract Boolean |
onPerformActionForVirtualView(Performs the specified accessibility action on the item associated with the virtual view identifier. |
Unit |
Populates an |
Unit |
onPopulateEventForVirtualView(Populates an |
Unit |
Populates an |
abstract Unit |
onPopulateNodeForVirtualView(Populates an |
Unit |
onVirtualViewKeyboardFocusChanged(virtualViewId: Int, hasFocus: Boolean)Called when the focus state of a virtual view changes. |
Inherited functions |
||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
Constants
HOST_ID
const val HOST_ID = -1: Int
Virtual node identifier value for the host view's node.
INVALID_ID
const val INVALID_ID = -2147483648: Int
Virtual node identifier value for invalid nodes.
Public constructors
ExploreByTouchHelper
ExploreByTouchHelper(host: View)
Constructs a new helper that can expose a virtual view hierarchy for the specified host view.
| Parameters | |
|---|---|
host: View |
view whose virtual view hierarchy is exposed by this helper |
Public functions
clearKeyboardFocusForVirtualView
fun clearKeyboardFocusForVirtualView(virtualViewId: Int): Boolean
Attempts to clear keyboard focus from a virtual view.
| Parameters | |
|---|---|
virtualViewId: Int |
the identifier of the virtual view from which to clear keyboard focus |
| Returns | |
|---|---|
Boolean |
whether this virtual view actually cleared keyboard focus |
dispatchHoverEvent
fun dispatchHoverEvent(event: MotionEvent): Boolean
Delegates hover events from the host view.
Dispatches hover MotionEvents to the virtual view hierarchy when the Explore by Touch feature is enabled.
This method should be called by overriding the host view's dispatchHoverEvent method:
@Override public boolean dispatchHoverEvent(MotionEvent event) { return mHelper.dispatchHoverEvent(this, event) || super.dispatchHoverEvent(event); }
| Parameters | |
|---|---|
event: MotionEvent |
The hover event to dispatch to the virtual view hierarchy. |
| Returns | |
|---|---|
Boolean |
Whether the hover event was handled. |
dispatchKeyEvent
fun dispatchKeyEvent(event: KeyEvent): Boolean
Delegates key events from the host view.
This method should be called by overriding the host view's dispatchKeyEvent method:
@Override public boolean dispatchKeyEvent(KeyEvent event) { return mHelper.dispatchKeyEvent(event) || super.dispatchKeyEvent(event); }
getAccessibilityFocusedVirtualViewId
fun getAccessibilityFocusedVirtualViewId(): Int
| Returns | |
|---|---|
Int |
the identifier of the virtual view that has accessibility focus or |
getAccessibilityNodeProvider
fun getAccessibilityNodeProvider(host: View!): AccessibilityNodeProviderCompat!
Gets the provider for managing a virtual view hierarchy rooted at this View and reported to AccessibilityServices that explore the window content.
The default implementation behaves as ViewCompat#getAccessibilityNodeProvider(View) for the case of no accessibility delegate been set.
| Returns | |
|---|---|
AccessibilityNodeProviderCompat! |
The provider. |
| See also | |
|---|---|
AccessibilityNodeProviderCompat |
fungetFocusedVirtualView(): Int
Returns the virtual view ID for the currently accessibility focused item.
| Returns | |
|---|---|
Int |
the identifier of the virtual view that has accessibility focus or |
getKeyboardFocusedVirtualViewId
fun getKeyboardFocusedVirtualViewId(): Int
| Returns | |
|---|---|
Int |
the identifier of the virtual view that has keyboard focus or |
invalidateRoot
fun invalidateRoot(): Unit
Notifies the accessibility framework that the properties of the parent view have changed.
You must call this method after adding or removing items from the parent view.
invalidateVirtualView
fun invalidateVirtualView(virtualViewId: Int): Unit
Notifies the accessibility framework that the properties of a particular item have changed.
You must call this method after changing any of the properties set in onPopulateNodeForVirtualView.
| Parameters | |
|---|---|
virtualViewId: Int |
the virtual view id to invalidate, or |
| See also | |
|---|---|
invalidateVirtualView |
invalidateVirtualView
fun invalidateVirtualView(virtualViewId: Int, changeTypes: Int): Unit
Notifies the accessibility framework that the properties of a particular item have changed.
You must call this method after changing any of the properties set in onPopulateNodeForVirtualView.
onFocusChanged
fun onFocusChanged(
gainFocus: Boolean,
direction: Int,
previouslyFocusedRect: Rect?
): Unit
Delegates focus changes from the host view.
This method should be called by overriding the host view's onFocusChanged method:
@Override public boolean onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) { super.onFocusChanged(gainFocus, direction, previouslyFocusedRect); mHelper.onFocusChanged(gainFocus, direction, previouslyFocusedRect); }
onInitializeAccessibilityEvent
fun onInitializeAccessibilityEvent(host: View!, event: AccessibilityEvent!): Unit
Initializes an AccessibilityEvent with information about the the host View which is the event source.
The default implementation behaves as ViewCompat#onInitalizeAccessibilityEvent(View v, AccessibilityEvent event) for the case of no accessibility delegate been set.
| Parameters | |
|---|---|
host: View! |
The View hosting the delegate. |
event: AccessibilityEvent! |
The event to initialize. |
| See also | |
|---|---|
onInitializeAccessibilityEvent |
ViewCompat#onInitializeAccessibilityEvent(View, AccessibilityEvent) |
onInitializeAccessibilityNodeInfo
fun onInitializeAccessibilityNodeInfo(
host: View!,
info: AccessibilityNodeInfoCompat!
): Unit
Initializes an AccessibilityNodeInfoCompat with information about the host view.
The default implementation behaves as ViewCompat#onInitializeAccessibilityNodeInfo(View, AccessibilityNodeInfoCompat) for the case of no accessibility delegate been set.
| Parameters | |
|---|---|
host: View! |
The View hosting the delegate. |
info: AccessibilityNodeInfoCompat! |
The instance to initialize. |
| See also | |
|---|---|
onInitializeAccessibilityNodeInfo |
ViewCompat#onInitializeAccessibilityNodeInfo(View, AccessibilityNodeInfoCompat) |
requestKeyboardFocusForVirtualView
fun requestKeyboardFocusForVirtualView(virtualViewId: Int): Boolean
Attempts to give keyboard focus to a virtual view.
| Parameters | |
|---|---|
virtualViewId: Int |
the identifier of the virtual view on which to place keyboard focus |
| Returns | |
|---|---|
Boolean |
whether this virtual view actually took keyboard focus |
sendEventForVirtualView
fun sendEventForVirtualView(virtualViewId: Int, eventType: Int): Boolean
Populates an event of the specified type with information about an item and attempts to send it up through the view hierarchy.
You should call this method after performing a user action that normally fires an accessibility event, such as clicking on an item.
public void performItemClick(T item) { ... sendEventForVirtualViewId(item.id, AccessibilityEvent.TYPE_VIEW_CLICKED); }
| Parameters | |
|---|---|
virtualViewId: Int |
the identifier of the virtual view for which to send an event |
eventType: Int |
the type of event to send |
| Returns | |
|---|---|
Boolean |
|
setBoundsInScreenFromBoundsInParent
fun setBoundsInScreenFromBoundsInParent(
node: AccessibilityNodeInfoCompat,
boundsInParent: Rect
): Unit
Calculates and assigns screen-relative bounds based on bounds in parent. Instead of calling the deprecated setBoundsInParent, it provides a convenient method to calculate and assign setBoundsInScreen based on boundsInParent.
| Parameters | |
|---|---|
node: AccessibilityNodeInfoCompat |
The node to populate |
boundsInParent: Rect |
The node bounds in the viewParent's coordinates. |
Protected functions
getVirtualViewAt
protected abstract fun getVirtualViewAt(x: Float, y: Float): Int
Provides a mapping between view-relative coordinates and logical items.
getVisibleVirtualViews
protected abstract fun getVisibleVirtualViews(virtualViewIds: (Mutable)List<Int!>!): Unit
Populates a list with the view's visible items. The ordering of items within virtualViewIds specifies order of accessibility focus traversal.
onPerformActionForVirtualView
protected abstract fun onPerformActionForVirtualView(
virtualViewId: Int,
action: Int,
arguments: Bundle?
): Boolean
Performs the specified accessibility action on the item associated with the virtual view identifier. See performAction for more information.
Implementations must handle any actions added manually in onPopulateNodeForVirtualView.
The helper class automatically handles focus management resulting from ACTION_ACCESSIBILITY_FOCUS and ACTION_CLEAR_ACCESSIBILITY_FOCUS actions.
| Parameters | |
|---|---|
virtualViewId: Int |
The virtual view identifier of the item on which to perform the action |
action: Int |
The accessibility action to perform |
arguments: Bundle? |
(Optional) A bundle with additional arguments, or null |
| Returns | |
|---|---|
Boolean |
true if the action was performed |
onPopulateEventForHost
protected fun onPopulateEventForHost(event: AccessibilityEvent): Unit
Populates an AccessibilityEvent with information about the host view.
The default implementation is a no-op.
| Parameters | |
|---|---|
event: AccessibilityEvent |
the event to populate with information about the host view |
onPopulateEventForVirtualView
protected fun onPopulateEventForVirtualView(
virtualViewId: Int,
event: AccessibilityEvent
): Unit
Populates an AccessibilityEvent with information about the specified item.
The helper class automatically populates the following fields based on the values set by onPopulateNodeForVirtualView, but implementations may optionally override them:
- event text, see
getText - content description, see
setContentDescription - scrollability, see
setScrollable - password state, see
setPassword - enabled state, see
setEnabled - checked state, see
setChecked
The following required fields are automatically populated by the helper class and may not be overridden:
- item class name, set to the value used in
onPopulateNodeForVirtualView - package name, set to the package of the host view's
Context, seesetPackageName - event source, set to the host view and virtual view identifier, see
setSource
| Parameters | |
|---|---|
virtualViewId: Int |
The virtual view id for the item for which to populate the event |
event: AccessibilityEvent |
The event to populate |
onPopulateNodeForHost
protected fun onPopulateNodeForHost(node: AccessibilityNodeInfoCompat): Unit
Populates an AccessibilityNodeInfoCompat with information about the host view.
The default implementation is a no-op.
| Parameters | |
|---|---|
node: AccessibilityNodeInfoCompat |
the node to populate with information about the host view |
onPopulateNodeForVirtualView
protected abstract fun onPopulateNodeForVirtualView(
virtualViewId: Int,
node: AccessibilityNodeInfoCompat
): Unit
Populates an AccessibilityNodeInfoCompat with information about the specified item.
Implementations must populate the following required fields:
- event text, see
setTextorsetContentDescription - bounds in screen coordinates, see
setBoundsInScreenand(AccessibilityNodeInfoCompat, Rect)
The helper class automatically populates the following fields with default values, but implementations may optionally override them:
- enabled state, set to
true, seesetEnabled - keyboard focusability, set to
true, seesetFocusable - item class name, set to
android.view.View, seesetClassName
The following required fields are automatically populated by the helper class and may not be overridden:
- package name, identical to the package name set by
onPopulateEventForVirtualView, seesetPackageName - node source, identical to the event source set in
onPopulateEventForVirtualView, seesetSource - parent view, set to the host view, see
setParent - visibility, computed based on parent-relative bounds, see
setVisibleToUser - accessibility focus, computed based on internal helper state, see
setAccessibilityFocused - keyboard focus, computed based on internal helper state, see
setFocused
Additionally, the helper class automatically handles keyboard focus and accessibility focus management by adding the appropriate ACTION_FOCUS, ACTION_CLEAR_FOCUS, ACTION_ACCESSIBILITY_FOCUS, or ACTION_CLEAR_ACCESSIBILITY_FOCUS actions. Implementations must never manually add these actions.
The helper class also automatically modifies parent- and screen-relative bounds to reflect the portion of the item visible within its parent.
| Parameters | |
|---|---|
virtualViewId: Int |
The virtual view identifier of the item for which to populate the node |
node: AccessibilityNodeInfoCompat |
The node to populate |
onVirtualViewKeyboardFocusChanged
protected fun onVirtualViewKeyboardFocusChanged(virtualViewId: Int, hasFocus: Boolean): Unit
Called when the focus state of a virtual view changes.