android - CoordinatorLayout 行为是否只能应用于 CoordinatorLayout 的直接子项?

标签 android android-coordinatorlayout

根据我目前的阅读,Behavior 只能应用于 CoordinatorLayout 的直接子级。但是下面的代码让我感到困惑:

<?xml version="1.0" encoding="utf-8"?>
<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=".ScrollingActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/app_bar_height"
        android:fitsSystemWindows="true"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="@android:color/holo_red_light"
            app:layout_scrollFlags="scroll"
            app:toolbarId="@+id/toolbar">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="parallax"
                app:popupTheme="@style/AppTheme.PopupOverlay" />

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


    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v4.widget.NestedScrollView
            android:id="@+id/nestedScrollView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            tools:context=".ScrollingActivity"
            tools:showIn="@layout/activity_scrolling">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/text_margin"
                android:text="@string/large_text" />

        </android.support.v4.widget.NestedScrollView>
    </FrameLayout>
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Test Button"
        android:visibility="visible"
        android:layout_gravity="bottom"
        app:layout_anchor="@id/nestedScrollView"
        app:layout_behavior="am.i.coord.CustomBehavior"
        />

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

NestedScrollView 不是 CoordinatorLayout 的直接子级(包裹在 FrameLayout 中),但仍应用了 Behavior,即正确触发 AppBarLayout 中的更改。

只有当我将它设为 CoordinatorLayout 的直接子级时,我在同一级别添加的具有自定义 behaviorButton 才有效。如果我将它作为 NestedScrollView 的同级移动到 FrameLayout 中,则不会执行任何回调。

为什么会有这种差异? 如果 Behavior 不打算被 CoordinatorLayout 的 非直接子级使用,是否有另一种方法让非直接子级在 中的滚动变化时得到通知code>NestedScrollView? 我只能考虑向 CoordinatorLayout 添加一个虚拟 View ,它将向间接子项发出滚动事件,但这会使代码变得困惑。

最佳答案

是的,行为必须附加到 CoordinatorLayout 的直接子级。为什么它适用于 NestedScrollViewIntercepting everything with CoordinatorLayout blog post 中有详细说明,其中指出:

Nested scrolling can originate not only on direct children of a CoordinatorLayout, but on any child View (a child of a child of a child of a CoordinatorLayout, for example)

这是嵌套滚动本身的一个单独属性,而不是 CoordinatorLayout 独有的属性:嵌套滚动事件通过每个父 ViewGroup 在 View 层次结构中向上传播。这就是 CoordinatorLayout 获得对这些事件的访问权限的方式,而 Behaviors 允许 CoordinatorLayout 将这些事件分派(dispatch)给不直接在 ScrollView 的 View 层次结构中的其他子级。

如您所料,如果您希望间接子级了解滚动变化,您必须拥有 CoordinatorLayout 的直接子级,即使它是一个虚拟 View 。当然,您可以创建 CoordinatorLayout 的子类,如果您愿意,可以挂接到相同的事件流中 all of the methods可以被覆盖。

关于android - CoordinatorLayout 行为是否只能应用于 CoordinatorLayout 的直接子项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51432900/

相关文章:

java - Activity 顺序 - 互动书

javascript - 从网页 Java 中删除部分网站

android - 在android中实现MVVM时对网络层的错误处理感到困惑,如何通知用户有问题?

android - 如何在 CollapsingToolbarLayout android 中定期更改图像?

android - 如何正确实现 Snackbar、Coordinator Layout 和 bottom|right FAB?

android - NestedScrollView 在使用 nestedScrollEnabaled 属性时抛出错误 "android.view.InflateException"

javascript - 在 Android Webview 中下载文件

android - Apk 未通过 CTS 测试 : shouldNotFIndUnexpectedIntents, 不知道为什么

android - 将 Snackbar 放在最高的 z 顺序以避免被 AutoCompleteTextView 下拉列表阻止

android - 如何使用 RecyclerView 在 DrawerLayout、Toolbar 和 ViewPager 布局中折叠工具栏?