Android 如何在坐标布局中捕获滚动事件

How to capture the scroll event in CoordinateLayout in Android

我有两个工具栏(一个设置为"enterAlways",另一个设置为"scroll")和一个坐标布局中的recyclerview,我想实现的是在recyclerview没有时捕获滚动事件已经滚动了,意思是设置为"enterAlways"的toolbar还在滚动的时候。

但是当我尝试 coordinateLayout.setOnScrollChangeListener() 时,它根本不起作用。

我的布局是这样的:

<android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/list_wrapper">

    <EndlessRecyclerView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
       app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#00ff0000"
        >
        <include      app:layout_behavior="com.viki.android.adapter.AbsListener"
            android:id="@+id/header_upper" layout="@layout/inlcude_channelheader_upper" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_scrollFlags="scroll"/>
        <include android:id="@+id/header_lower" layout="@layout/include_channelheader_lower" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_scrollFlags="enterAlways"/>

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

我如何准确捕获滚动事件以及 dx 和 dy 的值,就像 listview 或 recyclerview 的 onScrollListener 所做的那样。

AppBarLayout 提供了一个侦听器,您可以实现侦听 AppBarLayout 的滚动兄弟姐妹何时移动 up/down。您要做的是实现该侦听器并将其注册到您的 AppBarLayout,如下所示:

 mAppBar.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
        @Override
        public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {

            Log.d("tag_scroll", "recycler_view current offset: "+verticalOffset);
        }
    });