Android AppCompat-v21工具栏动画
我想使用新的Android工具栏模式,而不是ActionBar。 我从appCompat v21添加了一个工具栏作为SupportActionBar,现在,我想在滚动listView项目时用动画隐藏/显示它。 以前,我使用的方法是:actionBar.show()和actionBar.hide(),它会自动设置动画。但现在,它在工具栏中隐藏和显示,而不需要任何动画。 我该怎么办 活动布局:Android AppCompat-v21工具栏动画,android,android-actionbar,android-appcompat,material-design,Android,Android Actionbar,Android Appcompat,Material Design,我想使用新的Android工具栏模式,而不是ActionBar。 我从appCompat v21添加了一个工具栏作为SupportActionBar,现在,我想在滚动listView项目时用动画隐藏/显示它。 以前,我使用的方法是:actionBar.show()和actionBar.hide(),它会自动设置动画。但现在,它在工具栏中隐藏和显示,而不需要任何动画。 我该怎么办 活动布局: <include layout="@layout/toolbar_actionbar"
<include
layout="@layout/toolbar_actionbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/actionbar_margin" />
截图:
在作为工具栏父级的xml中添加下一行
android:animateLayoutChanges="true"
您需要的是一个滚动侦听器。它检测您是向上还是向下滚动,并相应地隐藏或显示工具栏。也称为“”模式 除了只使用hide()和show()方法外,对于动画,您必须这样做: 对于隐藏:
toolbarContainer.animate().translationY(-toolbarHeight).setInterpolator(new AccelerateInterpolator(2)).start();
要显示工具栏,请执行以下操作:
toolbarContainer.animate().translationY(0).setInterpolator(new DecelerateInterpolator(2)).start();
为了进一步阅读,你可以参考这个。它谈论一个浮动的动作按钮,但是对于一个工具栏它是相同的动画。或者在找到它的代码
您无需任何外部库即可轻松完成此操作。:-)
更新
您不再需要手动维护任何侦听器。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" />
您可以阅读有关使用设计支持库实现快速返回的更多信息。请使用代码和屏幕截图。工具栏只是一个
视图组
,因此任何(或者如果您的目标是API级别11+)都可以。@MH。这是对的。使用任何类型的动画。还有一个很棒的库(和其他好的动画)可以快速修复。该动画与hide()所做的不同。它执行该操作,但不通过从顶部滑动来设置动画。动画是淡入淡出的。2019年。同样的问题。我试图手动启动动画,但没有成功。父视图上的此标志像符咒一样解决了,我的代码仍然干净,没有动画攻击。:)
toolbarContainer.animate().translationY(0).setInterpolator(new DecelerateInterpolator(2)).start();
<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"