这对我来说似乎是一个错误:当您在首选项 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
(在 Activity 布局中放置 PreferenceFragment
):
<?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/15632215/