最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android中刪除Preference詳解

 更新時間:2015年01月19日 09:07:35   投稿:junjie  
這篇文章主要介紹了Android中刪除Preference詳解,很多時候刪除Preference總會失敗,本文著重分析刪除失敗的原因,需要的朋友可以參考下

Android的設(shè)置界面實(shí)現(xiàn)比較簡單,有時甚至只需要使用一個簡單的xml文件即可.聲明簡單,但是如何從PreferenceScreen或者PreferenceCategory中刪除一個Preference會簡單么.為什么有些人寫的就無法刪除成功呢?本文將從Android源碼實(shí)現(xiàn)來分析一下.

聲明文件

復(fù)制代碼 代碼如下:

<?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).

復(fù)制代碼 代碼如下:

PreferenceScreen screen = getPreferenceScreen();
CheckBoxPreference autoClearCheckboxPref = (CheckBoxPreference) screen.findPreference("rmcache");
screen.removePreference(autoClearCheckboxPref);

刪除key為holo_theme的Preference,其為PreferenceScreen root的孫子節(jié)點(diǎn),非直接關(guān)系.

復(fù)制代碼 代碼如下:

PreferenceCategory themePrefCategory = (PreferenceCategory) screen.findPreference("theme");
CheckBoxPreference holoCheckboxPref = (CheckBoxPreference)themePrefCategory.findPreference("holo_theme");
themePrefCategory.removePreference(holoCheckboxPref);

為什么刪除失敗

很多人出現(xiàn)了刪除失敗的問題,主要原因是使用了非父親節(jié)點(diǎn)來刪除,比如這樣

復(fù)制代碼 代碼如下:

PreferenceScreen screen = getPreferenceScreen();
CheckBoxPreference holoCheckboxPref = (CheckBoxPreference)screen.findPreference("holo_theme");
screen.removePreference(holoCheckboxPref);

PreferenceGroup刪除實(shí)現(xiàn),其實(shí)PreferenceScreen和PreferenceCategory都是PreferenceGroup的子類.

復(fù)制代碼 代碼如下:

/**
     * 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引用時,通常后者效率會高.

復(fù)制代碼 代碼如下:

/**
 * 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)確刪除哪一個.

相關(guān)文章

最新評論

阿瓦提县| 泗洪县| 荆州市| 通山县| 浦东新区| 凤阳县| 庆阳市| 周宁县| 金寨县| 宣化县| 比如县| 广平县| 密云县| 花莲县| 砀山县| 霍林郭勒市| 华宁县| 大足县| 壤塘县| 台前县| 鲁山县| 灌云县| 娱乐| 大庆市| 印江| 广南县| 崇明县| 永德县| 会昌县| 西乌| 哈巴河县| 鄯善县| 错那县| 榆中县| 南漳县| 西贡区| 全南县| 堆龙德庆县| 黔东| 海林市| 香格里拉县|