Android AppCompat-v21工具栏动画

我想使用新的 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);

截图:

解决方法

你需要的是滚动侦听器.它会检测您是向上还是向下滚动,并相应地隐藏或显示工具栏.也被称为’ Quick Return‘模式.

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

为了隐藏:

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

显示工具栏:

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

为了进一步阅读,您可以参考这个tutorial.It谈论一个浮动动作按钮,但它与工具栏相同的动画.或者在GitHub找到它的代码.

你可以很容易地做到这一点,没有任何外部的图书馆.

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...