工具栏不会与作为 CoordinatorLayout 子项的 Scrollview 一起折叠
Toolbar will not collapse with Scrollview as child of CoordinatorLayout
我正在尝试按照 Google 文档中有关使用 CoordinatorLayout 的说明进行操作,但我在使用 CoordinatorLayout 中的 ScrollView 时遇到了问题。基本上,向下滚动时,工具栏通常会与 RecyclerView 或 Listview 一起折叠。现在有了 ScrollView,它就不会崩溃了。
<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>
ScrollView
不配合CoordinatorLayout
。您必须使用 NestedScrollView
而不是 ScrollView
使用 NestedScrollView 将滚动视图折叠为协调器布局的子项。
用此代码替换您的代码:
<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>
实际答案应该是 CoordinatorLayout
不适用于 ScrollView
,因为 ScrollView
没有实施 NestedScrollingChild interface. NestedScrollView
is a ScrollView
with NestedScrollingChild
implementation. If you want to learn more about nested scrolling I made a blog post。
在使用 CoordinatorLayout
.
时使用 NestedScrollView
而不是常规的 ScrollView
To make the CollapsingToolbarLayout
scroll you can trigger the scroll
behavior by setting a minimum height of the child Layout of the
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>
*此处的 SupportDesignDemos 示例:https://github.com/android/platform_development/blob/master/samples/SupportDesignDemos/res/layout/include_appbar_scrollview.xml
您可以保留 ScrollView
并添加这个 XML 属性 : android:nestedScrollingEnabled="true"
这样它就知道 CoordinatorLayout 是同级并记住这个 属性 仅在 lollipop 版本 及更高版本 中受支持。
我正在尝试按照 Google 文档中有关使用 CoordinatorLayout 的说明进行操作,但我在使用 CoordinatorLayout 中的 ScrollView 时遇到了问题。基本上,向下滚动时,工具栏通常会与 RecyclerView 或 Listview 一起折叠。现在有了 ScrollView,它就不会崩溃了。
<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>
ScrollView
不配合CoordinatorLayout
。您必须使用 NestedScrollView
而不是 ScrollView
使用 NestedScrollView 将滚动视图折叠为协调器布局的子项。 用此代码替换您的代码:
<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>
实际答案应该是 CoordinatorLayout
不适用于 ScrollView
,因为 ScrollView
没有实施 NestedScrollingChild interface. NestedScrollView
is a ScrollView
with NestedScrollingChild
implementation. If you want to learn more about nested scrolling I made a blog post。
在使用 CoordinatorLayout
.
NestedScrollView
而不是常规的 ScrollView
To make the
CollapsingToolbarLayout
scroll you can trigger the scroll behavior by setting a minimum height of the child Layout of theNestedScrollView
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>
*此处的 SupportDesignDemos 示例:https://github.com/android/platform_development/blob/master/samples/SupportDesignDemos/res/layout/include_appbar_scrollview.xml
您可以保留 ScrollView
并添加这个 XML 属性 : android:nestedScrollingEnabled="true"
这样它就知道 CoordinatorLayout 是同级并记住这个 属性 仅在 lollipop 版本 及更高版本 中受支持。