Workarounds to avoid removing all prefs

Removing all prefs causes ugly animations, so avoid it at all cost
and cache all the prefs (while still added) as long as possible.

Bug: 26271353
Change-Id: I33b84d751938b460f4b66c0158057407dd45d974
This commit is contained in:
Jason Monk
2016-02-25 13:55:48 -05:00
parent 6c09bd2538
commit 2071eda150
5 changed files with 72 additions and 29 deletions

View File

@@ -34,6 +34,7 @@ import android.support.v7.preference.PreferenceViewHolder;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.ArrayMap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
@@ -97,6 +98,7 @@ public abstract class SettingsPreferenceFragment extends InstrumentedPreferenceF
private View mEmptyView;
private LinearLayoutManager mLayoutManager;
private HighlightablePreferenceGroupAdapter mAdapter;
private ArrayMap<String, Preference> mPreferenceCache;
@Override
public void onCreate(Bundle icicle) {
@@ -367,6 +369,28 @@ public abstract class SettingsPreferenceFragment extends InstrumentedPreferenceF
return mAdapter;
}
protected void cacheRemoveAllPrefs(PreferenceGroup group) {
mPreferenceCache = new ArrayMap<String, Preference>();
final int N = group.getPreferenceCount();
for (int i = 0; i < N; i++) {
Preference p = group.getPreference(i);
if (TextUtils.isEmpty(p.getKey())) {
continue;
}
mPreferenceCache.put(p.getKey(), p);
}
}
protected Preference getCachedPreference(String key) {
return mPreferenceCache != null ? mPreferenceCache.remove(key) : null;
}
protected void removeCachedPrefs(PreferenceGroup group) {
for (Preference p : mPreferenceCache.values()) {
group.removePreference(p);
}
}
private void highlightPreference(String key) {
final int position = canUseListViewForHighLighting(key);
if (position >= 0) {