1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.internal.widget; 18 19 import android.os.Bundle; 20 import android.view.View; 21 import android.view.View.AccessibilityDelegate; 22 import android.view.accessibility.AccessibilityEvent; 23 import android.view.accessibility.AccessibilityNodeInfo; 24 25 /** 26 * The AccessibilityDelegate used by RecyclerView. 27 * <p> 28 * This class handles basic accessibility actions and delegates them to LayoutManager. 29 */ 30 public class RecyclerViewAccessibilityDelegate extends AccessibilityDelegate { 31 final RecyclerView mRecyclerView; 32 33 RecyclerViewAccessibilityDelegate(RecyclerView recyclerView)34 public RecyclerViewAccessibilityDelegate(RecyclerView recyclerView) { 35 mRecyclerView = recyclerView; 36 } 37 shouldIgnore()38 boolean shouldIgnore() { 39 return mRecyclerView.hasPendingAdapterUpdates(); 40 } 41 42 @Override performAccessibilityAction(View host, int action, Bundle args)43 public boolean performAccessibilityAction(View host, int action, Bundle args) { 44 if (super.performAccessibilityAction(host, action, args)) { 45 return true; 46 } 47 if (!shouldIgnore() && mRecyclerView.getLayoutManager() != null) { 48 return mRecyclerView.getLayoutManager().performAccessibilityAction(action, args); 49 } 50 51 return false; 52 } 53 54 @Override onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info)55 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) { 56 super.onInitializeAccessibilityNodeInfo(host, info); 57 info.setClassName(RecyclerView.class.getName()); 58 if (!shouldIgnore() && mRecyclerView.getLayoutManager() != null) { 59 mRecyclerView.getLayoutManager().onInitializeAccessibilityNodeInfo(info); 60 } 61 } 62 63 @Override onInitializeAccessibilityEvent(View host, AccessibilityEvent event)64 public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) { 65 super.onInitializeAccessibilityEvent(host, event); 66 event.setClassName(RecyclerView.class.getName()); 67 if (host instanceof RecyclerView && !shouldIgnore()) { 68 RecyclerView rv = (RecyclerView) host; 69 if (rv.getLayoutManager() != null) { 70 rv.getLayoutManager().onInitializeAccessibilityEvent(event); 71 } 72 } 73 } 74 75 /** 76 * Gets the AccessibilityDelegate for an individual item in the RecyclerView. 77 * A basic item delegate is provided by default, but you can override this 78 * method to provide a custom per-item delegate. 79 */ getItemDelegate()80 public AccessibilityDelegate getItemDelegate() { 81 return mItemDelegate; 82 } 83 84 final AccessibilityDelegate mItemDelegate = new AccessibilityDelegate() { 85 @Override 86 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) { 87 super.onInitializeAccessibilityNodeInfo(host, info); 88 if (!shouldIgnore() && mRecyclerView.getLayoutManager() != null) { 89 mRecyclerView.getLayoutManager() 90 .onInitializeAccessibilityNodeInfoForItem(host, info); 91 } 92 } 93 94 @Override 95 public boolean performAccessibilityAction(View host, int action, Bundle args) { 96 if (super.performAccessibilityAction(host, action, args)) { 97 return true; 98 } 99 if (!shouldIgnore() && mRecyclerView.getLayoutManager() != null) { 100 return mRecyclerView.getLayoutManager() 101 .performAccessibilityActionForItem(host, action, args); 102 } 103 return false; 104 } 105 }; 106 } 107 108