Android 工具栏不会以Scrollview作为CoordinatorLayout的子级折叠

Android 工具栏不会以Scrollview作为CoordinatorLayout的子级折叠,android,toolbar,android-scrollview,android-toolbar,coordinator-layout,Android,Toolbar,Android Scrollview,Android Toolbar,Coordinator Layout,我试图在使用CoordinatorLayout时跟踪Google文档,但我对CoordinatorLayout中的ScrollView有一个问题。基本上,当向下滚动时,工具栏通常会用RecyclerView或Listview折叠。现在有了滚动视图,它就不会崩溃了 <android.support.design.widget.CoordinatorLayout android:layout_width="match_parent" android:layout_height=

我试图在使用CoordinatorLayout时跟踪Google文档,但我对CoordinatorLayout中的ScrollView有一个问题。基本上,当向下滚动时,工具栏通常会用RecyclerView或Listview折叠。现在有了滚动视图,它就不会崩溃了

<android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

        <TextView
            android:id="@+id/tv_View"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="@string/filler"
            style="@style/TextAppearance.AppCompat.Large"
            />

    </ScrollView>

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_scrollFlags="scroll|enterAlways"
            />

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

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


滚动视图
协调器布局不配合。您必须使用
NestedScrollView
而不是
ScrollView

使用NestedScrollView将您的ScrollView折叠为协调器布局的子级。 用以下代码替换您的代码:

<android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
                 android:layout_height="match_parent"
                 app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

        <TextView
            android:id="@+id/tv_View"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="@string/filler"
            style="@style/TextAppearance.AppCompat.Large"
            />

    </LinearLayout>
    </android.support.v4.widget.NestedScrollView>

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_scrollFlags="scroll|enterAlways"
            />

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

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


实际答案应该是,
协调布局
不适用于
滚动视图
,因为
滚动视图
没有实现接口
NestedScrollView
是一个带有
NestedScrollingChild
实现的
ScrollView
。如果您想了解更多有关嵌套滚动的信息,我对此做了详细介绍。

使用
协调布局时,请使用
嵌套滚动视图
而不是常规的
滚动视图

要使
折叠工具栏布局
滚动,您可以触发滚动 行为通过设置
NestedScrollView
to*1000dp

android:minHeight="1000dp"
布局:

<android.support.v4.widget.NestedScrollView
app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <!--to trigger scroll behavior-->
    <LinearLayout android:minHeight="1000dp"/>

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

您可以保留
ScrollView
并添加此XML属性:
android:nestedScrollingEnabled=“true”
,这样它就知道作为兄弟姐妹的坐标布局,并记住此属性仅在棒棒糖版本及更高版本中受支持,这确实回答了问题,工具栏不会崩溃,因为
CoordinatorLayout
不支持
ScrollView
,OP可以通过从
ScrollView
切换到
NestedScrollView
@dbugger来获得请求的行为。为什么这个答案不能提供答案?我花了一个多小时试图弄清楚为什么它不能正确滚动。谢谢确认工作-即使在通过supportFragmentManager Transaction替换的嵌套片段中,也非常感谢!NestedScrollView在ViewPager中时也能与CoordinatorLayout正确配合。请发布博客文章中的相关代码,以及它如何解决OP的问题。我只想指出问题的根本原因。接受的解决方案是可以的,但是没有解释为什么用NestedScrollView更改ScrollView可以解决这个问题。我只是说,你的解决方案可能很好,它只需要你的部分的一些代码,想象一下你的博客被删除了(无论什么原因),然后它可能会变得无用。我不太喜欢Android开发,但我或多或少知道堆栈溢出是如何工作的。。。不是100%的必要,但是如果你在你的答案中添加代码(这个和未来的),你的问答会更好。这只是一个建议,如果您继续,我很乐意升级。我可以在
NestedScrollView
上设置
android:layout\u height=“match\u parent”
,使用
NestedScrollView
而不是
CoordinatorLayout
中的
ScrollView
,也适用于
BottomNavigationView
。或者您可以使用
android:nestedScrollingEnabled=“true”