这对我来说似乎是一个错误:当您在首选项 fragment 中加载许多开关首选项时,当您滚动首选项时,它们会以某种方式重新设置自己。我用很少的演示代码单独测试了这个:
<小时/>/res/xml/prefs.xml
(只是一堆开关首选项,足以使首选项在屏幕上滚动):
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" android:key="my_prefs">
<PreferenceCategory android:key="my_prefs_cat" android:title="Settings">
<SwitchPreference android:key="p1" android:title="p1" android:defaultValue="false" />
<SwitchPreference android:key="p2" android:title="p2" android:defaultValue="false" />
<SwitchPreference android:key="p3" android:title="p3" android:defaultValue="false" />
<SwitchPreference android:key="p4" android:title="p4" android:defaultValue="false" />
<SwitchPreference android:key="p5" android:title="p5" android:defaultValue="false" />
<SwitchPreference android:key="p6" android:title="p6" android:defaultValue="false" />
<SwitchPreference android:key="p7" android:title="p7" android:defaultValue="false" />
<SwitchPreference android:key="p8" android:title="p8" android:defaultValue="false" />
<SwitchPreference android:key="p9" android:title="p9" android:defaultValue="false" />
<SwitchPreference android:key="p10" android:title="p10" android:defaultValue="false" />
</PreferenceCategory>
</PreferenceScreen>
<小时/>
/src/Prefs.java
(一个简单的PreferenceFragment
):
package com.example.preflistbug;
import android.os.Bundle;
import android.preference.PreferenceFragment;
public class Prefs extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefs);
}
}
<小时/>
/res/layout/main.xml
(将 PreferenceFragment
放置在 Activity 布局中):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<fragment android:name="com.example.preflistbug.Prefs"
android:id="@+id/frg_prefs"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
<小时/>
/src/MyActivity.java
(演示 Activity ):
package com.example.preflistbug;
import android.app.Activity;
import android.os.Bundle;
public class MyActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
<小时/>
问题:如果您更改第一个开关首选项,向下滚动,然后向上滚动,开关将重置。对于滚动到 View 之外并稍后访问的其他开关首选项也是如此。 (特别是水平方向)
模拟器上也会发生。我正在平台版本 15 ICS 上进行编译。正如您在上面的代码中看到的,这是一个非常简单的设置,我在这段代码中找不到任何内容,这可能可以解释为什么会发生这种情况。
更新
错误报告为 Issue 26194 .
更新2
它应该在 android L 版本中修复。
最佳答案
我能够重现这个问题。我还找到了一个解决方法,但我不知道它为什么有效:)
从 SwitchPreference
创建派生类,如下所示:
public class Pref extends SwitchPreference {
public Pref(Context context) {
super(context);
}
public Pref(Context context, AttributeSet attrs) {
super(context, attrs);
}
public Pref(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
}
然后,不要在 prefs.xml 中使用这些:
<SwitchPreference ... />
您可以使用这些来代替
<com.example.preflistbug.Pref ... />
派生似乎以某种方式解决了以下问题:ListView
驱动的首选项列表中的 View 回收正在重用控件,而不首先将它们从以前的 Preference
对象中“释放”(或者我认为是这样)。如果我了解更多信息,我会更新此答案。
关于android - 自动重新设置首选项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36609924/