Android 折叠工具栏不像 Google Play 应用那样使用惯性

标签 android android-collapsingtoolbarlayout android-appbarlayout

我正在使用 Android 设计支持库创建一个带有可折叠工具栏的 Activity ,该工具栏具有漂亮的淡入淡出效果,就像 Google Play 或 Whatsapp 的联系人资料一样。我会将 Activity 布局放在最后,但请记住,这只是默认的可折叠 Activity 布局,我将 ImageView 添加到 AppBarLayout 以创建工具栏 <-> 图像淡入淡出效果。

我在这个实现中遇到的问题表现为我将描述的 2 个症状:

  • Activity 内容很长,当我想通过快速滑动快速向上滚动时,滚动会在展开工具栏之前停止。我希望它继续,当我在我的 NestedScrollView 的底部时,我快速手指滑动以一直到我的 Activity 的顶部我希望这个滚动去展开工具栏,这就是Google Play 应用的行为或 Whatsapp 的个人资料的行为。

  • 同样,当工具栏展开时,滚动没有惯性,快速向下滑动会滚动一点点,这同样不是 Google Play 或 Whatsapp 配置文件的行为方式。折叠工具栏后,滚动的行为与它在 ScrollView、ListView 等中的始终一样。快速滑动可让您到达底部或顶部(除非有很多内容)。

设计支持库支持我描述的行为吗?

Activity .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=".ScrollingActivity">

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

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/toolbar_layout"
            android:fitsSystemWindows="true"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:contentScrim="?attr/colorPrimary">

            <ImageView
                android:src="@drawable/cuthbert"
                app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:scaleType="centerCrop"
                app:layout_collapseMode="parallax"
                android:minHeight="100dp"/>

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

    <include layout="@layout/content_scrolling"/>

    <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"
        android:src="@android:drawable/ic_dialog_email"/>

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

content_scrolling.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/activity_scrolling"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ScrollingActivity">

    <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.CollapsingToolbarLayout
   app:expandedTitleMarginEnd="64dp"
   app:expandedTitleMarginStart="48dp"
   app:expandedTitleTextAppearance="@android:color/transparent"

关于Android 折叠工具栏不像 Google Play 应用那样使用惯性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32910287/

相关文章:

android - onScroll 事件未针对 ListView 触发

android - 导航栏隐藏的 NestedScrollView 底部 [Android]

android - AppBarLayout 中的工具栏是可滚动的,尽管 RecyclerView 没有足够的内容可以滚动

android - 在模拟器上安装 com.google.android.gms,数据包存在错误

android - 从 Activity 向屏幕打印一个整数

android - CollapsingToolbarLayout 中的折叠状态下未显示后退按钮

android - AppBarLayout with RecyclerView 可以在不让 RecyclerView 过度滚动的情况下工作吗?

android - CollapsingToolbarLayout 禁用下拉扩展

android - 连接时自动启动 MTK 设备(非三星)

android - 如何同步两个 CoordinatorLayout + AppBarLayout 的滚动