• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 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 android.support.v7.widget;
18 
19 import android.os.Bundle;
20 import android.support.v4.view.AccessibilityDelegateCompat;
21 import android.support.v4.view.accessibility.AccessibilityNodeInfoCompat;
22 import android.view.View;
23 import android.view.accessibility.AccessibilityEvent;
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 AccessibilityDelegateCompat {
31     final RecyclerView mRecyclerView;
32     final AccessibilityDelegateCompat mItemDelegate;
33 
34 
RecyclerViewAccessibilityDelegate(RecyclerView recyclerView)35     public RecyclerViewAccessibilityDelegate(RecyclerView recyclerView) {
36         mRecyclerView = recyclerView;
37         mItemDelegate = new ItemDelegate(this);
38     }
39 
shouldIgnore()40     boolean shouldIgnore() {
41         return mRecyclerView.hasPendingAdapterUpdates();
42     }
43 
44     @Override
performAccessibilityAction(View host, int action, Bundle args)45     public boolean performAccessibilityAction(View host, int action, Bundle args) {
46         if (super.performAccessibilityAction(host, action, args)) {
47             return true;
48         }
49         if (!shouldIgnore() && mRecyclerView.getLayoutManager() != null) {
50             return mRecyclerView.getLayoutManager().performAccessibilityAction(action, args);
51         }
52 
53         return false;
54     }
55 
56     @Override
onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfoCompat info)57     public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfoCompat info) {
58         super.onInitializeAccessibilityNodeInfo(host, info);
59         info.setClassName(RecyclerView.class.getName());
60         if (!shouldIgnore() && mRecyclerView.getLayoutManager() != null) {
61             mRecyclerView.getLayoutManager().onInitializeAccessibilityNodeInfo(info);
62         }
63     }
64 
65     @Override
onInitializeAccessibilityEvent(View host, AccessibilityEvent event)66     public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
67         super.onInitializeAccessibilityEvent(host, event);
68         event.setClassName(RecyclerView.class.getName());
69         if (host instanceof RecyclerView && !shouldIgnore()) {
70             RecyclerView rv = (RecyclerView) host;
71             if (rv.getLayoutManager() != null) {
72                 rv.getLayoutManager().onInitializeAccessibilityEvent(event);
73             }
74         }
75     }
76 
77     /**
78      * Gets the AccessibilityDelegate for an individual item in the RecyclerView.
79      * A basic item delegate is provided by default, but you can override this
80      * method to provide a custom per-item delegate.
81      */
getItemDelegate()82     public AccessibilityDelegateCompat getItemDelegate() {
83         return mItemDelegate;
84     }
85 
86     /**
87      * The default implementation of accessibility delegate for the individual items of the
88      * RecyclerView.
89      * <p>
90      * If you are overriding {@code RecyclerViewAccessibilityDelegate#getItemDelegate()} but still
91      * want to keep some default behavior, you can create an instance of this class and delegate to
92      * the parent as necessary.
93      */
94     public static class ItemDelegate extends AccessibilityDelegateCompat {
95         final RecyclerViewAccessibilityDelegate mRecyclerViewDelegate;
96 
97         /**
98          * Creates an item delegate for the given {@code RecyclerViewAccessibilityDelegate}.
99          *
100          * @param recyclerViewDelegate The parent RecyclerView's accessibility delegate.
101          */
ItemDelegate(RecyclerViewAccessibilityDelegate recyclerViewDelegate)102         public ItemDelegate(RecyclerViewAccessibilityDelegate recyclerViewDelegate) {
103             mRecyclerViewDelegate = recyclerViewDelegate;
104         }
105 
106         @Override
onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfoCompat info)107         public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfoCompat info) {
108             super.onInitializeAccessibilityNodeInfo(host, info);
109             if (!mRecyclerViewDelegate.shouldIgnore()
110                     && mRecyclerViewDelegate.mRecyclerView.getLayoutManager() != null) {
111                 mRecyclerViewDelegate.mRecyclerView.getLayoutManager()
112                         .onInitializeAccessibilityNodeInfoForItem(host, info);
113             }
114         }
115 
116         @Override
performAccessibilityAction(View host, int action, Bundle args)117         public boolean performAccessibilityAction(View host, int action, Bundle args) {
118             if (super.performAccessibilityAction(host, action, args)) {
119                 return true;
120             }
121             if (!mRecyclerViewDelegate.shouldIgnore()
122                     && mRecyclerViewDelegate.mRecyclerView.getLayoutManager() != null) {
123                 return mRecyclerViewDelegate.mRecyclerView.getLayoutManager()
124                         .performAccessibilityActionForItem(host, action, args);
125             }
126             return false;
127         }
128     }
129 }
130 
131