Why SwitchPreference is not showing animation when switching from on to off & vice versa?

前端 未结 3 537
灰色年华
灰色年华 2020-12-29 15:39

I\'ve made a SwitchPreference for my app\'s preferences.

The problem is that the SwitchPreference is not showing animation whe

相关标签:
3条回答
  • 2020-12-29 15:53

    I had this problem too,and I had tried every solution noticed in stackoverflow but still can't solve the questions.It's seems an android bug when you try to extend subclass of Preference,the animation will disappear. See here for their bugtracker entry.

    Finally I found the solution, I create a class extends Preference,and set layout which contain a switch component, and deal with the click event in custom class, the switch's animation works fine.

    xml file mostly like:

    <com.test.MyPreference>
    <!--can define custom_switch_layout.xml base on preference_material.xml-->
    android:layout="@layout/custom_switch_layout"
    android:key="new_switch"
    android:title="pref_switch_title"</com.test.MyPreference>
    
    0 讨论(0)
  • 2020-12-29 15:56

    I had a similar problem with Checkboxes, I imagine it might be the same problem. The problem line seems to be under the parent class, TwoStatePreference's setChecked() function. The notifyChanged() line is actually telling the preference to redraw itself, which means it will suddenly appear as checked. You can extend SwitchPreference and just override that function and comment that line out, it should work just fine, as long as you don't anything else in that preference to redraw itself.

    @Override
    public void setChecked(boolean checked) {
        // Always persist/notify the first time; don't assume the field's default of false.
        final boolean changed = mChecked != checked;
        if (changed || !mCheckedSet) {
            mChecked = checked;
            mCheckedSet = true;
            persistBoolean(checked);
            if (changed) {
                notifyDependencyChange(shouldDisableDependents());
                //notifyChanged();  *******THIS IS REDRAWING THE VIEW IMMEDIATELY
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-29 16:08

    Causes

    I noticed that a few different things could cause the animation to go missing for my SwitchPreference objects:

    1. if the SwitchPreference is the very first Preference in the settings activity.

    2. if I extend the SwitchPreference and use that instead (post describing a similar problem).

    Fixes

    To avoid the first problem, I created a DummyPreference class which I used as the first Preference in the PreferenceScreen instead. Examples below.

    DummyPreference.java

    public class DummyPreference extends Preference
    {
        public DummyPreference(Context context,AttributeSet attrs)
        {
            super(context,attrs);
        }
    
        @Override
        public View getView(View convertView,ViewGroup parent)
        {
            View v = new View(getContext());
            v.setVisibility(View.GONE);
            return v;
        }
    }
    

    pref_whatever.xml

    <?xml version="1.0" encoding="utf-8"?>
    <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
        <com.exaple.DummyPreference/>
        <!-- other preference controls here -->
    </PreferenceScreen>
    

    To avoid the second problem, I had to just resort to using android's plain old Preference classes in the XML, and I moved any extra logic needed into the Activity or Fragment containing the Preference objects.

    I know this is an old post. I'm just hoping that it may help someone else in the future.

    0 讨论(0)
提交回复
热议问题