MotionLayout:单击和触摸在运动场景中的过渡时不起作用(像 UI 一样的 Youtube 播放器)

Poy*_*nan 6 android android-layout android-constraintlayout android-motionlayout

我有一个类似视图的 Youtube 视频播放器,我有一个列表,点击后会在下一个屏幕中播放视频。

我已将 Motionscene 添加到视频视图中,因此在向下拖动时,视频视图变小。但是这样做时 onClick 或 onTouch 事件不适用于播放/暂停控件。

在 youtube 上,我们可以看到两者都运行良好。所以我想知道我哪里出错了。

运动场景:

<MotionScene xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:motion="http://schemas.android.com/apk/res-auto">

    <Transition
        motion:constraintSetEnd="@id/collapsed"
        motion:constraintSetStart="@id/expanded"
        motion:duration="100">

        <OnSwipe
            motion:dragDirection="dragDown"
            motion:maxAcceleration="200"
            motion:touchAnchorId="@+id/player"
            motion:touchAnchorSide="bottom" />

    </Transition>

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

        <Constraint
            android:id="@id/player"
            android:layout_height="200dp"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintStart_toStartOf="parent"
            motion:layout_constraintTop_toTopOf="parent" />

        <Constraint
            android:id="@id/scrollView"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:visibility="visible"
            motion:layout_constraintBottom_toBottomOf="parent"
            motion:layout_constraintTop_toBottomOf="@id/player" />

    </ConstraintSet>

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

        <Constraint
            android:id="@id/player"
            android:layout_height="85dp"
            motion:layout_constraintBottom_toBottomOf="parent"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintStart_toStartOf="parent" />

        <Constraint
            android:id="@id/scrollView"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:visibility="gone"
            motion:layout_constraintBottom_toBottomOf="parent"
            motion:layout_constraintTop_toBottomOf="@id/player" />

    </ConstraintSet>
</MotionScene>
Run Code Online (Sandbox Code Playgroud)

主布局:

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


    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/player"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        app:layout_constraintBottom_toTopOf="@id/scrollView"
        app:layout_constraintTop_toTopOf="parent">

        <FrameLayout
            android:id="@+id/control"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:elevation="10dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <ImageView
                android:id="@+id/stop"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_gravity="center"
                android:contentDescription="@string/app_name"
                android:src="@drawable/pause"
                android:visibility="gone" />

            <ImageView
                android:id="@+id/start"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_gravity="center"
                android:contentDescription="@string/app_name"
                android:src="@drawable/play"
                android:visibility="gone" />
        </FrameLayout>

        <ImageView
            android:id="@+id/videoView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#B3000000"
            android:contentDescription="@string/app_name" />
    </androidx.constraintlayout.widget.ConstraintLayout>

    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@id/player">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="100dp"
                android:layout_gravity="center"
                android:background="#dff"
                android:gravity="center"
                android:text="@string/app_name" />
 </LinearLayout>
    </ScrollView>
</androidx.constraintlayout.motion.widget.MotionLayout>
Run Code Online (Sandbox Code Playgroud)

代码:

            @Override
            public void onClick(View v) {
                onVideoClick();
            }
        });
Run Code Online (Sandbox Code Playgroud)

依赖:

implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta2'

Run Code Online (Sandbox Code Playgroud)

Кон*_*нко 0

我通过将所需的元素包装在 NestedScrollView 中解决了这个问题

app:layoutDescription="@transition/profile_animations_user_other"
app:showPaths="false">

<androidx.core.widget.NestedScrollView
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintTop_toBottomOf="@id/divider">


    <LinearLayout ..../>

</androidx.core.widget.NestedScrollView>
Run Code Online (Sandbox Code Playgroud)

个人资料动画用户其他

<Transition
    app:constraintSetEnd="@id/end"
    app:constraintSetStart="@id/start"
    app:duration="1000">
    <OnSwipe
        app:dragDirection="dragUp"
        app:touchAnchorId="@id/view_pager"
        app:touchAnchorSide="top" />
    <OnSwipe
        app:dragDirection="dragUp"
        app:touchAnchorId="@id/toolbar"
        app:touchAnchorSide="top" />
</Transition>
Run Code Online (Sandbox Code Playgroud)