为什么使用AndroidX时嵌套的PreferenceScreen打不开?

Why does the nested PreferenceScreen not open when using AndroidX?

我正在使用 AndroidX 的首选项库来管理我的应用程序设置。我的 SettingsFragment 派生自 PreferenceFragmentCompat 并加载以下布局:

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

    <androidx.preference.PreferenceScreen
        android:key="screen_backup_key"
        android:title="@string/local_database">

        <androidx.preference.Preference
            android:key="button_save_key"
            android:title="@string/export" />

        <androidx.preference.Preference
            android:key="button_load_key"
            android:title="@string/_import" />

    </androidx.preference.PreferenceScreen>

    <androidx.preference.Preference
            android:key="screen_about"
            android:title="@string/about" />

</androidx.preference.PreferenceScreen>

布局显示正确,但是当我单击嵌套的 PreferenceScreen 条目时没有任何反应。它之前与 支持库 中的 PreferenceScreen 一起工作,现在已弃用。

我希望看到这个嵌套 PreferenceScreen 的内容。我做错了什么?

AndroidX 的首选项库不再支持它。

Declaring nested hierarchies within the same XML resource using a nested PreferenceScreen is no longer supported. You should use nested Fragment objects instead.

每个单独的屏幕都需要一个单独的 PreferenceFragmentCompat

If you have a large number of Preferences or distinct categories, you can display them on separate screens. Each screen should be a PreferenceFragmentCompat with its own separate hierarchy. Preferences on your initial screen can then link to subscreens that contain related Preferences.

你这样做:

To link screens with a Preference, you can declare an app:fragment in XML, or you can use Preference.setFragment(). Set the full package name of the PreferenceFragmentCompat that should be launched when the Preference is tapped, as shown below:

<Preference
    app:fragment="com.example.SyncFragment"
    .../>

这里是 source 以供进一步阅读。