Android NestedScrollView 在 CoordinatorLayout 中平滑滚动

标签 android android-scrollview android-coordinatorlayout

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

enter image description here

我需要一个平滑的滚动让用户看到展开的 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 在单独的回调中提供速度和位置,使用它们会导致移动不连贯。

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

关于Android NestedScrollView 在 CoordinatorLayout 中平滑滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38902851/

相关文章:

java - 特殊字符问题

java - listview 1 Activity 用于打开 listview 2 Activity

java - 尝试添加第二个表时应用程序崩溃

java - 对于放置在 Android 应用程序 Assets 文件夹中的文件,JNI C 代码中的 fopen 失败

Android 在 ScrollView 中添加 ListView 并从 ListView 中删除滚动

android - CoordinatorLayout 在 Android Studio 3.1.3 中不起作用?

Android ListView 无法正确缩放

Android ScrollView fillViewport 不工作

android - 协调器布局中自定义 View 的 BringToFront

android - 如何在协调器布局中添加拉动刷新