PreferenceFragment onPreferenceTreeClick没有触发

时间:2014-02-21 18:49:31

标签: android android-fragments

我在我的应用程序中使用PreferenceFragment。从ListFragment中选择时显示。这门课非常基础:

public class MyPreferencesFragment extends PreferenceFragment {

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // load the preferences from an XML resource
    addPreferencesFromResource(R.xml.preferences);
  }

  @Override
  public void onResume() {
    super.onResume();

    getActivity().setTitle("Preferences");
  }
}

它在小型设备上运行良好。当我在横向平板电脑上运行时会出现问题。如果我单击左侧ListFragment中的Preferences,则右侧会显示MyPreferencesFragment。然后,我可以选择任何首选项并更改其值。滚动首选项列表时出现问题。完成后,单击首选项列表中的任何项目都不会执行任何操作。我补充说:

@Override
public Boolean onPreferenceTreeClick (PreferenceScreen preferenceScreen, Preference preference) {

  System.out.println("preference " + preference.getKey() + " selected");
  return super.onPreferenceTreeClick(preferenceScreen, preference);
}

看看是否会对情况有所了解。当事情正在工作时调用它,但是一旦我滚动列表,就不再调用它。有没有人知道我需要做什么才能在滚动后选择偏好?

另外两个数据点:

  • 纵向工作正常。

  • 如果我单击电源按钮关闭显示屏(显示首选项列表时),当我重新打开屏幕时,即使滚动后我也可以选择项目。此时从列表中重新选择“首选项”会导致原始问题。

编辑:

我评论说它在小型设备上运行良好但似乎有时会出现与小型设备相同的问题。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

事实证明问题出现在我创建的一个Preference类中。我扩展了EditTextPreference类,以确保输入的值是一个数字并在适当的范围内。原来的课程看起来像这样:

public class NumberWithRangePreference extends EditTextPreference {

  private int min = -1;
  private int max = -1;
  private int defaultValue = -1;
  private int oldValue = -1;
  private String units = "";

  public NumberWithRangePreference(Context context, AttributeSet attrs) {
    super(context, attrs);

    for (int i=0; i<attrs.getAttributeCount(); ++i) {
      String attr = attrs.getAttributeName(i);

      if (attr.equals("min")) {
        min = Integer.parseInt(attrs.getAttributeValue(i));
      } else if (attr.equals("max")) {
        max = Integer.parseInt(attrs.getAttributeValue(i));
      } else if (attr.equals("units")) {
        units = attrs.getAttributeValue(i);
      } else if (attr.equals("defaultValue" )) {
        defaultValue = Integer.parseInt(attrs.getAttributeValue(i));
        oldValue = defaultValue;
      }
    }
  }

  @Override
  protected View onCreateView(ViewGroup group) {
    View view = super.onCreateView(group);

    oldValue = Integer.parseInt(PreferenceManager.getDefaultSharedPreferences(getContext()).getString(getKey(), "" + defaultValue));
    setSummary(oldValue + " " + units);
    return view;
  }

  @Override 
  protected void onBindView(View view) {
    super.onBindView(view);

    oldValue = Integer.parseInt(PreferenceManager.getDefaultSharedPreferences(getContext()).getString(getKey(), "" + defaultValue));
    setSummary(oldValue + " " + units);
  }

  @Override
  public void onClick(DialogInterface dialog, int which) {

    if (which == DialogInterface.BUTTON_POSITIVE) {
      int value = Integer.parseInt(getEditText().getEditableText().toString());
      if ((value < min) || (value > max)) {

        getEditText().setText("" + oldValue);

        AlertDialog.Builder errorDialog = new AlertDialog.Builder(getContext());
        errorDialog.setTitle("Invalid Entry");
        errorDialog.setMessage("Value should be between " + min + " and " + max + " " + units);
        errorDialog.setPositiveButton("OK", null);
        errorDialog.show();
      }
    }

    super.onClick(dialog, which);
  }
}

onCreateView和onBindView操作有时会导致PreferenceFragment停止响应项目点击,但我不确定原因。它与setSummary调用有关,因为如果我在onCreateView和onBindView中注释掉那些调用,那么一切都是可选的。

在查看其他一些示例之后,我将类修改为以下内容,现在效果很好:

public class NumberWithRangePreference extends EditTextPreference {

  private int min = -1;
  private int max = -1;
  private int defaultValue = -1;
  private int oldValue = -1;
  private String units = "";

  public NumberWithRangePreference(Context context, AttributeSet attrs) {
    super(context, attrs);

    for (int i=0; i<attrs.getAttributeCount(); ++i) {
      String attr = attrs.getAttributeName(i);

      if (attr.equals("min")) {
        min = Integer.parseInt(attrs.getAttributeValue(i));
      } else if (attr.equals("max")) {
        max = Integer.parseInt(attrs.getAttributeValue(i));
      } else if (attr.equals("units")) {
        units = attrs.getAttributeValue(i);
      } else if (attr.equals("defaultValue" )) {
        defaultValue = Integer.parseInt(attrs.getAttributeValue(i));
        oldValue = defaultValue;
      }
    }

    oldValue = Integer.parseInt(PreferenceManager.getDefaultSharedPreferences(getContext()).getString(getKey(), "" + defaultValue));
    setSummary("" + oldValue + " " + units);
  }

  @Override
  public void onClick(DialogInterface dialog, int which) {

    if (which == DialogInterface.BUTTON_POSITIVE) {
      int value = Integer.parseInt(getEditText().getEditableText().toString());
      if ((value < min) || (value > max)) {

        getEditText().setText("" + oldValue);

        AlertDialog.Builder errorDialog = new AlertDialog.Builder(getContext());
        errorDialog.setTitle("Invalid Entry");
        errorDialog.setMessage("Value should be between " + min + " and " + max + " " + units);
        errorDialog.setPositiveButton("OK", null);
        errorDialog.show();
      } else {
        setSummary("" + value + " " + units);
      }
    }

    super.onClick(dialog, which);
  }    
}