【问题标题】:Custom Row Adapter in androix.preference.ListPreferenceandroidx.preference.ListPreference 中的自定义行适配器
【发布时间】:2020-10-08 19:38:00
【问题描述】:
Android 中的旧版 ListPreference 附带:
protected void onPrepareDialogBuilder(android.app.AlertDialog.Builder builder)
这可以用来修改显示对话框中的行...
见custom row in a listPreference?
android.preference 库中不再提供此方法
想知道使用 android.preference 支持库时如何在显示的对话框中实现行自定义?
【问题讨论】:
标签:
android
androidx
android-preferences
【解决方案1】:
应覆盖PreferenceFragmentCompat#onDisplayPreferenceDialog(preference: Preference) 以显示自定义首选项对话框:
override fun onDisplayPreferenceDialog(preference: Preference) {
if (preference.key == MY_CUSTOM_DIALOG_KEY) {
if (parentFragmentManager.findFragmentByTag(DIALOG_FRAGMENT_TAG) != null) {
// already displayed to user
return
}
MyCustomPreferenceDialogFragment.newInstance(preference.key).let {
it.setTargetFragment(this, 0)
it.show(parentFragmentManager, DIALOG_FRAGMENT_TAG)
}
} else {
super.onDisplayPreferenceDialog(preference)
}
}
要实现您的自定义首选项对话框片段,您可以查看支持库中的实现:
- PreferenceDialogFragmentCompat