Android中刪除Preference詳解
Android的設(shè)置界面實(shí)現(xiàn)比較簡單,有時甚至只需要使用一個簡單的xml文件即可.聲明簡單,但是如何從PreferenceScreen或者PreferenceCategory中刪除一個Preference會簡單么.為什么有些人寫的就無法刪除成功呢?本文將從Android源碼實(shí)現(xiàn)來分析一下.
聲明文件
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:key="root">
<PreferenceCategory
android:key="theme"
android:title="Theme"
android:summary="Theme Settings"
>
<CheckBoxPreference
android:key="holo_theme"
android:title="Holo Theme"
android:summary="Use Holo Theme"
/>
</PreferenceCategory>
<CheckBoxPreference
android:key="rmcache"
android:title="Auto Clear Cache"
android:summary="Enable Auto Clear Cache "
/>
</PreferenceScreen>
層級關(guān)系
刪除Preference
刪除key為rmcache的Preference,這個Preference是PreferenceScreen root的子節(jié)點(diǎn).
PreferenceScreen screen = getPreferenceScreen();
CheckBoxPreference autoClearCheckboxPref = (CheckBoxPreference) screen.findPreference("rmcache");
screen.removePreference(autoClearCheckboxPref);
刪除key為holo_theme的Preference,其為PreferenceScreen root的孫子節(jié)點(diǎn),非直接關(guān)系.
PreferenceCategory themePrefCategory = (PreferenceCategory) screen.findPreference("theme");
CheckBoxPreference holoCheckboxPref = (CheckBoxPreference)themePrefCategory.findPreference("holo_theme");
themePrefCategory.removePreference(holoCheckboxPref);
為什么刪除失敗
很多人出現(xiàn)了刪除失敗的問題,主要原因是使用了非父親節(jié)點(diǎn)來刪除,比如這樣
PreferenceScreen screen = getPreferenceScreen();
CheckBoxPreference holoCheckboxPref = (CheckBoxPreference)screen.findPreference("holo_theme");
screen.removePreference(holoCheckboxPref);
PreferenceGroup刪除實(shí)現(xiàn),其實(shí)PreferenceScreen和PreferenceCategory都是PreferenceGroup的子類.
/**
* Removes a {@link Preference} from this group.
*
* @param preference The preference to remove.
* @return Whether the preference was found and removed.
*/
public boolean removePreference(Preference preference) {
final boolean returnValue = removePreferenceInt(preference);
notifyHierarchyChanged();
return returnValue;
}
private boolean removePreferenceInt(Preference preference) {
synchronized(this) {
preference.onPrepareForRemoval();
return mPreferenceList.remove(preference);
}
}
而mPreferenceList中存放的都是當(dāng)前PreferenceGroup的直接子Preference.
findPreference實(shí)現(xiàn)
findPreference查找不僅僅限于直接子Preference,會遍歷其所有的子Preference.
所以代碼中同樣有root PreferenceGroup和直接父PreferenceGroup引用時,通常后者效率會高.
/**
* Finds a {@link Preference} based on its key. If two {@link Preference}
* share the same key (not recommended), the first to appear will be
* returned (to retrieve the other preference with the same key, call this
* method on the first preference). If this preference has the key, it will
* not be returned.
* <p>
* This will recursively search for the preference into children that are
* also {@link PreferenceGroup PreferenceGroups}.
*
* @param key The key of the preference to retrieve.
* @return The {@link Preference} with the key, or null.
*/
public Preference findPreference(CharSequence key) {
if (TextUtils.equals(getKey(), key)) {
return this;
}
final int preferenceCount = getPreferenceCount();
for (int i = 0; i < preferenceCount; i++) {
final Preference preference = getPreference(i);
final String curKey = preference.getKey();
if (curKey != null && curKey.equals(key)) {
return preference;
}
if (preference instanceof PreferenceGroup) {
final Preference returnedPreference = ((PreferenceGroup)preference)
.findPreference(key);
if (returnedPreference != null) {
return returnedPreference;
}
}
}
return null;
}
findPreference和removePreference實(shí)現(xiàn)比較
為什么findPreference遍歷所有的子節(jié)點(diǎn),而removePreference不會,只會刪除直接子Preference
原因有以下幾點(diǎn):
1.findPreference支持遍歷查找,減少了聲明諸多的中間PreferenceGroup代碼.而findPreference屬于常用接口方法.
2.removePreference調(diào)用較少.
3.當(dāng)存在key相同的Preference時,如果removePreference不限定直接子Preference,那么無法準(zhǔn)確刪除哪一個.
- android ListView內(nèi)數(shù)據(jù)的動態(tài)添加與刪除實(shí)例代碼
- Android 創(chuàng)建/驗(yàn)證/刪除桌面快捷方式(已測試可用)
- Android實(shí)現(xiàn)WebView刪除緩存的方法
- Android Studio徹底刪除項(xiàng)目 Android Studio徹底刪除Module
- Android Recyclerview實(shí)現(xiàn)多選,單選,全選,反選,批量刪除的功能
- Android高仿QQ6.0側(cè)滑刪除實(shí)例代碼
- Android通訊錄開發(fā)之刪除功能的實(shí)現(xiàn)方法
- Android中刪除文件以及文件夾的命令記錄
- Android遞歸方式刪除某文件夾下的所有文件(.mp3文件等等)
- android實(shí)現(xiàn)簡單左滑刪除控件
相關(guān)文章
Android使用OkHttp進(jìn)行重定向攔截處理的方法
這篇文章主要介紹了Android使用OkHttp進(jìn)行重定向攔截處理的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-08-08Android App調(diào)試內(nèi)存泄露之Cursor篇
最近在工作中處理了一些內(nèi)存泄露的問題,在這個過程中我尤其發(fā)現(xiàn)了一些基本的問題反而忽略導(dǎo)致內(nèi)存泄露2012-11-11詳解Android Scroller與computeScroll的調(diào)用機(jī)制關(guān)系
這篇文章主要介紹了詳解Android Scroller與computeScroll的調(diào)用機(jī)制關(guān)系的相關(guān)資料,需要的朋友可以參考下2016-01-01非常詳細(xì)的android so庫逆向調(diào)試教程
這篇文章主要給大家介紹了關(guān)于android so庫逆向調(diào)試的相關(guān)資料,文中通過示例代碼以及圖文介紹的非常詳細(xì),對各位Android開發(fā)者具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2021-08-08Android編程簡易實(shí)現(xiàn)XML解析的方法詳解
這篇文章主要介紹了Android編程簡易實(shí)現(xiàn)XML解析的方法,結(jié)合實(shí)例形式總結(jié)分析了Android操作xml文件的各種常見技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-08-08Android 基于MediatorLiveData實(shí)現(xiàn)紅點(diǎn)的統(tǒng)一管理
這篇文章主要介紹了Android 基于MediatorLiveData實(shí)現(xiàn)紅點(diǎn)的統(tǒng)一管理,幫助大家更好的理解和學(xué)習(xí)使用Android,感興趣的朋友可以了解下2021-04-04Android學(xué)習(xí)筆記之ListView復(fù)用機(jī)制詳解
本篇文章主要介紹了Android學(xué)習(xí)筆記之ListView復(fù)用機(jī)制詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02