工具栏不是躲在RecyclerView滚动 [英] Toolbar not hiding on RecyclerView scroll

查看:275
本文介绍了工具栏不是躲在RecyclerView滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图根据 RecyclerView 的滚动,使工具栏在我的应用程序隐藏和显示。这GIF显示了我想要的目的。

我下面的<一个href="https://mzgreen.github.io/2015/06/23/How-to-hideshow-Toolbar-when-list-is-scrolling%28part3%29/"相对=nofollow>本教程并没有得到我想要的结果。这是我的活动的布局:

 &LT; android.support.design.widget.CoordinatorLayout
的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
的xmlns:程序=htt​​p://schemas.android.com/apk/res-auto
机器人:ID =@ + ID / coordinatorLayout
机器人:layout_width =match_parent
机器人:layout_height =match_parent
工具:上下文=。MainActivity
机器人:fitsSystemWindows =真正的&GT;

&LT; android.support.v4.widget.DrawerLayout
机器人:ID =@ + ID /抽屉
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:海拔=7DP&GT;

&LT; android.support.design.widget.AppBarLayout
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:方向=垂直&GT;

    &LT;包括布局=@布局/工具栏/&GT;

&LT; /android.support.design.widget.AppBarLayout>

&LT;的FrameLayout
        机器人:ID =@ + ID /容器
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:后台=#FFFFFF/&GT;

&LT; android.support.design.widget.NavigationView
    机器人:ID =@ + ID / navigation_view
    机器人:layout_height =match_parent
    机器人:layout_width =match_parent
    机器人:layout_gravity =开始
    应用程序:headerLayout =@布局/头
    应用程序:菜单=@菜单/抽屉/&GT;

&LT; /android.support.design.widget.CoordinatorLayout>
 

  

和这里的工具栏布局:

 &LT; XML版本=1.0编码=UTF-8&GT?;
&LT; android.support.v7.widget.Toolbar
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:程序=htt​​p://schemas.android.com/apk/res-auto
    机器人:主题=@风格/ ThemeOverlay.AppCompat.Dark
    机器人:ID =@ + ID /工具栏
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT
    机器人:=了minHeight?ATTR / actionBarSize
    机器人:背景=@色/ ColorPrimary
    应用程序:layout_scrollFlags =滚动| enterAlways/&GT;
 

当我运行此code时,工具栏完全消失。怎么了?

解决方案

如果你的 RecyclerView 里面的片段可以尝试以下code的根视图的片段布局:应用程序:layout_behavior =@字符串/ appbar_scrolling_view_behavior。包含一定的直接子视图中的 CoordinatorLayout

I'm trying to make the Toolbar in my app hide and show based on the RecyclerView's scrolling. This gif shows what I'm trying to achieve.

I'm following this tutorial and not getting the results I'm looking for. Here is my activity's layout:

<android.support.design.widget.CoordinatorLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:fitsSystemWindows="true">

<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:elevation="7dp">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

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

</android.support.design.widget.AppBarLayout>

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

<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.design.widget.CoordinatorLayout>

And here's the Toolbar layout:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:theme="@style/ThemeOverlay.AppCompat.Dark"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"
    android:background="@color/ColorPrimary"
    app:layout_scrollFlags="scroll|enterAlways" />

When I run this code, the Toolbar completely disappears. What's wrong?

解决方案

If your RecyclerView is inside of a fragment try putting the following code in the root view of the fragment layout: app:layout_behavior="@string/appbar_scrolling_view_behavior". The view that contains that must be a direct child of the CoordinatorLayout

这篇关于工具栏不是躲在RecyclerView滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆