禁用工具栏滚动

14

我有当前的设置:

<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/coordiator_layout_in_main"
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="wrap_content"
        android:minHeight="?attr/actionBarSize"
        app:layout_scrollFlags="scroll|enterAlways"/>
</android.support.design.widget.AppBarLayout>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <android.support.design.widget.NavigationView
        android:layout_width="170dp"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"/>

    <FrameLayout
        // I place a fragment containing a viewpager containing    fragments that contain a recyclerview.... 
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_toRightOf="@+id/nav_view">

    </FrameLayout>
</RelativeLayout>

<FrameLayout
    android:id="@+id/settings_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:visibility="invisible">
</FrameLayout>

<android.support.design.widget.FloatingActionButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_refresh"
    app:layout_anchor="@id/coordiator_layout_in_main"
    app:layout_anchorGravity="bottom|right|end"   
   app:layout_behavior="com.material.widget.MyFloatingActionButtonBehavior" />

</android.support.design.widget.CoordinatorLayout>
现在如果我滚动包含片段的帧布局,工具栏会像我想要的那样滑入和滑出。
现在的问题是,如果我滚动位于帧布局侧面(并且位于相对布局内部)的 NavView,我想禁用工具栏的滑入和滑出。
但是,无论我删除所有滚动行为,工具栏仍然会滑入和滑出(唯一禁用它的方法是从 appbarlayout 中删除滚动标志,但这会禁用 TolBar 的所有滑入和滑出)。
请问我错过了什么?难道滚动行为不应该将滚动事件传递到 CoordinatorLayout 吗?
5个回答

6

试试这个:

将此activity_screen.xml文件放入其中。

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" android:id="@+id/drawer_layout"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:fitsSystemWindows="true" tools:openDrawer="start">

    <include layout="@layout/app_bar_screen" android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView android:id="@+id/nav_view"
        android:layout_width="wrap_content" android:layout_height="match_parent"
        android:layout_gravity="start" android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_home_screen"
        app:itemIconTint="@color/app_theme_color"
        app:menu="@menu/activity_home_screen_drawer" />

</android.support.v4.widget.DrawerLayout>

请将此app_bar_screen.xml文件放置在指定位置。
 <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.test.app.HomeScreenActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay"/>



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

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


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

将此内容_screen.xml放置在相应位置。
 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/app_bar_home_screen"
    android:id="@+id/content_frame"
    android:background="@android:color/white"
    tools:context="com.test.app.HomeScreenActivity">


</RelativeLayout>

你应该注释或标注你的代码以帮助理解。否则这只会导致复制粘贴式的开发者。 - catch22
是的,我没有在进行任何测试之前给出任何答案。但感谢您的建议。 - Pradeep Gupta

6

不幸的是,NavigationView 包含 NavigationMenuView,它是一个 RecyclerView,因此支持嵌套滚动并在滚动时移动 AppBarLayout。解决这个问题的最好方法是将 NavigationView 移出 CoordinatorLayout。如果不可能,您可以尝试以下代码,但我没有测试过。

final RecyclerView navigationMenuView =
    (RecyclerView) findViewById(R.id.design_navigation_view);
navigationMenuView.setNestedScrollingEnabled(false);

请注意,即使此代码可以正常工作,但在更新设计库时可能会出现故障。

6
将您的NavigationView放在DrawerLayout内部,将CoordinatorLayout放在DrawerLayout主内容内部。
文档中指出:

NavigationView通常放置在DrawerLayout中。

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 android:id="@+id/drawer_layout"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:fitsSystemWindows="true">

 <!-- Your contents -->

 <android.support.design.widget.NavigationView
     android:id="@+id/navigation"
     android:layout_width="wrap_content"
     android:layout_height="match_parent"
     android:layout_gravity="start"
     app:menu="@menu/my_navigation_items" />
 </android.support.v4.widget.DrawerLayout>

2
从您的ToolBar中删除这行代码app:layout_scrollFlags="scroll|enterAlways"。在我的情况下有效。

0

对于任何可能感兴趣的人,这是我处理该问题的方法。

我通过从源代码中复制相关文件来创建了自定义版本的NavigationView,它们是:

  • NavigationView.java
  • NavigationMenuItem.java
  • NavigationMenuPresente.java
  • NavigationMenuView.java
  • ThemeUtils.java

修复导入,使新的NavigationView指向新复制的文件。

最后,在NavigationMenuView的构造函数中添加setNestedScrollingEnabled(false)

这是因为,正如@Michael正确指出的那样,NavigationMenuView是一个RecyclerView,因此它将其滚动事件传递给NestedScrollingParent(即CoordinatorLayout),通过设置setNestedScrollingEnabled(false),我们禁用了这种行为,并获得了所需的结果(滚动NavView不会展开/折叠AppBar)。


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