2014-02-21 45 views
1

我在我的應用程序中使用PreferenceFragment。它從ListFragment中選擇時顯示。這個課程非常基礎:PreferenceFragment onPreferenceTreeClick沒有觸發

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

看看是否會揭示出這種情況。它在事情正在工作時被調用,但是一旦我滾動列表,它就不再被調用。有人知道我需要做什麼才能在滾動後選擇首選項嗎?

兩個更多的數據點:

  • 它工作在縱向的罰款。

  • 如果我點擊電源按鈕關閉顯示屏(當顯示首選項列表時),當我重新打開屏幕時,即使在滾動後,我也可以選擇項目。從列表中重新選擇首選項會導致原始問題。

編輯:

我評論說,一個小的設備上的偉大工程,但它似乎有時有小的設備相同的問題。有什麼想法嗎?

回答

0

原來問題出在我創建的偏好類之一。我擴展了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); 
    }  
}