Android 禁用工具栏滚动

Android 禁用工具栏滚动,android,android-design-library,android-coordinatorlayout,androiddesignsupport,android-appbarlayout,Android,Android Design Library,Android Coordinatorlayout,Androiddesignsupport,Android Appbarlayout,我有当前设置: <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.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>

现在,如果我在包含片段的framelayout中滚动,工具栏就会按我的意愿滑入滑出,一切都会正常工作

现在的要点是,如果我滚动位于framelayout侧面(以及relativelayout内部)的NavView,我想禁用工具栏的滑入和滑出功能

但无论我是否删除所有滚动行为,工具栏都会继续滑入和滑出(唯一禁用它的方法是从appbarlayout中删除滚动标志,但这会禁用所有滑入和滑出工具栏)


请问我在这里遗漏了什么?scolling行为不应该将滚动事件传递给CoordinatorLayout吗?

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

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

请注意,即使此代码有效,在更新设计库时,它也可能会中断。

将导航视图移动到抽屉布局中,并将坐标布局移动到抽屉布局的主要内容中

从文档:

NavigationView通常放置在抽屉布局中


试试这个

将此活动放入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>
 <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>
 <?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>

将此应用程序放入\u bar\u 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>
 <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>
 <?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>

将此内容放入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>
 <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>
 <?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>

对于任何可能感兴趣的人,我就是这样处理这个问题的

我通过复制源中的相关文件创建了导航视图的自定义版本

它们是:

  • NavigationView.java
  • NavigationMenuItem.java
  • NavigationMenuPresente.java
  • NavigationMenuView.java
  • ThemeUtils.java
修复导入,使新的NavigationView指向新复制的文件

最后将这个
setNestedScrollingEnabled(false)
添加到
NavigationMenuView
的构造函数中


这是因为@Michael如何正确地指出
NavigationMenuView
是一个RecyclerView,因此它将滚动事件传递给
NestedScrollingParent
(CoordinatorLayout),通过设置
setNestedScrollingEnabled(false)
我们禁用此行为并获得所需的结果(滚动导航视图不会展开/折叠应用程序栏)

删除这行代码
app:layout\u scrollFlags=“scroll | enterally”
从您的
工具栏中
。在我的情况下起作用。

您应该对代码进行注释或注释,以帮助理解。否则这只会导致复制粘贴开发人员。是的,我没有给出任何答案,但没有进行任何测试。谢谢您的建议。