AppBarLayout 中的动画视图

Animating views in AppBarLayout

我在 coordinatorLayout 中有一个 AppBarLayout,如下所示:

<coordinatorLayout>
<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.design.widget.TabLayout
        android:id="@+id/scrollable_category_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_scrollFlags="scroll|enterAlways|snap"
        app:tabGravity="center"
        app:tabMode="scrollable" />

    <RelativeLayout
        android:id="@+id/parent_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="100dp"
        android:background="@android:color/white"
        android:visibility="visible">

        <few more layout/>
    </RelativeLayout>
</android.support.design.widget.AppBarLayout>

<more views>
</coordinatorLayout>

我希望默认情况下隐藏 relativeLayout,如果 API 给出特定响应,那么我将显示 RelativeLayout 及其内容。根据 API 响应,内容可以有不同的高度(例如,文本可以是单行或多行等)。所以一开始我不知道视图的高度。现在我想将 translationY 应用于视图,使其看起来像是来自 tabLayout 下方(显示阴影等)。

我试过很多解决方案,其中之一是:

adParentLayout.animate()
.translationY(0)
.setDuration(5000)
.withEndAction(new Runnable() {
        @Override
        public void run() {
            Toast.makeText(MainActivity.this, "Animation Complete", Toast.LENGTH_SHORT).show();
        }
    })
.withStartAction(new Runnable() {
        @Override
        public void run() {
            adParentLayout.setVisibility(View.VISIBLE);
            adParentLayout.setTranslationY(-adParentLayout.getHeight());
        }
    });

这显然行不通。我不太了解动画,希望得到建议

我尝试过的一个解决方案是使视图在布局中可见,然后应用转换。但这使得视图在 TabLayout 上方完全覆盖它。原来是 AppBarLayout 继承了 LinearLayout,所以第二个 Layout RelativeLayout 翻译在它上面。我不明白如何实现这种效果,任何正确方向的输入都会有所帮助。

我在使用 LayoutTransition 时得到了它。基本上我只是在 appBarLayout 中添加了 android:animateLayoutchanges=true 并且它处理了视图可见性的动画。

我不知道这件事。似乎是一件很棒的事情。