Android工具栏显示/隐藏空白区域

Ant*_*ine 5 android toolbar android-actionbar

当用户滚动列表时,我正在尝试隐藏或显示我的工具栏.要做到这一点,我正在使用翻译,但会出现一个空格而不是我的actionBar.如果我使用setVisibility(View.GONE),动画期间将出现空白区域,并在完成时隐藏,这很难看.

这是我的问题的简短视频

以下是我的动画制作方法(来自Google I/O应用程序):

public void showToolbar(boolean show){
    if (show) {
        toolbar.animate()
                .translationY(0)
                .alpha(1)
                .setDuration(HEADER_HIDE_ANIM_DURATION)
                .setInterpolator(new DecelerateInterpolator());
    } else {
        toolbar.animate()
                .translationY(-toolbar.getBottom())
                .alpha(0)
                .setDuration(HEADER_HIDE_ANIM_DURATION)
                .setInterpolator(new DecelerateInterpolator());
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/mainContent">

<include layout="@layout/toolbar"
    android:id="@+id/my_toolbar" />

<fragment
    android:name="com.ar.oe.fragments.SectionsFragment"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="?attr/actionBarSize"/>

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

还有我的工具栏

<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/my_toolbar"
android:layout_height="?attr/actionBarSize"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
/>
Run Code Online (Sandbox Code Playgroud)

Abh*_*uri 1

当用户尝试滚动时,最好为工具栏和下面的片段(一起)显示向上滑动翻译动画,这样只有工具栏离开视图,而片段到达顶部。(例如,200 毫秒内)。为此,请将整个外部相对布局平移约 20%(您可以进行更改,以便只有工具栏移出视图):

在你的动画文件夹中添加slide_up.xml:

<?xml version="1.0" encoding="utf-8"?>
<set
   xmlns:android="http://schemas.android.com/apk/res/android">  
   <translate 
    android:fillAfter="false"
    android:fromYDelta="0%" 
    android:toYDelta="-20%" 
    android:duration="200"/>

</set>
Run Code Online (Sandbox Code Playgroud)

然后,当触发滚动事件时,执行以下操作:

 ...
 RelativeLayout rel = (RelativeLayout)findViewById(R.id.mainContent);
 Animation slideUp = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_up);
 rel.startAnimation(slideUp);
 ...
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助...