我正在尝试使用AppCompatActivity和support.v7.preference在子屏幕上实现首选项
根据文档,另一个PreferenceScreen中的每个PreferenceScreen都充当子屏幕,并且框架将在单击时处理显示它。
http://developer.android.com/guide/topics/ui/settings.html#Subscreens
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<!-- opens a subscreen of settings -->
<PreferenceScreen
android:key="button_voicemail_category_key"
android:title="@string/voicemail"
android:persistent="false">
<ListPreference
android:key="button_voicemail_provider_key"
android:title="@string/voicemail_provider" ... />
<!-- opens another nested subscreen -->
<PreferenceScreen
android:key="button_voicemail_setting_key"
android:title="@string/voicemail_settings"
android:persistent="false">
...
</PreferenceScreen>
<RingtonePreference
android:key="button_voicemail_ringtone_key"
android:title="@string/voicemail_ringtone_title"
android:ringtoneType="notification" ... />
...
</PreferenceScreen>
...
</PreferenceScreen>
使用 native Activity,PreferenceFragment ...可以很好地工作,但是使用AppCompatActivity和PreferenceFragmentCompat,单击Preference元素只是将其突出显示,但不会打开子屏幕。
我在阅读文档和代码时找不到任何东西...我是否需要实现任何其他回调?
编辑:仅出于完整性考虑...
这可以正常工作并打开子屏幕:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new DemoPreferenceFragment())
.commit();
}
}
static public class DemoPreferenceFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
}
这不起作用/无法打开子屏幕:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.replace(android.R.id.content, new DemoPreferenceFragment())
.commit();
}
}
static public class DemoPreferenceFragment extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(Bundle bundle, String s) {
addPreferencesFromResource(R.xml.preferences);
}
}
}
编辑:2016年1月25日
摆弄了几天的support.v7.preference后,我在这里总结了自己的发现,希望对其他人有所帮助:
HowTo use support.v7.preference with AppCompat and potential drawbacks
最佳答案
它看起来像是PreferenceFragmentCompat中的错误或文档不足。它具有onNavigateToScreen方法,当您单击PreferenceScreen项目时会调用该方法。
但是方法getCallbackFragment()默认情况下返回null,因此您需要在片段中覆盖它以返回此值。另外,您还需要实现PreferenceFragmentCompat.OnPreferenceStartScreenCallback。
public class SettingsFragment extends PreferenceFragmentCompat implements PreferenceFragmentCompat.OnPreferenceStartScreenCallback {
public static SettingsFragment newInstance() {
return new SettingsFragment();
}
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
addPreferencesFromResource(R.xml.news_settings);
}
@Override
public Fragment getCallbackFragment() {
return this;
}
@Override
public boolean onPreferenceStartScreen(PreferenceFragmentCompat preferenceFragmentCompat, PreferenceScreen preferenceScreen) {
preferenceFragmentCompat.setPreferenceScreen(preferenceScreen);
return true;
}
}
但是,当您无法返回到初始的PreferenceScreen时,就会导致另一个问题,
另一种方法是替换此处描述的片段How to move back from Preferences subscreen to main screen in PreferenceFragmentCompat?