SwitchPreference Изменение поведения обратного вызова в леденце
В нашем проекте мы используем SwitchPreference, аналогичный настройкам Wi-Fi. Пользователь может переключать значение, нажимая кнопку переключения, и пользователь может видеть дополнительные параметры, щелкая заголовок.
Но это не работает в леденце на палочке. Я вижу некоторые изменения поведения в леденце на палочке.
В киткат:
Когда пользователь нажимает на кнопку переключения, вызывается обратный вызов onPreferenceChanged, а когда пользователь нажимает на заголовок, вызывается onPreferenceClicked.
В леденце: вы нажимаете на кнопку переключения или всегда вызывается заголовок onPreferenceClicked, а затем вызывается onPreferenceChanged. Как я могу получить такое же поведение в леденце? Такое поведение нарушает нашу функциональность.
2 ответа
Я столкнулся с той же проблемой и обнаружил следующее: https://code.google.com/p/android/issues/detail?can=2&start=0&num=100&q=&colspec=ID%20Type%20Status%20Owner%20Summary%20Stars&groupby=&sort=&id=172425
После некоторой попытки я нашел, как реализовать этот обходной путь, который работает в моем случае:
public class MySwitchPreference extends SwitchPreference {
/**
* Construct a new SwitchPreference with the given style options.
*
* @param context The Context that will style this preference
* @param attrs Style attributes that differ from the default
* @param defStyle Theme attribute defining the default style options
*/
public MySwitchPreference(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
/**
* Construct a new SwitchPreference with the given style options.
*
* @param context The Context that will style this preference
* @param attrs Style attributes that differ from the default
*/
public MySwitchPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
/**
* Construct a new SwitchPreference with default style options.
*
* @param context The Context that will style this preference
*/
public MySwitchPreference(Context context) {
super(context, null);
}
@Override
protected void onBindView(View view) {
ViewGroup viewGroup= (ViewGroup)view;
setSwitchClickable(viewGroup);
super.onBindView(view);
}
private void setSwitchClickable(ViewGroup viewGroup) {
if (null == viewGroup) {
return;
}
int count = viewGroup.getChildCount();
for(int n = 0; n < count; ++n) {
View childView = viewGroup.getChildAt(n);
if(childView instanceof Switch) {
final Switch switchView = (Switch) childView;
switchView.setClickable(true);
return;
} else if (childView instanceof ViewGroup){
ViewGroup childGroup = (ViewGroup)childView;
setSwitchClickable(childGroup);
}
}
}
Тогда вам просто нужно использовать свой собственный "MySwitchPreference" непосредственно в SwitchPreference.
В качестве обходного пути я создал собственный класс SwitchPreference
public class CustomSwitchPreference extends SwitchPreference {
public CustomSwitchPreference(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public CustomSwitchPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomSwitchPreference(Context context) {
super(context);
}
@Override
protected View onCreateView(ViewGroup parent) {
View view = super.onCreateView(parent);
LinearLayout switchView = (LinearLayout) ((LinearLayout) view).getChildAt(2);
switchView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final boolean newValue = !isChecked();
if (callChangeListener(newValue)) {
setChecked(newValue);
}
}
});
}
Это выглядит грязно, но работает для меня.