[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)
This commit is contained in:
Material Design Team 2022-05-12 17:11:48 -04:00 committed by haodong
parent a5a738bfa2
commit dff14cadd7

View File

@ -1732,17 +1732,14 @@ public class AppBarLayout extends LinearLayout implements CoordinatorLayout.Atta
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 class AppBarLayout extends LinearLayout implements CoordinatorLayout.Atta
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++) {