[go: nahoru, domu]

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