Я попытался отредактировать решение @waterdragon, но это было «отклонено пэром». Не уверен, почему, поскольку он по-прежнему является его решением, но дает конкретный пример.
Подкласс ListPreference
класс и переопределение setValue
и shouldDisableDependence
методы.
setValue
будет звонить notifyDependencyChange(shouldDisableDependents())
после super.setValue
, когда значение фактически изменено.
shouldDisableDependence
возвращает false только в том случае, если текущее значение равно item3 или любое другое желаемое значение, хранящееся в mDepedenceValue
.
package xxx;
import android.content.Context;
import android.content.res.TypedArray;
import android.preference.ListPreference;
import android.util.AttributeSet;
import xxx.R;
public class DependentListPreference extends ListPreference {
private final String CLASS_NAME = this.getClass().getSimpleName();
private String dependentValue = "";
public DependentListPreference(Context context) {
this(context, null);
}
public DependentListPreference(Context context, AttributeSet attrs) {
super(context, attrs);
if (attrs != null) {
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.DependentListPreference);
dependentValue = a.getString(R.styleable.DependentListPreference_dependentValue);
a.recycle();
}
}
@Override
public void setValue(String value) {
String mOldValue = getValue();
super.setValue(value);
if (!value.equals(mOldValue)) {
notifyDependencyChange(shouldDisableDependents());
}
}
@Override
public boolean shouldDisableDependents() {
boolean shouldDisableDependents = super.shouldDisableDependents();
String value = getValue();
return shouldDisableDependents || value == null || !value.equals(dependentValue);
}
}
Обновите attrs.xml:
<attr name="dependentValue" format="string" />
<declare-styleable name="DependentListPreference">
<attr name="dependentValue" />
</declare-styleable>
и внутри ваших предпочтений XML связать его с любым другим предпочтением, которое зависит от него:
<xxx.DependentListPreference
android:key="pref_key_wifi_security_type"
android:title="Wi-Fi Security Type"
app:dependentValue="1"
android:entries="@array/wifi_security_items"
android:entryValues="@array/wifi_security_values" />
<EditTextPreference
android:key="pref_key_wifi_security_key"
android:title="WPA2 Security Key"
android:summary="Tap to set a security key"
android:password="true"
android:dependency="pref_key_wifi_security_type" />
Я пойду на конечности и скажите, что это самый лучший ответ, который я когда-либо видел. –
@Emmanuel: переменные itemList и itemList2 должны быть объявлены окончательными. Во всяком случае, я голосовал, потому что ваш ответ отлично подействовал на меня! –
Было бы возможно, чтобы itemList2 зависел от * hidden * boolean value (предпочтения, которые не отображаются на экране предпочтений), а затем просто установите это скрытое значение в true или false в вашем коде выше? Эффект будет таким же, но я думаю, что было бы немного удобнее, если бы у вас было много предпочтений в зависимости от itemList (а не только одного). Если возможно, как бы вы могли скрыть это значение? – Malabarba