问题
I have a MyPreferenceFragment that inherits from PreferenceFragmentCompat. This fragment resides inside a viewpager.
I want the preference screen to have 1 EditTextPreference, "Name", and two preferences which should open up a new preference screen, "Mobile" and "Email". However, while all 3 shows up correctly, clicking "Mobile" or "Email" does nothing. How do i get it to open a new screen? From the documentation it seems like this should happen automatically when nesting PreferenceScreen.
The xml file for this PreferenceFragment looks like this:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<android.support.v7.preference.PreferenceCategory
android:key="account_category" >
<android.support.v7.preference.EditTextPreference
android:persistent="true"
android:key="account_name" />
<android.support.v7.preference.PreferenceScreen
android:key="account_mobile_screen" >
<android.support.v7.preference.EditTextPreference
android:persistent="true"
android:key="account_mobile" />
</android.support.v7.preference.PreferenceScreen>
<android.support.v7.preference.PreferenceScreen
android:key="account_email_screen" >
<android.support.v7.preference.EditTextPreference
android:persistent="true"
android:key="account_email" />
</android.support.v7.preference.PreferenceScreen>
</android.support.v7.preference.PreferenceCategory>
</android.support.v7.preference.PreferenceScreen>
回答1:
So .. I needed to do something similar, I hope it helps if you still have the problem.
My SettingsFragment:
using Android.OS;
using Android.Views;
using Android.Util;
using Android.Preferences;
using Android.Support.V7.Preferences;
namespace Aprepara.Droid.Activities.Fragments
{
public class SettingsFragment : PreferenceFragmentCompat
{
private View _view;
public override void OnCreatePreferences(Bundle savedInstanceState, string rootKey)
{
//AddPreferencesFromResource(Resource.Xml.preferences);
}
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
AddPreferencesFromResource(Resource.Xml.preferences);
}
}
}
In the settings.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/my_preferences_settings"/>
In the preference xml.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.preference.PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<android.support.v7.preference.PreferenceCategory
android:title="Category 1">
<android.support.v7.preference.SwitchPreferenceCompat
android:key="key1"
android:title="Switch Preference"
android:summary="Switch Summary"
android:defaultValue="true" />
<android.support.v7.preference.EditTextPreference
android:key="key2"
android:title="EditText Preference"
android:summary="EditText Summary"
android:dialogMessage="Dialog Message"
android:defaultValue="Default value" />
<android.support.v7.preference.CheckBoxPreference
android:key="key3"
android:title="CheckBox Preference"
android:summary="CheckBox Summary"
android:defaultValue="true"/>
</android.support.v7.preference.PreferenceCategory>
</android.support.v7.preference.PreferenceScreen>
Styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
....
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/ColorActionBar</item>
<item name="colorPrimaryDark">@color/ColorActionBar</item>
<item name="colorAccent">@color/accent</item>
<item name="preferenceTheme">@style/PreferenceThemeOverlay</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:listDivider">@color/background3</item>
<item name="preferenceTheme">@style/PreferenceThemeOverlay</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>
Folder structure of my project
I'm using the example https://www.codeday.top/2017/07/27/31411.html and https://medium.com/@JakobUlbrich/building-a-settings-screen-for-android-part-1-5959aa49337c
Finally, the result
来源:https://stackoverflow.com/questions/34085303/android-preferencefragment-in-viewpager