From dff14cadd78af3992dfd7c8c989314d11758554d Mon Sep 17 00:00:00 2001 From: Material Design Team Date: Thu, 12 May 2022 17:11:48 -0400 Subject: [PATCH] [AppBarLayout] Loosen check for scrollable child when adding a11y actions Adding the a11y delegate and a11y actions was dependent on there being a CoordinatorLayout child that both inherits from certain scrolling classes (NestedScrollingChild, ListView, ScrollView) and has an AppBarLayout scrolling behavior. This was too restrictive and so we only check for a behavior. Any scrolling child may add an a11y delegate or scroll action to the parent CoordinatorLayout. PiperOrigin-RevId: 448335380 (cherry picked from commit 4b81bb1d1a467d03b6f323639f5358ec14445c67) --- .../android/material/appbar/AppBarLayout.java | 30 +++++++++++++------ 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/lib/java/com/google/android/material/appbar/AppBarLayout.java b/lib/java/com/google/android/material/appbar/AppBarLayout.java index 3b60de07e17..778a8e29317 100644 --- a/lib/java/com/google/android/material/appbar/AppBarLayout.java +++ b/lib/java/com/google/android/material/appbar/AppBarLayout.java @@ -1732,17 +1732,14 @@ private void updateAccessibilityActions( CoordinatorLayout coordinatorLayout, @NonNull T appBarLayout) { ViewCompat.removeAccessibilityAction(coordinatorLayout, ACTION_SCROLL_FORWARD.getId()); ViewCompat.removeAccessibilityAction(coordinatorLayout, ACTION_SCROLL_BACKWARD.getId()); - View scrollingView = findFirstScrollingChild(coordinatorLayout); - // Don't add a11y actions if there is no scrolling view that the abl depends on for scrolling - // or the abl has no scroll range. - if (scrollingView == null || appBarLayout.getTotalScrollRange() == 0) { + // Don't add a11y actions if the abl has no scroll range. + if (appBarLayout.getTotalScrollRange() == 0) { return; } - // Don't add actions if the scrolling view doesn't have the behavior that will cause the abl - // to scroll. - CoordinatorLayout.LayoutParams lp = - (CoordinatorLayout.LayoutParams) scrollingView.getLayoutParams(); - if (!(lp.getBehavior() instanceof ScrollingViewBehavior)) { + // Don't add actions if a child view doesn't have the behavior that will cause the abl to + // scroll. + View scrollingView = getChildWithScrollingBehavior(coordinatorLayout); + if (scrollingView == null) { return; } @@ -1769,6 +1766,21 @@ public void onInitializeAccessibilityNodeInfo( addAccessibilityScrollActions(coordinatorLayout, appBarLayout, scrollingView); } + @Nullable + private View getChildWithScrollingBehavior(CoordinatorLayout coordinatorLayout) { + final int childCount = coordinatorLayout.getChildCount(); + for (int i = 0; i < childCount; i++) { + final View child = coordinatorLayout.getChildAt(i); + + CoordinatorLayout.LayoutParams lp = + (CoordinatorLayout.LayoutParams) child.getLayoutParams(); + if (lp.getBehavior() instanceof ScrollingViewBehavior) { + return child; + } + } + return null; + } + private boolean childrenHaveScrollFlags(AppBarLayout appBarLayout) { final int childCount = appBarLayout.getChildCount(); for (int i = 0; i < childCount; i++) {