当RecyclerView滚动时,工具栏隐藏会留下空白的问题

4
我正在尝试在RecyclerView滚动时隐藏我的工具栏,到目前为止,它似乎已经隐藏了,但是它留下了一个白色的空白。我很确定这与我的MainActivity布局和FrameLayout中的片段叠加有关。
以下是我的activity_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>

以下是包含 RecyclerView 的代码片段。

...
@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.fragment_main, container, false);

        getActivity().setTitle("Unit One");

        rv = (RecyclerView) v.findViewById(R.id.rv);
        rv.setHasFixedSize(true);

        llm = new LinearLayoutManager(getActivity());
        rv.setLayoutManager(llm);

        initializeData();
        initializeAdapter();

        rv.addOnScrollListener(showHideToolbarListener = new RecyclerViewUtils.ShowHideToolbarOnScrollingListener(MainActivity.toolbar));

        if (savedInstanceState != null) {
            showHideToolbarListener.onRestoreInstanceState((RecyclerViewUtils.ShowHideToolbarOnScrollingListener.State) savedInstanceState
                    .getParcelable(RecyclerViewUtils.ShowHideToolbarOnScrollingListener.SHOW_HIDE_TOOLBAR_LISTENER_STATE));
        }

        return v;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        outState.putParcelable(RecyclerViewUtils.ShowHideToolbarOnScrollingListener.SHOW_HIDE_TOOLBAR_LISTENER_STATE,
                showHideToolbarListener.onSaveInstanceState());
        super.onSaveInstanceState(outState);
    }

最后,fragment_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" />

这是对我的另一篇文章的跟进,链接在这里。不过我想问一个新问题,因为这次我尝试了不同的方法(不需要阅读整篇文章,只需看底部的编辑部分)。这是他的MainActivity以及实际的滚动监听代码
如果这似乎像是我在说“这是我的问题,你去解决吧”,那我道歉了。但我已经花了将近两个小时寻找可能的解决方案。如常,非常感谢您的帮助。

请贴出您使用的“RecyclerView”的项目布局。您是否使用了“CardView”?您的项目是否具有高度效果或边缘阴影效果? - SilentKnight
2个回答

6
我认为你正在使用translateY来动画化工具栏,它只会移动屏幕上所绘制的内容而不是视图本身。这意味着包含工具栏的视图仍然占据线性布局顶部的空间。
请使用他在main_activity.xml中使用的相同布局。 你也可以使用FrameLayout。重要的是将工具栏放置在RecyclerView之上(在xml文件中下方),并为RecyclerView添加与工具栏高度相等的顶部填充。
<android.support.v7.widget.RecyclerView
  android:clipToPadding="false"
  android:paddingTop="?attr/actionBarSize" />

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

我将标记此答案,因为对于其他遇到此问题的人来说,它是最正确的。我将我的LinearLayout更改为FrameLayout,并将Toolbar放置在子FrameLayout(用于片段)下方。额外的参考资料:https://www.reddit.com/r/androiddev/comments/3ca6gt/stackoverflow_question_problems_with_toolbar/cstn8t6 - wasimsandhu

2
最佳方法是使用新的设计支持库及其CoordinatorLayout: Design Support LibraryCoordinatorLayout
<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" />

更多示例,可以参考Chris Banes的演示应用程序CheeseSquare这篇教程


这种方法对我不起作用,因为我的DrawerLayout是main_activity.xml中的根元素。 - wasimsandhu
这里有一个不错的教程,告诉你如何使用RecyclerView实现它。https://mzgreen.github.io/2015/02/15/How-to-hideshow-Toolbar-when-list-is-scroling(part1)/然而,这也告诉你在布局中添加paddingTop = actionBarSize。我并不是很喜欢这种方法,但目前我是这样解决的。 - JaydeepW

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接