gpt4 book ai didi

android - 在 Android Lollipop 中扩展 Preference 类 = 丢失动画

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:17:38 25 4
gpt4 key购买 nike

只是为了扩展CheckBoxPreferenceSwitchPreference在 Android Lollipop 上,小部件(复选框或开关)将不再有动画。

我想扩展 SwitchPreference 以强制 api < 21 使用 SwitchCompat 而不是他们正在使用的默认设置(这显然是错误的)。

我正在使用新的 AppCompatPreferenceActivityappcompat-v7:22.1.1但这似乎并不影响开关。

问题是,只要扩展这些类,而不添加任何自定义布局或小部件资源布局,动画就消失了。

我知道我可以写两个我的 preference.xml 实例(在 values-v21 内部)并且它会工作......但我想知道为什么会这样,如果有人知道没有两个偏好的解决方案.xml.

代码示例:

public class SwitchPreference extends android.preference.SwitchPreference {

public SwitchPreference(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public SwitchPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}

public SwitchPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}

public SwitchPreference(Context context) {
super(context);
}
}

这或与 CheckBoxPreference 相同,然后使用:

<com.my.package.SwitchPreference />

会使 Lollipop 设备中的动画消失。

--

我为 SwitchPreference 尝试的另一件事(我可以通过 CheckBoxPreference 实现)是提供一个具有默认 ID 但 @android:id/switchWidget 的布局is not public同时 @android:id/checkbox是。我也知道我可以使用 <CheckBoxPreference />并提供一个实际上是 SwitchCompat 的小部件布局,但我想避免这种情况(混淆名称)。

最佳答案

看来我找到了解决您的问题的方法。

详尽的解释

SwitchCompat 中,当切换开关时,它会在播放动画之前测试一些函数:getWindowToken() != null && ViewCompat.isLaidOut(this) && isShown().

完整方法:

@Override
public void setChecked(boolean checked) {
super.setChecked(checked);
// Calling the super method may result in setChecked() getting called
// recursively with a different value, so load the REAL value...
checked = isChecked();
if (getWindowToken() != null && ViewCompat.isLaidOut(this) && isShown()) {
animateThumbToCheckedState(checked);
} else {
// Immediately move the thumb to the new position.
cancelPositionAnimator();
setThumbPosition(checked ? 1 : 0);
}
}

通过使用自定义 View 扩展 SwitchCompat,我发现 isShown() 总是返回 false,因为在第三次迭代whileparent == null

public boolean isShown() {
View current = this;
//noinspection ConstantConditions
do {
if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
return false;
}
ViewParent parent = current.mParent;
if (parent == null) {
return false; // We are not attached to the view root
}
if (!(parent instanceof View)) {
return true;
}
current = (View) parent;
} while (current != null);

return false;
}

有趣的是,第三个父级是 Preference 中传递给 getView(View convertView, ViewGroup parent) 的第二个属性,意味着 PreferenceGroupAdapter 没有' 将父级传递给它自己的 getView()。我不知道为什么会发生这种情况,以及为什么这种情况只发生在自定义首选项类中。

出于测试目的,我使用了 CheckBoxPreferenceSwitchCompat 作为 widgetLayout,而且我也没有看到动画。 p>

修复

现在要解决这个问题:只需让您自己的 View 扩展 SwitchCompat,然后像这样覆盖您的 isShown():

@Override
public boolean isShown() {
return getVisibility() == VISIBLE;
}

将此 SwitchView 用于您的 widgetLayout 样式,动画再次工作 :D

样式:

<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">

<item name="android:checkBoxPreferenceStyle">@style/Preference.SwitchView</item>

</style>

<style name="Preference.SwitchView">
<item name="android:widgetLayout">@layout/preference_switch_view</item>
</style>

小部件布局:

<de.Maxr1998.example.preference.SwitchView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null"
android:clickable="false"
android:focusable="false" />

关于android - 在 Android Lollipop 中扩展 Preference 类 = 丢失动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30336635/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com