ViewPager中的Android PreferenceFragment [英] Android PreferenceFragment in ViewPager

查看:33
本文介绍了ViewPager中的Android PreferenceFragment的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MyPreferenceFragment,它继承自PreferenceFragmentCompat.该片段位于viewpager中.

我希望首选项屏幕具有1个EditTextPreference,名称"和两个首选项,这两个首选项应打开一个新的首选项屏幕:移动"和电子邮件".但是,虽然所有3个都正确显示,但是单击移动"或电子邮件"不会执行任何操作.我如何获得它以打开新屏幕?从文档看来,嵌套PreferenceScreen时应该自动发生这种情况.

此PreferenceFragment的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.PreferenceCategoryandroid:key ="account_category">< android.support.v7.preference.EditTextPreferenceandroid:persistent ="true"android:key ="account_name"/>< android.support.v7.preference.PreferenceScreenandroid:key ="account_mobile_screen">< android.support.v7.preference.EditTextPreferenceandroid:persistent ="true"android:key ="account_mobile"/></android.support.v7.preference.PreferenceScreen>< android.support.v7.preference.PreferenceScreenandroid:key ="account_email_screen">< android.support.v7.preference.EditTextPreferenceandroid:persistent ="true"android:key ="account_email"/></android.support.v7.preference.PreferenceScreen></android.support.v7.preference.PreferenceCategory></android.support.v7.preference.PreferenceScreen> 

解决方案

所以..我需要做类似的事情,希望对您仍然有帮助.

我的设置片段:

 使用Android.OS;使用Android.Views;使用Android.Util;使用Android.Preferences;使用Android.Support.V7.Preferences;命名空间Aprepara.Droid.Activities.Fragments{公共类SettingsFragment:PreferenceFragmentCompat{私人检视_view;公共重写void OnCreatePreferences(捆绑的saveInstanceState,字符串rootKey){//AddPreferencesFromResource(Resource.Xml.preferences);}公共重写void OnCreate(Bundle savedInstanceState){base.OnCreate(savedInstanceState);AddPreferencesFromResource(Resource.Xml.preferences);}}} 

在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"/> 

在首选项xml中.

 <?xml version ="1.0" encoding ="utf-8"?>< android.support.v7.preference.PreferenceScreenxmlns:android ="http://schemas.android.com/apk/res/android">< android.support.v7.preference.PreferenceCategoryandroid:title =类别1">< android.support.v7.preference.SwitchPreferenceCompatandroid:key ="key1"android:title =切换偏好设置"android:summary =开关摘要"android:defaultValue ="true"/>< android.support.v7.preference.EditTextPreferenceandroid:key ="key2"android:title ="EditText首选项"android:summary ="EditText摘要"android:dialogMessage =对话讯息"android:defaultValue =默认值"/>< android.support.v7.preference.CheckBoxPreferenceandroid:key ="key3"android:title ="CheckBox首选项"android:summary =复选框摘要"android:defaultValue ="true"/></android.support.v7.preference.PreferenceCategory></android.support.v7.preference.PreferenceScreen> 

Styles.xml

 <?xml version ="1.0" encoding ="utf-8"?><资源>....< 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> 

我的项目的文件夹结构

我正在使用示例

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>

解决方案

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

这篇关于ViewPager中的Android PreferenceFragment的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆