Android NestedScrollView 在 CoordinatorLayout 中平滑滚动

Android NestedScrollView smooth scroll in CoordinatorLayout

我正在使用示例 ScrollingActivity(包含在 SDK 中)来测试视差行为。该示例在 CoordinatorLayout 中使用 NestedScrollView。当我从屏幕底部向上滚动时;滚动在工具栏处停止(即使我的滚动速度很高)。正如您在附图中看到的那样,需要多次滚动才能显示展开的 AppBarLayout。

我需要平滑的滚动让用户看到展开的 AppBarLayout。有趣的是,如果我使用 RecyclerView 而不是 NestedScrollView,这个问题就不会发生。

我正在使用构建工具 23.0.3。这是布局 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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="me.deepakmishra.swipetests.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="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

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

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

    <android.support.v4.widget.NestedScrollView
        android:layout_gravity="fill_vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:context="me.deepakmishra.swipetests.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>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/fab_margin"
        app:layout_anchor="@id/app_bar"
        app:layout_anchorGravity="bottom|end"
        app:srcCompat="@android:drawable/ic_dialog_email" />

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

我无法使用 CoordinatorLayout 实现此目的,主要是因为我无法获得同时封装速度和位置的事件。 CoordinatorLayout 在单独的回调中提供速度和位置,使用它们会导致移动不连贯。

我使用自定义处理程序以传统方式实现了视差效果,以跟踪所有 scroll/fling 操作。

将 android:fillViewport="true" 和 android:layout_gravity="fill_vertical" 添加到您的 NestedScrollView。希望对您有所帮助。