PreferenceScreen.java revision 83deca7547a76f037c32fdc946173b4708b8d05b
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.content.Context;
20import android.util.AttributeSet;
21
22/**
23 * Represents a top-level {@link Preference} that
24 * is the root of a Preference hierarchy. A {@link PreferenceFragmentCompat}
25 * points to an instance of this class to show the preferences. To instantiate
26 * this class, use {@link PreferenceManager#createPreferenceScreen(android.content.Context)}.
27 * <ul>
28 * This class can appear in two places:
29 * <li> When a {@link PreferenceFragmentCompat} points to this, it is used as the root
30 * and is not shown (only the contained preferences are shown).
31 * <li> When it appears inside another preference hierarchy, it is shown and
32 * serves as the gateway to another screen of preferences (either by showing
33 * another screen of preferences as a {@link android.app.Dialog} or via a
34 * {@link android.content.Context#startActivity(android.content.Intent)} from the
35 * {@link Preference#getIntent()}). The children of this {@link PreferenceScreen}
36 * are NOT shown in the screen that this {@link PreferenceScreen} is shown in.
37 * Instead, a separate screen will be shown when this preference is clicked.
38 * </ul>
39 * <p>Here's an example XML layout of a PreferenceScreen:</p>
40 * <pre>
41 &lt;PreferenceScreen
42 xmlns:android="http://schemas.android.com/apk/res/android"
43 android:key="first_preferencescreen"&gt;
44 &lt;CheckBoxPreference
45 android:key="wifi enabled"
46 android:title="WiFi" /&gt;
47 &lt;PreferenceScreen
48 android:key="second_preferencescreen"
49 android:title="WiFi settings"&gt;
50 &lt;CheckBoxPreference
51 android:key="prefer wifi"
52 android:title="Prefer WiFi" /&gt;
53 ... other preferences here ...
54 &lt;/PreferenceScreen&gt;
55 &lt;/PreferenceScreen&gt; </pre>
56 * <p>
57 * In this example, the "first_preferencescreen" will be used as the root of the
58 * hierarchy and given to a {@link android.support.v14.preference.PreferenceFragment}
59 * or {@link PreferenceFragmentCompat}.
60 * The first screen will
61 * show preferences "WiFi" (which can be used to quickly enable/disable WiFi)
62 * and "WiFi settings". The "WiFi settings" is the "second_preferencescreen" and when
63 * clicked will show another screen of preferences such as "Prefer WiFi" (and
64 * the other preferences that are children of the "second_preferencescreen" tag).
65 *
66 * <div class="special reference">
67 * <h3>Developer Guides</h3>
68 * <p>For information about building a settings UI with Preferences,
69 * read the <a href="{@docRoot}guide/topics/ui/settings.html">Settings</a>
70 * guide.</p>
71 * </div>
72 *
73 * @see PreferenceCategory
74 */
75public final class PreferenceScreen extends PreferenceGroup  {
76
77    private boolean mShouldUseGeneratedIds = true;
78
79    /**
80     * Do NOT use this constructor, use {@link PreferenceManager#createPreferenceScreen(Context)}.
81     * @hide-
82     */
83    public PreferenceScreen(Context context, AttributeSet attrs) {
84        super(context, attrs, R.attr.preferenceScreenStyle);
85    }
86
87    @Override
88    protected void onClick() {
89        if (getIntent() != null || getFragment() != null || getPreferenceCount() == 0) {
90            return;
91        }
92        final PreferenceManager.OnNavigateToScreenListener listener =
93                getPreferenceManager().getOnNavigateToScreenListener();
94        if (listener != null) {
95            listener.onNavigateToScreen(this);
96        }
97    }
98
99    @Override
100    protected boolean isOnSameScreenAsChildren() {
101        return false;
102    }
103
104    /**
105     * See {@link #setShouldUseGeneratedIds(boolean)}
106     * @return {@code true} if the adapter should use the preference IDs returned by
107     *         {@link Preference#getId()} as stable item IDs
108     */
109    public boolean shouldUseGeneratedIds() {
110        return mShouldUseGeneratedIds;
111    }
112
113    /**
114     * Set whether the adapter created for this screen should attempt to use the auto-generated
115     * preference IDs returned by {@link Preference#getId()} as stable item IDs. Setting this to
116     * false can suppress unwanted animations if {@link Preference} objects are frequently removed
117     * from and re-added to their containing {@link PreferenceGroup}.
118     * <p>
119     * This method may only be called when the preference screen is not attached to the hierarchy.
120     * <p>
121     * Default value is {@code true}.
122     *
123     * @param shouldUseGeneratedIds {@code true} if the adapter should use the preference ID as a
124     *                                          stable ID, or {@code false} to disable the use of
125     *                                          stable IDs
126     */
127    public void setShouldUseGeneratedIds(boolean shouldUseGeneratedIds) {
128        if (isAttached()) {
129            throw new IllegalStateException("Cannot change the usage of generated IDs while" +
130                    " attached to the preference hierarchy");
131        }
132        mShouldUseGeneratedIds = shouldUseGeneratedIds;
133    }
134}
135