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

前端 未结 3 564
灰色年华
灰色年华 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 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

    
    
        
        
    
    

    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.

提交回复
热议问题