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

问题描述 投票:5回答:1

我有一个类似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>

main_layout:

<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>

java代码:

            @Override
            public void onClick(View v) {
                onVideoClick();
            }
        });

依赖关系:

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

android android-layout android-constraintlayout android-motionlayout
1个回答
0
投票

我偶然发现了类似的问题。我最终将OnTouchListener设置为Motionlayout

motionLayout.setOnTouchListener { _, event ->
    if (event.action != MotionEvent.ACTION_UP) return@setOnTouchListener false

    val isTouchingPlayButton = Rect().also { playButton.getHitRect(it) }
        .contains(event.x.toInt(), event.y.toInt())

    if (isTouchingPlayButton && motionLayout.progress == 0f) {
        player.play()
        return@setOnTouchListener true
    }

    false
}
© www.soinside.com 2019 - 2024. All rights reserved.