包含小于屏幕高度的内容的 NestedScrollView 在 CoordinatorLayout 中滚动

Gra*_*eme 7 android android-viewpager android-coordinatorlayout

我正在尝试实现 Material Design 引入的 ActionBar 滚动内容行为。

我试图在包含 aSlidingTabLayout和 aViewPager作为AppBarLayout. 我已经通过添加app:layout_behavior="@string/appbar_scrolling_view_behavior"到当前状态ViewPager

这导致了两种意想不到的行为:

1)当Fragment内容小于屏幕高度时,仍然会发生垂直滚动(AppBarLayout如果它的邻居不滚动,则不应滚动)。

2)当滚动触摸事件在可触摸子元素上启动时,不会发生滚动。编辑:这里描述了这个问题问题 182549

您可以在此 gif 中看到,Page0 上的第一个 ScrollEvent 显示问题 1,Page0 上的第二个滚动显示问题 2。(奇怪的是,问题 2 在 Page1 上并不明显)。

在此处输入图片说明

我创建了一个完整的演示项目,你可以在 GitHub 上找到它,它展示了这个错误:https : //github.com/gwoodhouse/TestApplication

为了快速参考,我的活动 xml 是

<?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"
                                                 android:layout_width="match_parent"
                                                 android:layout_height="match_parent"
                                                 android:background="@color/teal"
                                                 android:orientation="vertical">


    <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="?android:attr/actionBarSize"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            android:background="@color/teal"
            app:layout_scrollFlags="scroll|enterAlways"/>

        <com.example.graemecastle.testapplication.SlidingTabLayout
            android:id="@+id/slidingTabLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/background"
            app:layout_scrollFlags="scroll|enterAlways"/>

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

    <android.support.v4.view.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/background"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>


</android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)

我的片段 XML 如下:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:orientation="vertical">

        <android.support.v7.widget.CardView
            android:layout_margin="16dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            app:cardBackgroundColor="@color/green"
            app:cardElevation="2dp">

            <LinearLayout

                android:layout_width="wrap_content"
                android:layout_height="?android:attr/actionBarSize"
                android:background="?android:attr/selectableItemBackground"
                android:clickable="true"
                android:gravity="center_vertical"
                android:orientation="horizontal">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="16dp"
                    android:text="Button"
                    android:textColor="#FFFFFF"/>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="8dp"
                    android:layout_marginRight="16dp"
                    android:text=">"
                    android:textColor="#FFFFFF"/>
            </LinearLayout>
        </android.support.v7.widget.CardView>

        <!-- Extra Buttons Added Here to show how view's which should scroll scroll correctly -->
    </LinearLayout>
</android.support.v4.widget.NestedScrollView>
Run Code Online (Sandbox Code Playgroud)

到目前为止,捕获onInterceptTouchEvent()还没有让我清楚发生了什么。有谁有想法吗?