【问题标题】:Animating views in AppBarLayoutAppBarLayout 中的动画视图
【发布时间】:2016-09-19 16:55:04
【问题描述】:

我在 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,所以第二个LayoutRelativeLayout在它上面翻译。我不明白这种效果是如何实现的,任何正确方向的输入都会有所帮助。

【问题讨论】:

    标签: android android-layout android-animation android-coordinatorlayout android-appbarlayout


    【解决方案1】:

    我让它与 LayoutTransition 一起工作。基本上我只是在 appBarLayout 中添加了 android:animateLayoutchanges=true ,它处理了视图可见性的动画。

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

    【讨论】:

      猜你喜欢
      • 2015-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多