Skip to content

Commit

Permalink
[AppBarLayout] Loosen check for scrollable child when adding a11y act…
Browse files Browse the repository at this point in the history
…ions

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 4b81bb1)
  • Loading branch information
Material Design Team authored and pekingme committed May 25, 2022
1 parent a5a738b commit dff14ca
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions lib/java/com/google/android/material/appbar/AppBarLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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++) {
Expand Down

0 comments on commit dff14ca

Please sign in to comment.