【问题标题】:Preference items being automatically re-set?偏好项目被自动重新设置?
【发布时间】:2013-03-15 23:05:04
【问题描述】:

这对我来说似乎是一个错误:当您在首选项片段中加载许多开关首选项时,当您滚动首选项时,它们会以某种方式重新设置自己。我已经用很少的演示代码单独测试了这个:


/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):

<?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(演示活动):

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);
    }
}

问题:如果您更改了第一个开关首选项,向下滚动,向上滚动,开关将被重置。对于滚动到视图之外并稍后访问的其他开关首选项也是如此。 (特别是水平方向)

也发生在模拟器上。我正在平台版本 15 ICS 上编译。正如您在上面的代码中看到的,这是一个非常简单的设置,我在这段代码中找不到任何东西,这可能解释了为什么会发生这种情况。

更新

错误报告为Issue 26194

更新 2

它应该在 android L 版本中得到修复。

【问题讨论】:

  • 删除 这工作正常..
  • 旋转到横向时似乎不会发生这种情况。

标签: android sharedpreferences fragment


【解决方案1】:

我能够重现此问题。我也找到了一种解决方法,但我不知道它为什么有效:)

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 驱动的首选项列表中的视图回收正在重用控件而不首先将它们从以前的Preference 对象中“释放”出来的问题(或者我认为)。如果我知道更多,我会更新这个答案。

【讨论】:

  • 修复工作,非常聪明的想法:),享受赏金。我知道 ListView 回收与它有关,但实际原因仍然是个谜。我会尝试查找源代码并可能提交错误报告。谢谢。
  • 哇,这太糟糕了,但确实有效。我猜谷歌需要修复这个错误。谢谢你,我自己永远也想不通!
  • 我认为它起作用的原因归结为 Preference 的构造函数类中的这些行。来自 api 17:if (!getClass().getName().startsWith("android.preference")) { // For subclasses not in this package, assume the worst and don't cache views mHasSpecifiedLayout = true; } 或来自 api 23:if (!getClass().getName().startsWith("android.preference") &amp;&amp; !getClass().getName().startsWith("com.android")) { // For non-framework subclasses, assume the worst and don't cache views. mCanRecycleLayout = false; }
猜你喜欢
  • 2015-04-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-27
  • 1970-01-01
  • 1970-01-01
  • 2020-04-02
相关资源
最近更新 更多