禁用工具栏滚动
Disable toolbar scrolling
我有当前设置:
<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 中删除滚动标志,但禁用所有滑入和滑出工具栏)
请问我在这里遗漏了什么?滚动行为不是应该将滚动事件传递给 CoordinatorLayout 吗?
不幸的是,NavigationView
包含 NavigationMenuView
即 RecyclerView
,因此它支持嵌套滚动并在滚动时移动 AppBarLayout
。解决此问题的最佳方法是将 NavigationView
移出 CoordinatorLayout
。如果不行你可以试试下面的代码,我还没有测试过。
final RecyclerView navigationMenuView =
(RecyclerView) findViewById(R.id.design_navigation_view);
navigationMenuView.setNestedScrollingEnabled(false);
请注意,即使此代码有效,它也可能在更新设计库时中断。
将 NavigationView 移到 DrawerLayout 中,将 CoordinatorLayout 移到 DrawerLayout 的主要内容中。
来自文档:
NavigationView is typically placed inside a 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>
试试这个,
放这个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>
放这个content_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>
对于可能感兴趣的任何人,这就是我处理问题的方式。
我通过从源代码复制相关文件创建了我的 NavigationView 自定义版本,
他们是:
- NavigationView.java
- NavigationMenuItem.java
- NavigationMenuPresente.java
- NavigationMenuView.java
- ThemeUtils.java
修复导入,使新的 NavigationView 指向新复制的文件。
最后将这个 setNestedScrollingEnabled(false)
添加到 NavigationMenuView
的构造函数中。
这是因为,@Michael 如何正确指出 NavigationMenuView
是一个 RecyclerView,因此它通过设置 [=10] 将其滚动事件传递给 NestedScrollingParent
(CoordinatorLayout) =] 我们禁用此行为并获得所需的结果(滚动 NavView 不会 expand/collapse AppBar)
从您的 ToolBar
中删除这行代码app:layout_scrollFlags="scroll|enterAlways"
。在我的案例中有效。
我有当前设置:
<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 中删除滚动标志,但禁用所有滑入和滑出工具栏)
请问我在这里遗漏了什么?滚动行为不是应该将滚动事件传递给 CoordinatorLayout 吗?
不幸的是,NavigationView
包含 NavigationMenuView
即 RecyclerView
,因此它支持嵌套滚动并在滚动时移动 AppBarLayout
。解决此问题的最佳方法是将 NavigationView
移出 CoordinatorLayout
。如果不行你可以试试下面的代码,我还没有测试过。
final RecyclerView navigationMenuView =
(RecyclerView) findViewById(R.id.design_navigation_view);
navigationMenuView.setNestedScrollingEnabled(false);
请注意,即使此代码有效,它也可能在更新设计库时中断。
将 NavigationView 移到 DrawerLayout 中,将 CoordinatorLayout 移到 DrawerLayout 的主要内容中。
来自文档:
NavigationView is typically placed inside a 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>
试试这个,
放这个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>
放这个content_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>
对于可能感兴趣的任何人,这就是我处理问题的方式。
我通过从源代码复制相关文件创建了我的 NavigationView 自定义版本,
他们是:
- NavigationView.java
- NavigationMenuItem.java
- NavigationMenuPresente.java
- NavigationMenuView.java
- ThemeUtils.java
修复导入,使新的 NavigationView 指向新复制的文件。
最后将这个 setNestedScrollingEnabled(false)
添加到 NavigationMenuView
的构造函数中。
这是因为,@Michael 如何正确指出 NavigationMenuView
是一个 RecyclerView,因此它通过设置 [=10] 将其滚动事件传递给 NestedScrollingParent
(CoordinatorLayout) =] 我们禁用此行为并获得所需的结果(滚动 NavView 不会 expand/collapse AppBar)
从您的 ToolBar
中删除这行代码app:layout_scrollFlags="scroll|enterAlways"
。在我的案例中有效。