CollapsingToolbar-与正确的锚定重叠

时间:2018-08-10 16:57:22

标签: java android android-layout android-collapsingtoolbarlayout android-nestedscrollview

我正在尝试创建一个CollapsingToolbarLayout,其中有一个与背景重叠的电影海报。为此,我启用behavior_overlapTop =“ 67dp”,但这会导致奇怪的滚动行为。

enter image description here

滚动时,我的视图从其重叠位置移开。我如何让他们留在原地?

<?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=".MediaItemBaseActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:theme="@style/AppTheme.AppBarOverlay"
        app:elevation="1dp">

        <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="@color/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:toolbarId="@+id/toolbar"
            app:scrimAnimationDuration="250"
            app:scrimVisibleHeightTrigger="100dp">

            <ImageView
                android:id="@+id/backdrop"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                android:scaleType="centerCrop"
                app:layout_collapseMode="parallax"
                app:layout_collapseParallaxMultiplier="0.7" />

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:elevation="20dp"
                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_width="match_parent"
        android:layout_height="match_parent"
        app:behavior_overlapTop="0dp"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:context=".MediaItemBaseActivity"
        tools:showIn="@layout/activity_media_item_base">

        <android.support.constraint.ConstraintLayout
            android:id="@+id/ll_content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:paddingEnd="16dp"
            android:paddingStart="16dp">

            <ImageView
                android:id="@+id/poster"
                android:layout_width="134dp"
                android:layout_height="201dp"
                android:layout_gravity="top|start"
                android:layout_marginTop="-10dp"
                android:elevation="10dp"
                app:layout_anchor="@id/ll_content"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <TextView
                android:id="@+id/surtitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="4dp"
                android:textAllCaps="false"
                android:textSize="14sp"
                app:layout_constraintBottom_toTopOf="@+id/title"
                app:layout_constraintStart_toStartOf="@+id/title" />

            <TextView
                android:id="@+id/title"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="16dp"
                android:layout_marginTop="96dp"
                android:ellipsize="end"
                android:maxEms="9"
                android:maxLines="3"
                android:singleLine="false"
                android:textAllCaps="false"
                android:textColor="@android:color/black"
                android:textSize="20sp"
                android:textStyle="bold"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toEndOf="@+id/poster"
                app:layout_constraintTop_toTopOf="parent" />

            <TextView
                android:id="@+id/subtitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="4dp"
                android:textAllCaps="false"
                android:textSize="14sp"
                app:layout_constraintStart_toStartOf="@+id/surtitle"
                app:layout_constraintTop_toBottomOf="@+id/title" />

            <TextView
                android:id="@+id/body"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/text_margin"
                android:layout_marginTop="8dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/poster" />

            <TextView
                android:id="@+id/credit_title_1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:textStyle="bold"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/body" />

            <TextView
                android:id="@+id/credit_name_1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="8dp"
                app:layout_constraintStart_toEndOf="@id/credit_title_1"
                app:layout_constraintTop_toTopOf="@id/credit_title_1" />

            <TextView
                android:id="@+id/credit_title_2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textStyle="bold"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/credit_name_1" />

            <TextView
                android:id="@+id/credit_name_2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:layout_constraintStart_toStartOf="@+id/credit_name_1"
                app:layout_constraintTop_toTopOf="@id/credit_title_2" />

            <TextView
                android:id="@+id/credit_title_3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textStyle="bold"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/credit_name_2" />

            <TextView
                android:id="@+id/credit_name_3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:layout_constraintStart_toStartOf="@+id/credit_name_2"
                app:layout_constraintTop_toTopOf="@id/credit_title_3" />

        </android.support.constraint.ConstraintLayout>

    </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="@drawable/ic_add_black_24dp" />

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

1 个答案:

答案 0 :(得分:1)

这里发生的是CollapsingToolbar和NestedScrollView都在滚动,这很明显。为了获得您可能想要的效果(类似于FAB,但即使折叠也可以看到视图),您需要扩展CoordinatorLayout.Behaiour类以使自己的行为成为现实。设置behavior_overlapTop="67dp"不会执行任何操作,除了当前似乎正在执行的操作。