DropDownPreference setOnPreferenceChangeListener初始化时会自行调用 [英] DropDownPreference setOnPreferenceChangeListener calls itself when initialized
问题描述
我已针对此问题提交了此处的错误,我认为这是平台错误.如果您具有DropDownPreference OnPreferenceChangeListener,则Android似乎会一次选择"第一项,并且只有在首次在一个新的应用程序上打开屏幕时才会选择"一次,从而触发侦听器.
I have filed a bug here on this issue which I believe is a platform bug. If you have a DropDownPreference OnPreferenceChangeListener, Android appears to "choose" the first item once and only once when the screen is first opened on a fresh app, thereby firing the listener.
有没有人看到这种行为,我该如何预防?我尝试放入一个布尔型优先标记以滤除不触发信号,但是现在下拉菜单中的第一个选项是不可选择的,直到我选择另一个选项为止.我相信在幕后,PreferenceFragmentCompat
认为下拉列表中的第一个选项是默认",并且我已尝试在首选项上使用setDefaultValue
和setValueIndex
尝试以编程方式对其进行初始化而无济于事. xml首选项没有设置默认值.
Has anyone seen this behavior and how might I prevent it? I tried putting in a boolean preference flag to filter the misfire out, but now the first option in my dropdown is unselectable until I choose another option. I believe behind the scenes, the PreferenceFragmentCompat
thinks the first option in the dropdown is the "default", and I have tried using setDefaultValue
and setValueIndex
on the preference to try and initialize it programmatically to no avail. The xml preference has no default value set.
我的下拉首选项称为准备时间",它的初始值来自我们的服务器,然后来自本地数据库:
My dropdown preference is called "prep time", and it's initial value comes from our server and then from a local db:
val setting = settings.getSettingByName(preferencesHelper.merchantBranchID, AppConstants.Settings.PREP_TIME)
if (setting != null) {
var prepTime = setting.value
if (prepTime.isEmpty()) {
prepTime = "15"
}
val summaryProvider = Preference.SummaryProvider<DropDownPreference> {
if (prepTime.toInt() == 1)
"$prepTime minute"
else
"$prepTime minutes"
}
val preference = findPreference<DropDownPreference>(AppConstants.Settings.PREP_TIME)
withContext(Dispatchers.Main) {
preference?.summaryProvider = summaryProvider
preference?.setDefaultValue(setting.value)
}
preference?.onPreferenceChangeListener = Preference.OnPreferenceChangeListener { pref, newValue ->
if(prefs.prepTimePreferenceInittedDueToPlatformBug) {
updateSetting(groupID, branchID, setting.id, newValue.toString())
.observe(viewLifecycleOwner, Observer { response ->
response?.let {
Timber.i("Settings put observed, status= " + response.status + ", data=" + response.data)
})
prepTime = newValue.toString()
}
else{
//We need to do this to prevent a false positive for new installs.
//Dropdown preferences exhibit this, which I think is a platform bug, reported:
//https://issuetracker.google.com/issues/156047817
prefs.prepTimePreferenceInittedDueToPlatformBug = true
}
true
}
}
推荐答案
据我所知,这与Spinners有关.某些情况会在不应该发生的情况下触发它,如果它是此错误 >然后它不会被修复.这个有很多荒谬的技巧.这是我的解决方案,从那里的答案之一扩展而来:
As far as I can tell this is related to Spinners. Something triggers it when it shouldn't, and if it is this bug then it will not be fixed. There are many ridiculous hacks to this one. Here is my solution extended from one of the answers there:
var actualSpinnerClick = false;
preference?.onPreferenceClickListener = Preference.OnPreferenceClickListener {
actualSpinnerClick = true
true
}
preference?.onPreferenceChangeListener = Preference.OnPreferenceChangeListener { pref, newValue ->
if(actualSpinnerClick) {
actualSpinnerClick = false;
updateSetting(groupID, branchID, setting.id, newValue.toString())
.observe(viewLifecycleOwner, Observer { response ->
response?.let {
Timber.i("Settings put observed, status= " + response.status + ", data=" + response.data)
}
})
prepTime = newValue.toString()
}
true
}
这篇关于DropDownPreference setOnPreferenceChangeListener初始化时会自行调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!