preference项目被自动重新设置? [英] Preference items being automatically re-set?
问题描述
这看起来像我的一个错误:当您在preference片段加载许多开关preferences,他们莫名其妙地重新设置自己,当你滚动preferences。我已分别测试了这个很少有演示code:
This appears like a bug to me: When you load many switch preferences in a preference fragment, they somehow re-set themselves , when you scroll the preferences. I have separately tested this with little demo code:
/res/xml/$p$pfs.xml
(只是一堆开关preferences,就足以让preferences滚动在屏幕上)
/res/xml/prefs.xml
(Just a bunch of switch preferences, just enough to make preferences scroll on screen) :
<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/$p$pfs.java
(一个简单的 preferenceFragment
):
/src/Prefs.java
(A simple 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
的活动布局):
/res/layout/main.xml
(Placed PreferenceFragment
in Activity layout) :
<?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
(演示活动):
/src/MyActivity.java
(Demo 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);
}
}
问题:如果您更改第一个开关preference,向下滚动,滚动备份,设备复位。这同样适用于其他的开关preferences其中滚动拿出来看,后来被访问。 (特别是在水平方向)
Problem: If you change the first switch preference , scroll down, scroll back up, the switch is reset. Same is true for other switch preferences which scroll out of view and are visited later. (specially, in horizontal orientation)
在模拟器发生得。我对编译平台版本15,ICS。正如你可以看到上面的code,这是一个非常简单的设置,我找不到这个code什么,这或许可以解释为什么发生这种情况。
Happens on emulator too. I'm compiling on platform version 15, ICS. As you can see in above code, this is a very simple setup, I can't find anything in this code, that might explain why this is happening.
错误报告发行26194 。
这应该是固定在Android的大号版本。
It is supposed to be fixed in android L release.
推荐答案
我是能够重现此问题。我还发现了一个解决办法,但我不知道为什么它的工作原理:)
I was able to reproduce this issue. I also found a workaround but I don't know why it works :)
创建一个派生类开关preference
像这样:
Create a derived class from SwitchPreference
like so:
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使用这些:
Then, instead of using these in your prefs.xml:
<SwitchPreference ... />
您可以使用这些替代
<com.example.preflistbug.Pref ... />
的推导似乎以某种方式解决了问题,其视图回收的的ListView
驱动的preference列表被重用的控件没有从他们的$解放出来p $ pvious preference
对象第一个(或因此我认为)。如果我想出更多的我会更新这个答案。
The derivation seems to somehow fixes the issue where the view recycling in the ListView
-driven preference list is reusing the controls without "freeing" them from their previous Preference
object first (or so I think). I'll update this answer if I figure out more.
这篇关于preference项目被自动重新设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!