【问题标题】:Disable toolbar scrolling禁用工具栏滚动
【发布时间】:2016-07-02 18:12:06
【问题描述】:

我有当前的设置:

<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 中删除滚动标志,但这会禁用所有滑入和滑出工具栏)

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

【问题讨论】:

    标签: android android-design-library android-coordinatorlayout androiddesignsupport android-appbarlayout


    【解决方案1】:

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

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

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

    【讨论】:

      【解决方案2】:

      将 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>
      

      【讨论】:

        【解决方案3】:

        试试这个,

        把这个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>
        

        【讨论】:

        • 您应该注释或注释您的代码以帮助理解。否则这只会导致复制粘贴开发人员。
        • 是的,我没有经过任何测试没有给出任何答案。但是感谢您的建议。
        【解决方案4】:

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

        我通过从源中复制相关文件创建了我的 NavigationView 的自定义版本,

        他们是:

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

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

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

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

        【讨论】:

          【解决方案5】:

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-11-11
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多