Android 应用于ScrollView的MotionScene中的OnSwipe过渡不起作用

Android 应用于ScrollView的MotionScene中的OnSwipe过渡不起作用,android,scrollview,android-motionlayout,Android,Scrollview,Android Motionlayout,目前,我正在尝试理解MotionLayout(在“测试应用程序”中),以便将其应用于更大的应用程序 我尝试的是模拟可折叠工具栏 我做了一个布局,有两张图片,还有ScrollView: 布局 <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.motion.widget.MotionLayout xmlns:android="http://schemas.android.com/apk/res/and

目前,我正在尝试理解MotionLayout(在“测试应用程序”中),以便将其应用于更大的应用程序

我尝试的是模拟可折叠工具栏

我做了一个布局,有两张图片,还有ScrollView:

布局

<?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_l"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layoutDescription="@xml/motion_test"
    tools:context=".MainActivity"
    tools:showPaths="true">

    <ImageView
        android:id="@+id/background_image"
        android:layout_width="0dp"
        android:layout_height="200dp"
        android:scaleType="centerCrop"
        android:src="@drawable/img_back"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent " />

    <TextView
        android:id="@+id/title"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"
        android:layout_marginBottom="16dp"
        android:text="Awsome title"
        android:textColor="#ffffff"
        app:layout_constraintBottom_toBottomOf="@id/background_image"
        app:layout_constraintEnd_toEndOf="parent "
        app:layout_constraintStart_toStartOf="parent" />

    <ImageView
        android:id="@+id/back_button"
        android:layout_width="32dp"
        android:layout_height="32dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:scaleType="centerCrop"
        android:src="@drawable/ic_launcher_background"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent " />

    <ScrollView
        android:id="@+id/scroll_view"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/background_image">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_constraintBottom_toTopOf="@+id/text1"
            app:layout_constraintTop_toTopOf="@+id/text1">

            <TextView
                android:id="@+id/text1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_margin="8dp"
                android:text="@string/text_test"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <TextView
                android:id="@+id/text2"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_margin="8dp"
                android:text="@string/text_test"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/text1" />
        </androidx.constraintlayout.widget.ConstraintLayout>

    </ScrollView>

    <ImageView
        android:id="@+id/logo"
        android:layout_width="64dp"
        android:layout_height="64dp"
        android:elevation="4dp"
        android:scaleType="centerCrop"
        android:src="@drawable/img_logo"
        app:layout_constraintBottom_toBottomOf="@id/background_image"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/background_image" />

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

然后,我创建了MotionScene布局来处理过渡:

运动场景xml

<?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:constraintSetEnd="@id/collapsed"
        app:constraintSetStart="@id/expanded">

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

    </Transition>

    <ConstraintSet android:id="@+id/collapsed">

        <Constraint
            android:id="@id/background_image"
            android:layout_height="?attr/actionBarSize"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">
            <CustomAttribute
                app:attributeName="imageAlpha"
                app:customIntegerValue="0" />
        </Constraint>

        <Constraint
            android:id="@id/logo"
            android:layout_width="64dp"
            android:layout_height="64dp"
            android:visibility="gone"
            app:layout_constraintBottom_toBottomOf="@id/background_image"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/background_image">
            <CustomAttribute
                app:attributeName="imageAlpha"
                app:customIntegerValue="0" />
        </Constraint>

        <Constraint
            android:id="@id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="@id/background_image"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="@id/background_image">
            <CustomAttribute
                app:attributeName="textColor"
                app:customColorValue="#000000" />
        </Constraint>
    </ConstraintSet>

    <ConstraintSet android:id="@+id/expanded">
        <Constraint
            android:id="@id/background_image"
            android:layout_height="200dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">
            <CustomAttribute
                app:attributeName="imageAlpha"
                app:customIntegerValue="255" />
        </Constraint>

        <Constraint
            android:id="@id/logo"
            android:layout_width="64dp"
            android:layout_height="64dp"
            android:visibility="visible"
            app:layout_constraintBottom_toBottomOf="@id/background_image"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/background_image">
            <CustomAttribute
                app:attributeName="imageAlpha"
                app:customIntegerValue="255" />
        </Constraint>

        <Constraint
            android:id="@id/title"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginEnd="16dp"
            android:layout_marginBottom="16dp"
            android:scaleX="1.0"
            android:scaleY="1.0"
            app:layout_constraintBottom_toBottomOf="@id/background_image"
            app:layout_constraintEnd_toEndOf="parent "
            app:layout_constraintStart_toStartOf="parent">
            <CustomAttribute
                app:attributeName="textColor"
                app:customColorValue="#ffffff" />
        </Constraint>
    </ConstraintSet>

</MotionScene>

这就是我的问题所在。动画效果很好,但前提是我在ImageView中做手势。如果在ScrollView中执行该手势,则忽略转换

正如您在
onswip
中所看到的,
touchAnchorID
应用于滚动视图

可能无法应用于ScrollView?在这种情况下,可以应用哪种解决方案


因为在“真实项目”中,视图已使用ScrollView完成。

您找到解决方案了吗?您好@Vodet!事实上并不是这样,最终在项目中讨论了这种类型的“功能”,所以我停止了寻找解决方案。也许在将来,我必须回来,因为设计师们正在谈论同样的行为。对不起@Shudy您是否尝试过用嵌套的ScrollView替换ScrollView?用你的例子试过了,似乎效果很好。