Traté de editar la solución de @waterdragon pero fue "rechazada por pares". No estoy seguro de por qué porque sigue siendo su solución, pero proporciona un ejemplo concreto.
Subclase ListPreference
clase, y reemplaza setValue
y shouldDisableDependence
métodos.
setValue
llamará notifyDependencyChange(shouldDisableDependents())
después de super.setValue
cuando el valor realmente se cambie.
shouldDisableDependence
devuelve falso solo si el valor actual es item3, o cualquier otro valor deseado almacenado en 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);
}
}
actualización de su attrs.xml:
<attr name="dependentValue" format="string" />
<declare-styleable name="DependentListPreference">
<attr name="dependentValue" />
</declare-styleable>
y en el interior de su preferencia xml atar a cualquier otra preferencia que depende de él:
<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" />
Voy a salir en una extremidad y decir que esta es la mejor respuesta que he visto en mi vida. –
@Emmanuel: las variables itemList e itemList2 deben declararse finales. De todos modos, voté, porque tu respuesta funcionó muy bien para mí. –
¿Sería posible que itemList2 dependa de un valor booleano * oculto * (una preferencia que no aparece en la pantalla de preferencias) y luego simplemente establezca este valor oculto en verdadero o falso en su código anterior? El efecto sería el mismo, pero creo que sería un poco más conveniente si tuviera muchas preferencias según itemList (en lugar de solo una). Si es posible, ¿cómo podría ocultar este valor? – Malabarba