Move PreferenceGroupAdapter creation out of PreferenceScreen
Change-Id: I38a81a505b34dcee50e5c364095b0dcdf8a881c7
diff --git a/v14/preference/src/android/support/v14/preference/PreferenceFragment.java b/v14/preference/src/android/support/v14/preference/PreferenceFragment.java
index 9fb1840..20bf2aa 100644
--- a/v14/preference/src/android/support/v14/preference/PreferenceFragment.java
+++ b/v14/preference/src/android/support/v14/preference/PreferenceFragment.java
@@ -29,6 +29,7 @@
import android.support.v7.preference.EditTextPreference;
import android.support.v7.preference.ListPreference;
import android.support.v7.preference.Preference;
+import android.support.v7.preference.PreferenceGroupAdapter;
import android.support.v7.preference.PreferenceManager;
import android.support.v7.preference.PreferenceScreen;
import android.support.v7.widget.LinearLayoutManager;
@@ -162,7 +163,7 @@
/**
* Called when the user has clicked on a Preference that has
* a fragment class name associated with it. The implementation
- * to should instantiate and switch to an instance of the given
+ * should instantiate and switch to an instance of the given
* fragment.
*/
boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref);
@@ -442,7 +443,7 @@
private void bindPreferences() {
final PreferenceScreen preferenceScreen = getPreferenceScreen();
if (preferenceScreen != null) {
- getListView().setAdapter(preferenceScreen.getAdapter());
+ getListView().setAdapter(onCreateAdapter(preferenceScreen));
}
onBindPreferences();
}
@@ -493,6 +494,16 @@
}
/**
+ * Creates the root adapter.
+ *
+ * @param preferenceScreen Preference screen object to create the adapter for.
+ * @return An adapter that contains the preferences contained in this {@link PreferenceScreen}.
+ */
+ protected RecyclerView.Adapter onCreateAdapter(PreferenceScreen preferenceScreen) {
+ return new PreferenceGroupAdapter(preferenceScreen);
+ }
+
+ /**
* Called when a preference in the tree requests to display a dialog. Subclasses should
* override this method to display custom dialogs or to handle dialogs for custom preference
* classes.
diff --git a/v7/preference/src/android/support/v7/preference/PreferenceFragmentCompat.java b/v7/preference/src/android/support/v7/preference/PreferenceFragmentCompat.java
index 72f742c..ac4ea17 100644
--- a/v7/preference/src/android/support/v7/preference/PreferenceFragmentCompat.java
+++ b/v7/preference/src/android/support/v7/preference/PreferenceFragmentCompat.java
@@ -436,7 +436,7 @@
private void bindPreferences() {
final PreferenceScreen preferenceScreen = getPreferenceScreen();
if (preferenceScreen != null) {
- getListView().setAdapter(preferenceScreen.getAdapter());
+ getListView().setAdapter(onCreateAdapter(preferenceScreen));
}
onBindPreferences();
}
@@ -487,6 +487,16 @@
}
/**
+ * Creates the root adapter.
+ *
+ * @param preferenceScreen Preference screen object to create the adapter for.
+ * @return An adapter that contains the preferences contained in this {@link PreferenceScreen}.
+ */
+ protected RecyclerView.Adapter onCreateAdapter(PreferenceScreen preferenceScreen) {
+ return new PreferenceGroupAdapter(preferenceScreen);
+ }
+
+ /**
* Called when a preference in the tree requests to display a dialog. Subclasses should
* override this method to display custom dialogs or to handle dialogs for custom preference
* classes.
diff --git a/v7/preference/src/android/support/v7/preference/PreferenceScreen.java b/v7/preference/src/android/support/v7/preference/PreferenceScreen.java
index dc1c98f..2010080 100644
--- a/v7/preference/src/android/support/v7/preference/PreferenceScreen.java
+++ b/v7/preference/src/android/support/v7/preference/PreferenceScreen.java
@@ -17,7 +17,6 @@
package android.support.v7.preference;
import android.content.Context;
-import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
/**
@@ -74,8 +73,6 @@
*/
public final class PreferenceScreen extends PreferenceGroup {
- private RecyclerView.Adapter mRootAdapter;
-
/**
* Do NOT use this constructor, use {@link PreferenceManager#createPreferenceScreen(Context)}.
* @hide-
@@ -84,35 +81,6 @@
super(context, attrs, R.attr.preferenceScreenStyle);
}
- /**
- * Returns an adapter that can be attached to a {@link PreferenceFragment}
- * or {@link PreferenceFragmentCompat} to show the preferences contained in this
- * {@link PreferenceScreen}.
- * <p>
- * This {@link PreferenceScreen} will NOT appear in the returned adapter, instead
- * it appears in the hierarchy above this {@link PreferenceScreen}.
- *
- * @return An adapter that manages the {@link Preference} contained in this
- * {@link PreferenceScreen}.
- */
- public final RecyclerView.Adapter getAdapter() {
- if (mRootAdapter == null) {
- mRootAdapter = onCreateAdapter();
- }
-
- return mRootAdapter;
- }
-
- /**
- * Creates the root adapter.
- *
- * @return An adapter that contains the preferences contained in this {@link PreferenceScreen}.
- * @see #getAdapter()
- */
- protected RecyclerView.Adapter onCreateAdapter() {
- return new PreferenceGroupAdapter(this);
- }
-
@Override
protected void onClick() {
if (getIntent() != null || getFragment() != null || getPreferenceCount() == 0) {