Android 工具栏使用“循环使用时隐藏”保留空白查看滚动条

Android 工具栏使用“循环使用时隐藏”保留空白查看滚动条,android,android-fragments,toolbar,android-recyclerview,Android,Android Fragments,Toolbar,Android Recyclerview,我试图在RecyclerView滚动条上隐藏我的工具栏,到目前为止它似乎已经隐藏,但它留下了一个白色的空白。我很确定这与我的MainActivity布局的覆盖和FrameLayout中的片段有关 这是我的活动\u main.xml。我需要FrameLayout,因为我正在加载不同的片段,并在导航抽屉中选择了一个项目 <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/a

我试图在RecyclerView滚动条上隐藏我的工具栏,到目前为止它似乎已经隐藏,但它留下了一个白色的空白。我很确定这与我的MainActivity布局的覆盖和FrameLayout中的片段有关

这是我的
活动\u main.xml
。我需要FrameLayout,因为我正在加载不同的片段,并在导航抽屉中选择了一个项目

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:elevation="7dp"
    tools:context=".MainActivity"
    android:fitsSystemWindows="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <include layout="@layout/toolbar" />

        <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#FFFFFF" />

    </LinearLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/header"
        app:menu="@menu/drawer" />

</android.support.v4.widget.DrawerLayout>
最后是
fragment\u main.xml
中的RecyclerView布局:

 <android.support.v7.widget.RecyclerView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingLeft="8dp"
        android:paddingRight="8dp"
        android:orientation="vertical"
        android:scrollbars="vertical"
        android:id="@+id/rv" />
,但我想问一个新问题,因为这次我正在尝试一个新的问题(不要阅读整篇文章,请参阅底部的编辑)。这是他的和实际的


如果这看起来像是我在说“这是我的大便,去解决它吧”,我很抱歉,但我已经花了将近两个小时来寻找可能的解决方案一如既往,我非常感谢您的帮助。

我认为,您用来设置工具栏动画的translateY只会移动屏幕上绘制的内容,而不会移动视图位置本身。这意味着保持工具栏的视图仍然占据线性布局顶部的空间

使用他在他的电脑中使用的相同布局。 也可以使用框架布局。重要的是,将工具栏放在RecyclerView的顶部(xml中的下方),并向RecyclerView添加与工具栏高度相等的顶部填充

<android.support.v7.widget.RecyclerView
  android:clipToPadding="false"
  android:paddingTop="?attr/actionBarSize" />

<android.support.v7.widget.Toolbar
  ..... />

最好的方法是使用新的及其
协调布局

<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" />

或通过。

发布
回收视图的项目布局
。您是否使用了
CardView
?您的项目具有立面效果或边缘阴影效果…这种方法不适用于我,因为我的抽屉布局是我的主要活动中的根元素。xml这里有一个很好的教程,告诉您如何使用RecyclerView进行此操作。但是,这也告诉您在布局中添加paddingTop=actionBarSize。我真的不喜欢这种方法,但我现在用这种方法解决了它。我将标记这个答案,因为它对于任何偶然发现这个问题的人来说都是最正确的。我将LinearLayout更改为FrameLayout,并将工具栏放置在子FrameLayout(用于片段)下方。额外学分:
<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" />