16904f67c96a28a0e5966b4fb6d37a0ad5f136858Tony Mantler/* 26904f67c96a28a0e5966b4fb6d37a0ad5f136858Tony Mantler * Copyright (C) 2015 The Android Open Source Project 36904f67c96a28a0e5966b4fb6d37a0ad5f136858Tony Mantler * 46904f67c96a28a0e5966b4fb6d37a0ad5f136858Tony Mantler * Licensed under the Apache License, Version 2.0 (the "License"); 56904f67c96a28a0e5966b4fb6d37a0ad5f136858Tony Mantler * you may not use this file except in compliance with the License. 66904f67c96a28a0e5966b4fb6d37a0ad5f136858Tony Mantler * You may obtain a copy of the License at 76904f67c96a28a0e5966b4fb6d37a0ad5f136858Tony Mantler * 86904f67c96a28a0e5966b4fb6d37a0ad5f136858Tony Mantler * http://www.apache.org/licenses/LICENSE-2.0 96904f67c96a28a0e5966b4fb6d37a0ad5f136858Tony Mantler * 106904f67c96a28a0e5966b4fb6d37a0ad5f136858Tony Mantler * Unless required by applicable law or agreed to in writing, software 116904f67c96a28a0e5966b4fb6d37a0ad5f136858Tony Mantler * distributed under the License is distributed on an "AS IS" BASIS, 126904f67c96a28a0e5966b4fb6d37a0ad5f136858Tony Mantler * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 136904f67c96a28a0e5966b4fb6d37a0ad5f136858Tony Mantler * See the License for the specific language governing permissions and 146904f67c96a28a0e5966b4fb6d37a0ad5f136858Tony Mantler * limitations under the License 156904f67c96a28a0e5966b4fb6d37a0ad5f136858Tony Mantler */ 166904f67c96a28a0e5966b4fb6d37a0ad5f136858Tony Mantler 176904f67c96a28a0e5966b4fb6d37a0ad5f136858Tony Mantlerpackage android.support.v7.preference; 186904f67c96a28a0e5966b4fb6d37a0ad5f136858Tony Mantler 196904f67c96a28a0e5966b4fb6d37a0ad5f136858Tony Mantlerimport android.support.annotation.IdRes; 206904f67c96a28a0e5966b4fb6d37a0ad5f136858Tony Mantlerimport android.support.v7.widget.RecyclerView; 216904f67c96a28a0e5966b4fb6d37a0ad5f136858Tony Mantlerimport android.util.SparseArray; 226904f67c96a28a0e5966b4fb6d37a0ad5f136858Tony Mantlerimport android.view.View; 236904f67c96a28a0e5966b4fb6d37a0ad5f136858Tony Mantler 246904f67c96a28a0e5966b4fb6d37a0ad5f136858Tony Mantler/** 256904f67c96a28a0e5966b4fb6d37a0ad5f136858Tony Mantler * A {@link android.support.v7.widget.RecyclerView.ViewHolder} class which caches views associated 266904f67c96a28a0e5966b4fb6d37a0ad5f136858Tony Mantler * with the default {@link Preference} layouts. Cached views can be retrieved by calling 276904f67c96a28a0e5966b4fb6d37a0ad5f136858Tony Mantler * {@link #findViewById(int)}. 286904f67c96a28a0e5966b4fb6d37a0ad5f136858Tony Mantler */ 296904f67c96a28a0e5966b4fb6d37a0ad5f136858Tony Mantlerpublic class PreferenceViewHolder extends RecyclerView.ViewHolder { 306904f67c96a28a0e5966b4fb6d37a0ad5f136858Tony Mantler private final SparseArray<View> mCachedViews = new SparseArray<>(4); 31913cb9a1d85f1cf77430312e91e9a2ceeb553e9dTony Mantler private boolean mDividerAllowedAbove; 32913cb9a1d85f1cf77430312e91e9a2ceeb553e9dTony Mantler private boolean mDividerAllowedBelow; 336904f67c96a28a0e5966b4fb6d37a0ad5f136858Tony Mantler 346904f67c96a28a0e5966b4fb6d37a0ad5f136858Tony Mantler /* package */ PreferenceViewHolder(View itemView) { 356904f67c96a28a0e5966b4fb6d37a0ad5f136858Tony Mantler super(itemView); 366904f67c96a28a0e5966b4fb6d37a0ad5f136858Tony Mantler 376904f67c96a28a0e5966b4fb6d37a0ad5f136858Tony Mantler // Pre-cache the views that we know in advance we'll want to find 386904f67c96a28a0e5966b4fb6d37a0ad5f136858Tony Mantler mCachedViews.put(android.R.id.title, itemView.findViewById(android.R.id.title)); 396904f67c96a28a0e5966b4fb6d37a0ad5f136858Tony Mantler mCachedViews.put(android.R.id.summary, itemView.findViewById(android.R.id.summary)); 406904f67c96a28a0e5966b4fb6d37a0ad5f136858Tony Mantler mCachedViews.put(android.R.id.icon, itemView.findViewById(android.R.id.icon)); 416904f67c96a28a0e5966b4fb6d37a0ad5f136858Tony Mantler mCachedViews.put(R.id.icon_frame, itemView.findViewById(R.id.icon_frame)); 423fadd62b614e4a69aefe920aac640bdb629e502eJason Monk mCachedViews.put(AndroidResources.ANDROID_R_ICON_FRAME, 433fadd62b614e4a69aefe920aac640bdb629e502eJason Monk itemView.findViewById(AndroidResources.ANDROID_R_ICON_FRAME)); 446904f67c96a28a0e5966b4fb6d37a0ad5f136858Tony Mantler } 456904f67c96a28a0e5966b4fb6d37a0ad5f136858Tony Mantler 466904f67c96a28a0e5966b4fb6d37a0ad5f136858Tony Mantler /** 476904f67c96a28a0e5966b4fb6d37a0ad5f136858Tony Mantler * Returns a cached reference to a subview managed by this object. If the view reference is not 486904f67c96a28a0e5966b4fb6d37a0ad5f136858Tony Mantler * yet cached, it falls back to calling {@link View#findViewById(int)} and caches the result. 496904f67c96a28a0e5966b4fb6d37a0ad5f136858Tony Mantler * 506904f67c96a28a0e5966b4fb6d37a0ad5f136858Tony Mantler * @param id Resource ID of the view to find 516904f67c96a28a0e5966b4fb6d37a0ad5f136858Tony Mantler * @return The view, or null if no view with the requested ID is found. 526904f67c96a28a0e5966b4fb6d37a0ad5f136858Tony Mantler */ 536904f67c96a28a0e5966b4fb6d37a0ad5f136858Tony Mantler public View findViewById(@IdRes int id) { 546904f67c96a28a0e5966b4fb6d37a0ad5f136858Tony Mantler final View cachedView = mCachedViews.get(id); 556904f67c96a28a0e5966b4fb6d37a0ad5f136858Tony Mantler if (cachedView != null) { 566904f67c96a28a0e5966b4fb6d37a0ad5f136858Tony Mantler return cachedView; 576904f67c96a28a0e5966b4fb6d37a0ad5f136858Tony Mantler } else { 586904f67c96a28a0e5966b4fb6d37a0ad5f136858Tony Mantler final View v = itemView.findViewById(id); 596904f67c96a28a0e5966b4fb6d37a0ad5f136858Tony Mantler if (v != null) { 606904f67c96a28a0e5966b4fb6d37a0ad5f136858Tony Mantler mCachedViews.put(id, v); 616904f67c96a28a0e5966b4fb6d37a0ad5f136858Tony Mantler } 626904f67c96a28a0e5966b4fb6d37a0ad5f136858Tony Mantler return v; 636904f67c96a28a0e5966b4fb6d37a0ad5f136858Tony Mantler } 646904f67c96a28a0e5966b4fb6d37a0ad5f136858Tony Mantler } 65913cb9a1d85f1cf77430312e91e9a2ceeb553e9dTony Mantler 66913cb9a1d85f1cf77430312e91e9a2ceeb553e9dTony Mantler /** 67913cb9a1d85f1cf77430312e91e9a2ceeb553e9dTony Mantler * Dividers are only drawn between items if both items allow it, or above the first and below 68913cb9a1d85f1cf77430312e91e9a2ceeb553e9dTony Mantler * the last item if that item allows it. 69913cb9a1d85f1cf77430312e91e9a2ceeb553e9dTony Mantler * 70913cb9a1d85f1cf77430312e91e9a2ceeb553e9dTony Mantler * @return true if dividers are allowed above this item 71913cb9a1d85f1cf77430312e91e9a2ceeb553e9dTony Mantler */ 72913cb9a1d85f1cf77430312e91e9a2ceeb553e9dTony Mantler public boolean isDividerAllowedAbove() { 73913cb9a1d85f1cf77430312e91e9a2ceeb553e9dTony Mantler return mDividerAllowedAbove; 74913cb9a1d85f1cf77430312e91e9a2ceeb553e9dTony Mantler } 75913cb9a1d85f1cf77430312e91e9a2ceeb553e9dTony Mantler 76913cb9a1d85f1cf77430312e91e9a2ceeb553e9dTony Mantler /** 77913cb9a1d85f1cf77430312e91e9a2ceeb553e9dTony Mantler * Dividers are only drawn between items if both items allow it, or above the first and below 78913cb9a1d85f1cf77430312e91e9a2ceeb553e9dTony Mantler * the last item if that item allows it. 79913cb9a1d85f1cf77430312e91e9a2ceeb553e9dTony Mantler * 8092c3b5474321450e910495c0ee6bd613ee4b42e6Tony Mantler * By default, {@link Preference#onBindViewHolder(PreferenceViewHolder)} will set this to the 8192c3b5474321450e910495c0ee6bd613ee4b42e6Tony Mantler * same value as returned by {@link Preference#isSelectable()}, so that non-selectable items 8292c3b5474321450e910495c0ee6bd613ee4b42e6Tony Mantler * do not have a divider drawn above them. 8392c3b5474321450e910495c0ee6bd613ee4b42e6Tony Mantler * 84913cb9a1d85f1cf77430312e91e9a2ceeb553e9dTony Mantler * @param allowed false to prevent dividers being drawn above this item 85913cb9a1d85f1cf77430312e91e9a2ceeb553e9dTony Mantler */ 86913cb9a1d85f1cf77430312e91e9a2ceeb553e9dTony Mantler public void setDividerAllowedAbove(boolean allowed) { 87913cb9a1d85f1cf77430312e91e9a2ceeb553e9dTony Mantler mDividerAllowedAbove = allowed; 88913cb9a1d85f1cf77430312e91e9a2ceeb553e9dTony Mantler } 89913cb9a1d85f1cf77430312e91e9a2ceeb553e9dTony Mantler 90913cb9a1d85f1cf77430312e91e9a2ceeb553e9dTony Mantler /** 91913cb9a1d85f1cf77430312e91e9a2ceeb553e9dTony Mantler * Dividers are only drawn between items if both items allow it, or above the first and below 92913cb9a1d85f1cf77430312e91e9a2ceeb553e9dTony Mantler * the last item if that item allows it. 93913cb9a1d85f1cf77430312e91e9a2ceeb553e9dTony Mantler * 94913cb9a1d85f1cf77430312e91e9a2ceeb553e9dTony Mantler * @return true if dividers are allowed below this item 95913cb9a1d85f1cf77430312e91e9a2ceeb553e9dTony Mantler */ 96913cb9a1d85f1cf77430312e91e9a2ceeb553e9dTony Mantler public boolean isDividerAllowedBelow() { 97913cb9a1d85f1cf77430312e91e9a2ceeb553e9dTony Mantler return mDividerAllowedBelow; 98913cb9a1d85f1cf77430312e91e9a2ceeb553e9dTony Mantler } 99913cb9a1d85f1cf77430312e91e9a2ceeb553e9dTony Mantler 100913cb9a1d85f1cf77430312e91e9a2ceeb553e9dTony Mantler /** 101913cb9a1d85f1cf77430312e91e9a2ceeb553e9dTony Mantler * Dividers are only drawn between items if both items allow it, or above the first and below 102913cb9a1d85f1cf77430312e91e9a2ceeb553e9dTony Mantler * the last item if that item allows it. 103913cb9a1d85f1cf77430312e91e9a2ceeb553e9dTony Mantler * 10492c3b5474321450e910495c0ee6bd613ee4b42e6Tony Mantler * By default, {@link Preference#onBindViewHolder(PreferenceViewHolder)} will set this to the 10592c3b5474321450e910495c0ee6bd613ee4b42e6Tony Mantler * same value as returned by {@link Preference#isSelectable()}, so that non-selectable items 10692c3b5474321450e910495c0ee6bd613ee4b42e6Tony Mantler * do not have a divider drawn below them. 10792c3b5474321450e910495c0ee6bd613ee4b42e6Tony Mantler * 108913cb9a1d85f1cf77430312e91e9a2ceeb553e9dTony Mantler * @param allowed false to prevent dividers being drawn below this item 109913cb9a1d85f1cf77430312e91e9a2ceeb553e9dTony Mantler */ 110913cb9a1d85f1cf77430312e91e9a2ceeb553e9dTony Mantler public void setDividerAllowedBelow(boolean allowed) { 111913cb9a1d85f1cf77430312e91e9a2ceeb553e9dTony Mantler mDividerAllowedBelow = allowed; 112913cb9a1d85f1cf77430312e91e9a2ceeb553e9dTony Mantler } 1136904f67c96a28a0e5966b4fb6d37a0ad5f136858Tony Mantler} 114