下面列出了android.support.v4.view.ViewCompat#IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Returns whether the supplied {@link View} and {@link AccessibilityNodeInfoCompat} would
* produce spoken feedback if it were accessibility focused. NOTE: not all speaking nodes are
* focusable.
*
* @param view The {@link View} to evaluate
* @param node The {@link AccessibilityNodeInfoCompat} to evaluate
* @return {@code true} if it meets the criterion for producing spoken feedback
*/
public static boolean isSpeakingNode(
@Nullable AccessibilityNodeInfoCompat node,
@Nullable View view) {
if (node == null || view == null) {
return false;
}
if (!node.isVisibleToUser()) {
return false;
}
int important = ViewCompat.getImportantForAccessibility(view);
if (important == ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS ||
(important == ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_NO &&
node.getChildCount() <= 0)) {
return false;
}
return node.isCheckable() || hasText(node) || hasNonActionableSpeakingDescendants(node, view);
}
private static boolean includeChildForAccessibility(View child) {
// If the child is not important for accessibility we make
// sure this hides the entire subtree rooted at it as the
// IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDATS is not
// supported on older platforms but we want to hide the entire
// content and not opened drawers if a drawer is opened.
return ViewCompat.getImportantForAccessibility(child)
!= ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS
&& ViewCompat.getImportantForAccessibility(child)
!= ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_NO;
}
public static String getIgnoredReasons(View view) {
int important = ViewCompat.getImportantForAccessibility(view);
if (important == ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_NO) {
return "View has importantForAccessibility set to 'NO'.";
}
if (important == ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS) {
return "View has importantForAccessibility set to 'NO_HIDE_DESCENDANTS'.";
}
ViewParent parent = view.getParent();
while (parent instanceof View) {
if (ViewCompat.getImportantForAccessibility((View) parent)
== ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS) {
return "An ancestor View has importantForAccessibility set to 'NO_HIDE_DESCENDANTS'.";
}
parent = parent.getParent();
}
AccessibilityNodeInfoCompat node = createNodeInfoFromView(view);
try {
if (!node.isVisibleToUser()) {
return "View is not visible.";
}
if (AccessibilityUtil.isAccessibilityFocusable(node, view)) {
return "View is actionable, but has no description.";
}
if (AccessibilityUtil.hasText(node)) {
return "View is not actionable, and an ancestor View has co-opted its description.";
}
return "View is not actionable and has no description.";
} finally {
node.recycle();
}
}
private static boolean includeChildForAccessibility(View child) {
// If the child is not important for accessibility we make
// sure this hides the entire subtree rooted at it as the
// IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDATS is not
// supported on older platforms but we want to hide the entire
// content and not opened drawers if a drawer is opened.
return ViewCompat.getImportantForAccessibility(child)
!= ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS
&& ViewCompat.getImportantForAccessibility(child)
!= ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_NO;
}
private void addChildrenForAccessibility(AccessibilityNodeInfoCompat info, ViewGroup v) {
final int childCount = v.getChildCount();
for (int i = 0; i < childCount; i++) {
final View child = v.getChildAt(i);
if (filter(child)) {
continue;
}
// Adding children that are marked as not important for
// accessibility will break the hierarchy, so we need to check
// that value and re-parent views if necessary.
final int importance = ViewCompat.getImportantForAccessibility(child);
switch (importance) {
case ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS:
// Always skip NO_HIDE views and their descendants.
break;
case ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_NO:
// Re-parent children of NO view groups, skip NO views.
if (child instanceof ViewGroup) {
addChildrenForAccessibility(info, (ViewGroup) child);
}
break;
case ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_AUTO:
// Force AUTO views to YES and add them.
ViewCompat.setImportantForAccessibility(
child, ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_YES);
case ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_YES:
info.addChild(child);
break;
}
}
}
private void addChildrenForAccessibility(AccessibilityNodeInfoCompat info, ViewGroup v) {
final int childCount = v.getChildCount();
for (int i = 0; i < childCount; i++) {
final View child = v.getChildAt(i);
if (filter(child)) {
continue;
}
// Adding children that are marked as not important for
// accessibility will break the hierarchy, so we need to check
// that value and re-parent views if necessary.
final int importance = ViewCompat.getImportantForAccessibility(child);
switch (importance) {
case ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS:
// Always skip NO_HIDE views and their descendants.
break;
case ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_NO:
// Re-parent children of NO view groups, skip NO views.
if (child instanceof ViewGroup) {
addChildrenForAccessibility(info, (ViewGroup) child);
}
break;
case ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_AUTO:
// Force AUTO views to YES and add them.
ViewCompat.setImportantForAccessibility(
child, ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_YES);
case ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_YES:
info.addChild(child);
break;
}
}
}
private void addChildrenForAccessibility(AccessibilityNodeInfoCompat info, ViewGroup v) {
final int childCount = v.getChildCount();
for (int i = 0; i < childCount; i++) {
final View child = v.getChildAt(i);
if (filter(child)) {
continue;
}
// Adding children that are marked as not important for
// accessibility will break the hierarchy, so we need to check
// that value and re-parent views if necessary.
final int importance = ViewCompat.getImportantForAccessibility(child);
switch (importance) {
case ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS:
// Always skip NO_HIDE views and their descendants.
break;
case ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_NO:
// Re-parent children of NO view groups, skip NO views.
if (child instanceof ViewGroup) {
addChildrenForAccessibility(info, (ViewGroup) child);
}
break;
case ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_AUTO:
// Force AUTO views to YES and add them.
ViewCompat.setImportantForAccessibility(
child, ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_YES);
case ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_YES:
info.addChild(child);
break;
}
}
}
private void addChildrenForAccessibility(AccessibilityNodeInfoCompat info, ViewGroup v) {
final int childCount = v.getChildCount();
for (int i = 0; i < childCount; i++) {
final View child = v.getChildAt(i);
if (filter(child)) {
continue;
}
// Adding children that are marked as not important for
// accessibility will break the hierarchy, so we need to check
// that value and re-parent views if necessary.
final int importance = ViewCompat.getImportantForAccessibility(child);
switch (importance) {
case ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS:
// Always skip NO_HIDE views and their descendants.
break;
case ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_NO:
// Re-parent children of NO view groups, skip NO views.
if (child instanceof ViewGroup) {
addChildrenForAccessibility(info, (ViewGroup) child);
}
break;
case ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_AUTO:
// Force AUTO views to YES and add them.
ViewCompat.setImportantForAccessibility(
child, ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_YES);
case ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_YES:
info.addChild(child);
break;
}
}
}
private static boolean includeChildForAccessibility(View child) {
// If the child is not important for accessibility we make
// sure this hides the entire subtree rooted at it as the
// IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDATS is not
// supported on older platforms but we want to hide the entire
// content and not opened drawers if a drawer is opened.
return ViewCompat.getImportantForAccessibility(child)
!= ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS
&& ViewCompat.getImportantForAccessibility(child)
!= ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_NO;
}
public static boolean getIgnored(View view) {
int important = ViewCompat.getImportantForAccessibility(view);
if (important == ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_NO ||
important == ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS) {
return true;
}
// Go all the way up the tree to make sure no parent has hidden its descendants
ViewParent parent = view.getParent();
while (parent instanceof View) {
if (ViewCompat.getImportantForAccessibility((View) parent)
== ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS) {
return true;
}
parent = parent.getParent();
}
AccessibilityNodeInfoCompat node = createNodeInfoFromView(view);
try {
if (!node.isVisibleToUser()) {
return true;
}
if (AccessibilityUtil.isAccessibilityFocusable(node, view)) {
if (node.getChildCount() <= 0) {
// Leaves that are accessibility focusable are never ignored, even if they don't have a
// speakable description
return false;
} else if (AccessibilityUtil.isSpeakingNode(node, view)) {
// Node is focusable and has something to speak
return false;
}
// Node is focusable and has nothing to speak
return true;
}
// If this node has no focusable ancestors, but it still has text,
// then it should receive focus from navigation and be read aloud.
if (!AccessibilityUtil.hasFocusableAncestor(node, view) && AccessibilityUtil.hasText(node)) {
return false;
}
return true;
} finally {
node.recycle();
}
}
private void updateAccessibilityFlags() {
int accessible = mState == State.NORMAL ?
ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_YES :
ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS;
setImportantForAccessibility(accessible);
}
private void updateAccessibilityFlags() {
int accessible = mState == State.NORMAL ?
ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_YES :
ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS;
setImportantForAccessibility(accessible);
}