如何在不推送内容的情况下折叠AppBarLayout动画,类似于WhatsApp Android?

Alf*_*sch 7 android android-toolbar android-coordinatorlayout android-appbarlayout

如何在ViewPager片段中滚动RecyclerView时独立折叠工具栏,而不会影响ViewPager的折叠效果?

我的意思是,在WhatsApp Android中,如果您滚动3个主列表中的任何一个,工具栏不会随内容移动,而是它有一个独立的动画轴,非常流畅且不会推送内容视图.您可以看到我向上或向下移动一点,RecycleView没有被工具栏折叠动画推动,它会独立移动:

在此输入图像描述

我在MainActivity布局中有以下层次结构:

<CoordinatorLayout>
    <AppBarLayout>
        <Toolbar/>
        <TabLayout/>
    </AppBarLayout>

    <ViewPager>
        <!-- Fragments with RecyclerView -->
    </ViewPager>

</CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)

activity_main.xml中

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|snap|enterAlways"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

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

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

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:clickable="true"
        app:layout_behavior="ScrollingFABBehavior"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_email" />

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

片段的内容是RecyclerView:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context="fragments.ConversationsFragment">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:scrollbars="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</FrameLayout>
Run Code Online (Sandbox Code Playgroud)

但是当我滚动RecyclerView时,如果我中途停止,工具栏会突然向上或向下推动片段,而不是像WhatsApp那样独立移动动画: 在此输入图像描述

在这个例子中,它更加极端:

在此输入图像描述

如果我不添加snap作为滚动标记,工具栏会中途停止或推送片段,具体取决于我滚动的距离.

app:layout_scrollFlags="scroll|snap|enterAlways"
Run Code Online (Sandbox Code Playgroud)

使用CoordinatorLayout完成任何方法?如果没有,任何指针将不胜感激.

小智 0

使用NestedScrollView实现类似whatsapp的功能,

<android.support.v4.widget.NestedScrollView
    android:id="@+id/scroll"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clipToPadding="false"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

</android.support.v4.widget.NestedScrollView>
Run Code Online (Sandbox Code Playgroud)