1 /*
2  * Copyright (C) 2015 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.preference;
18 
19 import android.support.annotation.IdRes;
20 import android.support.annotation.RestrictTo;
21 import android.support.v7.widget.RecyclerView;
22 import android.util.SparseArray;
23 import android.view.View;
24 
25 /**
26  * A {@link android.support.v7.widget.RecyclerView.ViewHolder} class which caches views associated
27  * with the default {@link Preference} layouts. Cached views can be retrieved by calling
28  * {@link #findViewById(int)}.
29  */
30 public class PreferenceViewHolder extends RecyclerView.ViewHolder {
31     private final SparseArray<View> mCachedViews = new SparseArray<>(4);
32     private boolean mDividerAllowedAbove;
33     private boolean mDividerAllowedBelow;
34 
PreferenceViewHolder(View itemView)35     /* package */ PreferenceViewHolder(View itemView) {
36         super(itemView);
37 
38         // Pre-cache the views that we know in advance we'll want to find
39         mCachedViews.put(android.R.id.title, itemView.findViewById(android.R.id.title));
40         mCachedViews.put(android.R.id.summary, itemView.findViewById(android.R.id.summary));
41         mCachedViews.put(android.R.id.icon, itemView.findViewById(android.R.id.icon));
42         mCachedViews.put(R.id.icon_frame, itemView.findViewById(R.id.icon_frame));
43         mCachedViews.put(AndroidResources.ANDROID_R_ICON_FRAME,
44                 itemView.findViewById(AndroidResources.ANDROID_R_ICON_FRAME));
45     }
46 
47     /** @hide */
48     @RestrictTo(RestrictTo.Scope.TESTS)
createInstanceForTests(View itemView)49     public static PreferenceViewHolder createInstanceForTests(View itemView) {
50         return new PreferenceViewHolder(itemView);
51     }
52 
53     /**
54      * Returns a cached reference to a subview managed by this object. If the view reference is not
55      * yet cached, it falls back to calling {@link View#findViewById(int)} and caches the result.
56      *
57      * @param id Resource ID of the view to find
58      * @return The view, or null if no view with the requested ID is found.
59      */
findViewById(@dRes int id)60     public View findViewById(@IdRes int id) {
61         final View cachedView = mCachedViews.get(id);
62         if (cachedView != null) {
63             return cachedView;
64         } else {
65             final View v = itemView.findViewById(id);
66             if (v != null) {
67                 mCachedViews.put(id, v);
68             }
69             return v;
70         }
71     }
72 
73     /**
74      * Dividers are only drawn between items if both items allow it, or above the first and below
75      * the last item if that item allows it.
76      *
77      * @return true if dividers are allowed above this item
78      */
isDividerAllowedAbove()79     public boolean isDividerAllowedAbove() {
80         return mDividerAllowedAbove;
81     }
82 
83     /**
84      * Dividers are only drawn between items if both items allow it, or above the first and below
85      * the last item if that item allows it.
86      *
87      * By default, {@link Preference#onBindViewHolder(PreferenceViewHolder)} will set this to the
88      * same value as returned by {@link Preference#isSelectable()}, so that non-selectable items
89      * do not have a divider drawn above them.
90      *
91      * @param allowed false to prevent dividers being drawn above this item
92      */
setDividerAllowedAbove(boolean allowed)93     public void setDividerAllowedAbove(boolean allowed) {
94         mDividerAllowedAbove = allowed;
95     }
96 
97     /**
98      * Dividers are only drawn between items if both items allow it, or above the first and below
99      * the last item if that item allows it.
100      *
101      * @return true if dividers are allowed below this item
102      */
isDividerAllowedBelow()103     public boolean isDividerAllowedBelow() {
104         return mDividerAllowedBelow;
105     }
106 
107     /**
108      * Dividers are only drawn between items if both items allow it, or above the first and below
109      * the last item if that item allows it.
110      *
111      * By default, {@link Preference#onBindViewHolder(PreferenceViewHolder)} will set this to the
112      * same value as returned by {@link Preference#isSelectable()}, so that non-selectable items
113      * do not have a divider drawn below them.
114      *
115      * @param allowed false to prevent dividers being drawn below this item
116      */
setDividerAllowedBelow(boolean allowed)117     public void setDividerAllowedBelow(boolean allowed) {
118         mDividerAllowedBelow = allowed;
119     }
120 }
121