Android SwitchPreference работает некорректно в 4.2.2

Любая идея, почему наличие нескольких SwitchPreferences в PreferenceScreen создаст проблему, из-за которой, если вы выберете любое из полей, это приведет к изменению других полей? Эта проблема возникает при тестировании на Nexus 4 с версией 4.2.2, но не на Galaxy S3 с версией 4.0.4. Спасибо за любую помощь, которую вы можете предоставить.

<?xml version="1.0" encoding="utf-8"?>

<PreferenceCategory android:title="@string/description_photo_preference">

    <SwitchPreference
            android:key="use_gallery"
            android:title="@string/title_use_gallery_preference"
            android:summaryOff="@string/summary_dont_use_gallery_as_photo_source"
            android:summaryOn="@string/summary_use_gallery_as_photo_source"
            android:defaultValue="true"
   />

    <SwitchPreference
            android:key="use_camera"
            android:title="@string/title_use_camera_preference"
            android:summaryOff="@string/summary_dont_use_camera_as_photo_source"
            android:summaryOn="@string/summary_use_camera_as_photo_source"
            android:defaultValue="true"
  />
  <SwitchPreference
            android:key="show_last_vin"
            android:title="@string/pref_string"
            android:summaryOff="@string/pref_display__false"
            android:summaryOn="@string/pref_display_true"
            android:defaultValue="true"
  />
 </PreferenceCategory>
<PreferenceCategory android:title="@string/description_photo_quality_settings">
    <ListPreference
        android:key="prefPhotoQuality"
        android:entries="@array/photo_quality_settings"
        android:summary="@string/pref_user_photo_quality_settings"
        android:entryValues="@array/photo_quality_settings_values"
        android:title="@string/description_photo_quality_settings" />
</PreferenceCategory>


person Jaz    schedule 15.07.2013    source источник
comment
могу подтвердить, что столкнулся с этой проблемой на Nexus 4.2.2   -  person AAP    schedule 30.07.2013
comment
У меня то же самое на моем Nexus 7 под управлением 4.2.2.   -  person Eric    schedule 02.08.2013
comment
Это HTC Sensation с Android 4.0.3, такая же проблема.   -  person marczellm    schedule 18.12.2013
comment
Рад видеть, что я не единственный, кто сталкивается с этой проблемой. Телефоны Samsung работают нормально, но у Nexus и MotoX есть проблемы.   -  person FilmiHero    schedule 03.06.2014


Ответы (1)


Похоже, что это известная проблема, о которой сообщает эта запись SO.

Существует обходной путь, который можно найти здесь. .

Вот код обходного пути:

public class CustomSwitchPreference 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 CustomSwitchPreference(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 CustomSwitchPreference(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 CustomSwitchPreference(Context context) {
        super(context, null);
    }

    @Override
    protected void onBindView(View view) {
        // Clean listener before invoke SwitchPreference.onBindView
        ViewGroup viewGroup= (ViewGroup)view;
        clearListenerInViewGroup(viewGroup);
        super.onBindView(view);
    }

    /**
     * Clear listener in Switch for specify ViewGroup.
     *
     * @param viewGroup The ViewGroup that will need to clear the listener.
     */
    private void clearListenerInViewGroup(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.setOnCheckedChangeListener(null);
                return;
            } else if (childView instanceof ViewGroup){
                ViewGroup childGroup = (ViewGroup)childView;
                clearListenerInViewGroup(childGroup);
            }
        }
    }

}
person FilmiHero    schedule 03.06.2014