PreferenceFragmentCompat

PreferenceFragmentCompat

问题是如何与其他嵌套的PreferenceFragmentCompat一起构建PreferenceFragmentCompat。

主要活动:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    getSupportFragmentManager()
        .beginTransaction()
        .replace(android.R.id.content, new PrefsFragment())
        .commit();
}


PrefsFragment:

public class PrefsFragment extends PreferenceFragmentCompat {

@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
    setPreferencesFromResource(R.xml.preferences, rootKey);
}

public static class GeneralPreferenceFragment extends PreferenceFragmentCompat {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
        setPreferencesFromResource(R.xml.pref_general, rootKey);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        return super.onOptionsItemSelected(item);
    }
}
...


preferences.xml:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<Preference
    app:fragment="com.example.playground.PrefsFragment$GeneralPreferenceFragment"
    android:icon="@drawable/ic_info_black_24dp"
    app:title="@string/pref_header_general"
    app:key="general" />

<Preference
    app:fragment="com.example.playground.PrefsFragment$NotificationPreferenceFragment"
    android:icon="@drawable/ic_notifications_black_24dp"
    app:title="@string/pref_header_notifications"
    app:key="notifications" />

<Preference
    app:fragment="com.example.playground.PrefsFragment$DataSyncPreferenceFragment"
    android:icon="@drawable/ic_sync_black_24dp"
    app:title="@string/pref_header_data_sync"
    app:key="data_sync" />

</PreferenceScreen>


因此,当我单击任何首选项时,什么也没有发生,而我希望加载其他PreferenceFragment。我读过https://developer.android.com/guide/topics/ui/settings/organize-your-settings,其中指出:当用户点击带有关联Fragment的Preference时,将调用接口方法PreferenceFragmentCompat.OnPreferenceStartFragmentCallback.onPreferenceStartFragment()。您应该在此方法中显示新屏幕,并应在周围的“活动”中实现该方法。

但是我想实现在PrefsFragment而不是父Activity中处理所有基础首选项片段的所有逻辑。
可能吗 ?

感谢您的时间和关注。

最佳答案

如果要split your hierarchy into multiple screens,可以看一下直接来自Google samples的以下代码:
您首先以这种方式定义根PreferenceScreen

<?xml version="1.0" encoding="utf-8"?>

<PreferenceScreen
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <Preference
        app:title="@string/basic_preferences"
        app:summary="Sample preferences using basic attributes"
        app:fragment="com.example.androidx.preference.sample.MainActivity$BasicPreferencesFragment"/>

    <Preference
        app:title="@string/widgets"
        app:summary="Sample preferences with different widgets"
        app:fragment="com.example.androidx.preference.sample.MainActivity$WidgetPreferencesFragment"/>

    <Preference
        app:title="@string/dialogs"
        app:summary="Sample preferences that launch dialogs"
        app:fragment="com.example.androidx.preference.sample.MainActivity$DialogPreferencesFragment"/>

    <Preference
        app:title="@string/advanced_attributes"
        app:summary="Sample preferences with advanced attributes"
        app:fragment="com.example.androidx.preference.sample.MainActivity$AdvancedPreferencesFragment"/>

</PreferenceScreen>


然后,您将有一些子PreferenceScreen,如下所示:

<?xml version="1.0" encoding="utf-8"?>

<androidx.preference.PreferenceScreen
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <PreferenceCategory
        app:title="@string/basic_preferences">

        <SwitchPreferenceCompat
            app:key="switch"
            app:title="@string/title_switch_preference"
            app:summary="@string/summary_switch_preference"/>

        <EditTextPreference
            app:key="edittext"
            app:title="@string/title_edittext_preference"
            app:useSimpleSummaryProvider="true"
            app:dialogTitle="@string/dialog_title_edittext_preference"/>

        <ListPreference
            app:key="list"
            app:title="@string/title_list_preference"
            app:useSimpleSummaryProvider="true"
            app:entries="@array/entries"
            app:entryValues="@array/entry_values"
            app:dialogTitle="@string/dialog_title_list_preference"/>

    </PreferenceCategory>

</androidx.preference.PreferenceScreen>


最后,例如,扩展了ActivityAppCompatActivity和扩展了Fragment的内部PreferenceFragmentCompat

package com.example.androidx.preference.sample

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat

class MainActivity : AppCompatActivity(),
        PreferenceFragmentCompat.OnPreferenceStartFragmentCallback {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        supportFragmentManager
                .beginTransaction()
                .replace(R.id.settings, SettingsFragment())
                .commit()
        supportFragmentManager.addOnBackStackChangedListener {
            if (supportFragmentManager.backStackEntryCount == 0) {
                setTitle(R.string.title)
            }
        }
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
    }

    override fun onSupportNavigateUp(): Boolean {
        if (supportFragmentManager.popBackStackImmediate()) {
            return true
        }
        return super.onSupportNavigateUp()
    }

    override fun onPreferenceStartFragment(
            caller: PreferenceFragmentCompat,
            pref: Preference
    ): Boolean {
        // Instantiate the new Fragment
        val args = pref.extras
        val fragment = supportFragmentManager.fragmentFactory.instantiate(
                classLoader,
                pref.fragment,
                args
        ).apply {
            arguments = args
            setTargetFragment(caller, 0)
        }
        // Replace the existing Fragment with the new Fragment
        supportFragmentManager.beginTransaction()
                .replace(R.id.settings, fragment)
                .addToBackStack(null)
                .commit()
        return true
    }

    /**
     * The root preference fragment that displays preferences that link to the other preference
     * fragments below.
     */
    class SettingsFragment : PreferenceFragmentCompat() {
        override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
            setPreferencesFromResource(R.xml.root, rootKey)
        }
    }

    /**
     * A preference fragment that demonstrates commonly used preference attributes.
     */
    class BasicPreferencesFragment : PreferenceFragmentCompat() {
        override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
            setPreferencesFromResource(R.xml.basic_preferences, rootKey)
        }
    }

    /**
     * A preference fragment that demonstrates preferences which contain dynamic widgets.
     */
    class WidgetPreferencesFragment : PreferenceFragmentCompat() {
        override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
            setPreferencesFromResource(R.xml.widgets, rootKey)
        }
    }

    /**
     * A preference fragment that demonstrates preferences that launch a dialog when tapped.
     */
    class DialogPreferencesFragment : PreferenceFragmentCompat() {
        override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
            setPreferencesFromResource(R.xml.dialogs, rootKey)
        }
    }

    /**
     * A preference fragment that demonstrates more advanced attributes and functionality.
     */
    class AdvancedPreferencesFragment : PreferenceFragmentCompat() {
        override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
            setPreferencesFromResource(R.xml.advanced, rootKey)
        }
    }
}

10-08 17:14