Why SwitchPreference is not showing animation when

2019-02-03 19:47发布

问题:

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

The problem is that the SwitchPreference is not showing animation when I'm switching between on & off, rather, it is switching with a sudden jerk.

Here's preferences.xml file's code:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <SwitchPreference
        android:id="@+id/notification"
        android:key="notification"
        android:title="@string/notification"
        android:defaultValue="true"/>
</PreferenceScreen>

I'm a beginner, so please cooperate & let me know what's wrong here.

Thanks in advance.

回答1:

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.



回答2:

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
        }
    }
}


回答3:

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>