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