【问题标题】:Animating view to visible causes undesired animations on other views将视图动画到可见会导致其他视图上出现不需要的动画
【发布时间】:2020-06-24 10:42:11
【问题描述】:

在尝试将视图从消失变为可见时,我遇到了一些奇怪的影响。见附件 gif。

我有一个简单的Switch,它可以切换TextView 的可见性。当TextView 变为可见时,Button 上会出现不希望的动画效果。

我认为这与R.id.group 布局高度有关。请参阅下面的布局。

当我删除ScrollView 并将R.id.group 布局高度更改为match_parent 时,它可以正常工作。但这不是解决方案,因为我最终需要ScrollView

如何避免按钮动画效果?

源代码:

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    Switch sw = view.findViewById(R.id.toggle);
    TextView tv = view.findViewById(R.id.text);
    sw.setOnCheckedChangeListener((button, isChecked) -> {
        int visibility = isChecked ? View.VISIBLE : View.GONE;
        tv.setVisibility(visibility);
    });
}
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:padding="10dp"
    >

    <LinearLayout
        android:id="@+id/group"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:animateLayoutChanges="true"
            android:orientation="vertical"
            >

            <Switch
                android:id="@+id/toggle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="toggle"
                />

            <TextView
                android:id="@+id/text"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Blabla\nblabla\nblabla\nblabla"
                />

        </LinearLayout>

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Self destruct"
            />

    </LinearLayout>
</ScrollView>

【问题讨论】:

    标签: android android-layout animation button android-scrollview


    【解决方案1】:

    毕竟设法在万维网上找到了解决方法。支持Sam Chen's anwser

    基本上,我需要为R.id.group 布局设置LayoutTransition(又名animateLayoutChanges="true"),Button 所在的位置,并启用CHANGING 转换:

    LayoutTransition transition = new LayoutTransition();
    transition.enableTransitionType(LayoutTransition.CHANGING);
    groupLayout.setLayoutTransition(transition);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-26
      相关资源
      最近更新 更多