MotionLayout与NavHostFragment结合

时间:2019-07-15 22:21:11

标签: android navigation android-animation android-constraintlayout android-architecture-components

我正在尝试使用MotionLayout结合使用NavHostFragment获得折叠的工具栏动画。动画不起作用。我的应用启动时确实获得了170dp工具栏,但是在NavHostFragment中滚动时,工具栏仍为170dp。 NavHostFragment是否不适合作为touchAnchorId?

我的MainActivity.xml和collapsing_toolbar.xml(MotionScene)在下面。

我尝试了很多事情,例如将NavHostFragment嵌套在滚动视图中。无济于事。这是我的应用的screenshot

<?xml version="1.0" encoding="utf-8"?>

<androidx.constraintlayout.motion.widget.MotionLayout 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:id="@+id/motion_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="?attr/background_fragments"
    app:currentState="@id/start"
    app:layoutDescription="@xml/collapsing_toolbar"
    tools:showPaths="true"
    tools:theme="@style/AppThemeDark">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:elevation="16dp"
        android:minHeight="?attr/actionBarSize"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:menu="@menu/menu_toolbar"
        android:background="?attr/background_color_toolbar"
        app:titleTextColor="?attr/text_color_toolbar"/>

    <fragment

        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:isScrollContainer="true"
        app:layout_constraintBottom_toTopOf="@id/bottom_nav"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/toolbar"
        app:navGraph="@navigation/nav_graph" />


    <com.google.android.material.bottomnavigation.BottomNavigationView
        app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior"
        android:id="@+id/bottom_nav"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/background_color_bottom_nav"
        android:elevation="16dp"
        app:itemHorizontalTranslationEnabled="false"
        app:itemIconTint="?attr/icon_color_bottom_nav"
        app:itemTextColor="?attr/icon_color_bottom_nav"
        app:labelVisibilityMode="labeled"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:menu="@menu/bottom_nav_menu" />

</androidx.constraintlayout.motion.widget.MotionLayout>

and

<?xml version="1.0" encoding="utf-8"?>
<MotionScene xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <Transition
        app:constraintSetStart="@+id/start"
        app:constraintSetEnd="@+id/end"
        app:duration="1000"        >

        <OnSwipe
            app:dragDirection="dragUp"
            app:touchAnchorId="@+id/nav_host_fragment"
            app:touchAnchorSide="top"/>

    </Transition>

    <ConstraintSet android:id="@+id/start">
        <Constraint
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="170dp"
            android:background="@drawable/launch_screen_image"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>

    </ConstraintSet>

    <ConstraintSet android:id="@+id/end">
        <Constraint
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:minHeight="?attr/actionBarSize"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:menu="@menu/menu_toolbar"
            app:titleTextColor="?attr/text_color_toolbar"
            />
    </ConstraintSet>

</MotionScene>

0 个答案:

没有答案