【问题标题】:Android AppCompat-v21 Toolbar AnimationAndroid AppCompat-v21 工具栏动画
【发布时间】:2015-02-04 09:43:27
【问题描述】:

我想使用新的 Android 工具栏模式而不是 ActionBar。 我从 appCompat v21 添加了一个工具栏作为 SupportActionBar,现在,我想在滚动 listView 项目时用动画隐藏/显示它。 之前,我使用方法:actionBar.show() 和 actionBar.hide(),它会自动生成动画。但现在,在工具栏中它隐藏和显示没有任何动画。 我该怎么办???

活动布局:

<include
    layout="@layout/toolbar_actionbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="@dimen/actionbar_margin" />

工具栏布局:

<android.support.v7.widget.Toolbar   
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"

android:id="@+id/toolbarActionbar_T_actionToolbar"

android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"

android:background="?attr/colorPrimary"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />

活动 Java:

actionToolbar = (Toolbar) findViewById(R.id.toolbarActionbar_T_actionToolbar);
setSupportActionBar(actionToolbar);

截图:

【问题讨论】:

标签: android android-actionbar android-appcompat material-design


【解决方案1】:

在作为工具栏父级的 xml 中添加以下行。

android:animateLayoutChanges="true"

【讨论】:

  • 快速修复解决方案。动画与 hide() 所做的不同。
  • 完成这项工作,但不会通过从顶部滑动来制作动画。动画淡入淡出。
  • 2019。同样的问题。我试图手动启动动画,但没有成功。父视图上的这个标志就像一个魅力一样解决了,我的代码在没有动画黑客的情况下仍然干净。 :)
【解决方案2】:

你需要的是一个滚动监听器。它会检测您是向上还是向下滚动并相应地隐藏或显示工具栏。也称为“Quick Return”模式。

除了只使用 hide() 和 show() 方法之外,对于动画,你必须这样做:

为了隐藏:

toolbarContainer.animate().translationY(-toolbarHeight).setInterpolator(new AccelerateInterpolator(2)).start();

用于显示工具栏:

 toolbarContainer.animate().translationY(0).setInterpolator(new DecelerateInterpolator(2)).start();

要进一步阅读,您可以参考此tutorial。它谈到了浮动操作按钮,但它与工具栏的动画相同。或者在GitHub 找到它的代码。

您可以非常简单地做到这一点,而无需任何外部库。 :-)

更新

您不再需要手动维护任何侦听器。 Android 的设计支持库使您能够使用纯 XML 来完成此操作。

这是启用快速返回的 XML 片段:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:layout_scrollFlags="scroll|enterAlways" />

</android.support.design.widget.AppBarLayout>

<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

关键在于这一行:

app:layout_scrollFlags="scroll|enterAlways"

您可以阅读更多关于使用设计支持库实现快速返回的信息,here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-23
    相关资源
    最近更新 更多