2010-03-07 74 views
13

在我的Android應用程序中,我有一個PreferenceScreen父項,它有3個CheckBoxPreferences作爲子項。PreferenceScreen android:摘要更新!

當我點擊父preferenceScreen,並顯示3個複選框,我選擇其中之一,並在Preference.OnPreferenceChangeListener asociated與我設置了父母的preferenceScreen總結與複選框:

Parent.setSummary("string depending on the selection") 

的事情是,當我回到父母時,它的摘要不會更新,即使內部值已相應地更改爲設置的值。

有沒有人有關於此行爲的任何想法?

+0

有一點要注意,如果感興趣的偏好本身就是'CheckBoxPreference'(或其他'TwoStatePreference',如'SwitchPreference'),然後'setSummary'沒有按如果分配了android:summaryOn或android:summaryOff,則不起作用。在這種情況下,應該使用'setSummaryOn'和'setSummaryOff',這不是明顯的imho。 – Stan 2015-06-27 12:52:21

回答

1

我發現,它似乎遵循了setSummary()getListView().invalidate()

12

使用

Parent.setSummary("string depending on the selection"); 
((BaseAdapter)getPreferenceScreen().getRootAdapter()).notifyDataSetChanged(); 

的作品就像一個魅力,可以不管被用來更改總結的地方。

+0

如果屏幕嵌套在其他屏幕中,則應在直接父屏幕上完成。 – 2016-06-16 15:12:36

0

對片段而不是活動的新堅持似乎使這更難。 invalidate路由似乎沒有工作,也沒有使用底層View的方法。感謝halxinate的回答,我現在已經設法解決了這個問題。人誰是新的,因爲我這這裏都是一些細節:

當創建的設置片段,保存在您的主要活動的參考,如:

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    switch (item.getItemId()) { 
    case R.id.action_preferences: 
     if (getFragmentManager().getBackStackEntryCount() < 1) { 
      FragmentTransaction trans = getFragmentManager() 
        .beginTransaction(); 
      // Save a reference to the settings fragment 
      settingsFrag = new SettingsFragment(); 
      trans.replace(R.id.container, settingsFrag); 
      trans.addToBackStack(null); 
      trans.commit(); 
     } 
     return true; 
    default: 
     return super.onOptionsItemSelected(item); 
    } 
} 

然後,當你想從OnSharedPreferenceChangeListener更新外部PreferenceScreen的摘要使用這種事情。請注意,您需要在您的偏好片段XML來定義一個鍵外PreferenceScreen(在這種情況下android:key="prefs_root"):

public static void setOuterSummaries(SettingsFragment sf) { 
    // Set the outer preferences summaries 
    if (sf == null) 
     return; 
    //Code to update the summaries.... 

    // Force the parent screen summaries to update 
    prefScr = (PreferenceScreen) sf.findPreference("prefs_root"); 
    if (prefScr != null) 
      ((BaseAdapter) prefScr.getRootAdapter()).notifyDataSetChanged(); 

} 

OnSharedPreferenceChangeListener listener = new SharedPreferences.OnSharedPreferenceChangeListener() { 
    public void onSharedPreferenceChanged(SharedPreferences prefs, 
      String key) { 
     Log.e("L", "Change"); 
     setOuterSummaries(settingsFrag); 
    } 
}; 

需要注意的是,你可以參考保存到BaseAdapter,而不是將設置片段但這方法似乎更安全,當你想到將其推廣到多片段情況或動態創建片段內容的代碼時。

1

這是正確的做法

Preference pref = findPreference(getString(R.string.key_of_pref)); 
PreferenceScreen parent = (PreferenceScreen) sf.findPreference(getString(R.string.key_of_preference_screen)); 
pref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { 
    @Override 
    public boolean onPreferenceChange(Preference preference, Object newValue) { 
     boolean newValueBool = (Boolean) newValue; 
     parent.setSummary(newValueBool ? "Summary is true" : "Summary is false");             
     ((BaseAdapter) getPreferenceScreen().getRootAdapter()).notifyDataSetChanged(); 
     // true to update the state of the Preference with the new value 
     // in case you want to disallow the change return false 
     return true; 
    } 
});