首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >运动布局OnSwipe禁用对YoutubePlayer (YoutubePlayer API)的单击

运动布局OnSwipe禁用对YoutubePlayer (YoutubePlayer API)的单击
EN

Stack Overflow用户
提问于 2020-07-26 02:13:56
回答 1查看 786关注 0票数 5

我有一个YoutubePlayer播放一个视频在我的运动布局。我想实现这个youtube-like motion https://developer.android.com/training/constraint-layout/motionlayout/examples#youtube-like_motion

这里的问题是,在上面提供的示例中,它们使用ImageView而不是YoutubePlayer接口来完成动画。但不幸的是,youtube播放器界面似乎“覆盖”点击监听器,只允许点击,而不是滑动。(这是我的推论)。

我想知道它是如何在youtube应用程序中实现的。如果有人能回答我,请回答。我在我的应用程序中需要这个。

我提出了一个问题,请看一下。https://issuetracker.google.com/issues/162155197

这就是我尝试过的:

  1. touchRegionId设置为youtube播放器前面或后面的视图时,对界面的单击完全被禁用。但是动画作品。我看不到这个选项的解决方案,因为我认为这种行为是故意的。

  1. limitBoundsTo设置为视图时,它就完成了它的工作。它将OnSwipe操作区域限制在这样的视图边界上。这正是我需要的工作方式,直到YOUTUBE播放器界面初始化为止。在初始化之后,OnSwipe就不再被检测到,接口只监听单击,例如,这使您能够暂停视频。如果一定要查看的限制大于youtube界面,我可以对视图的剩余部分进行滑动。但youtube的界面不听刷卡。也许这个接口不支持

  1. 我尝试过没有设置上述任何一个。只有普通的OnSwipe与拖动方向。在初始化youtube之前,该刷在任何地方都能工作。初始化youtube播放器界面所使用的像素时,停止监听滑动,它们只侦听单击。

考虑到2和3,我认为这是接口本身的问题。不管你有什么建议,请告诉我。谢谢。

这是我的运动布局:

代码语言:javascript
复制
<androidx.constraintlayout.motion.widget.MotionLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/blackBackground"
    android:nestedScrollingEnabled="false"
    app:layoutDescription="@xml/activity_main_scene"
    app:motionDebug="SHOW_ALL">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/player_background"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:touchscreenBlocksFocus="false"
        android:clickable="false"
        android:background="#000000"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <FrameLayout
        android:id="@+id/main_player"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="@id/player_background"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="@id/player_background" />

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

这是我的activity_main_scene xml

代码语言:javascript
复制
<?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">

    <ConstraintSet android:id="@+id/openYoutubePlayer">
        <Constraint
            android:id="@id/player_background"
            android:layout_width="match_parent"
            android:layout_height="240dp"
            android:elevation="0dp">
            <CustomAttribute
                app:attributeName="backgroundColor"
                app:customColorValue="#000000" />
        </Constraint>
    </ConstraintSet>

    <ConstraintSet android:id="@+id/closeYoutubePlayer">
        <Constraint
            android:id="@id/player_background"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:elevation="4dp">
            <CustomAttribute
                app:attributeName="backgroundColor"
                app:customColorValue="@color/blackBackground" />
        </Constraint>
    </ConstraintSet>

    <ConstraintSet android:id="@+id/bottomYoutubePlayer">
        <Constraint
            android:id="@id/main_player"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            app:layout_constraintBottom_toBottomOf="@id/player_background"
            app:layout_constraintLeft_toLeftOf="@id/player_background"
            app:layout_constraintTop_toTopOf="@id/player_background" />
        <Constraint
            android:id="@id/player_background"
            android:layout_width="0dp"
            android:layout_height="54dp"
            app:layout_constraintBottom_toTopOf="@id/nav_view"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent" />
    </ConstraintSet>

    <Transition
        app:constraintSetEnd="@id/openYoutubePlayer"
        app:constraintSetStart="@id/closeYoutubePlayer"
        app:duration="500" />

    <Transition
        android:id="@+id/youtubePlayerIsOpened"
        app:constraintSetEnd="@id/bottomYoutubePlayer"
        app:motionInterpolator="linear"
        app:constraintSetStart="@id/openYoutubePlayer"
        app:duration="500">
        <OnSwipe
            app:dragDirection="dragUp"
            app:touchRegionId="@id/player_background"
            app:touchAnchorId="@id/player_background"/>
    </Transition>

</MotionScene>

编辑: YOUTUBE初始化添加了

代码语言:javascript
复制
val youtubePlayerSupportFragment = YouTubePlayerSupportFragmentX.newInstance()
            supportFragmentManager.beginTransaction()
                .add(R.id.main_player, youtubePlayerSupportFragment).commit()
            youtubePlayerSupportFragment.initialize(
                resources.getString(R.string.API_KEY),
                object : YouTubePlayer.OnInitializedListener {
                    override fun onInitializationSuccess(
                        p0: YouTubePlayer.Provider?,
                        p1: YouTubePlayer?,
                        p2: Boolean
                    ) {
                        p1?.loadVideo(incomingLink)
                        currentLink = incomingLink
                        youtubePlayer = p1
                    }

                    override fun onInitializationFailure(
                        p0: YouTubePlayer.Provider?,
                        p1: YouTubeInitializationResult?
                    ) {
                        layoutUtils.createToast(
                            applicationContext,
                            "Error al iniciar Youtube"
                        )
                    }
                })
EN

回答 1

Stack Overflow用户

发布于 2021-03-04 23:49:17

这是一个复杂的运动布局场景的问题。要在相同的转换中使用onClick和onSwipe,您需要:

  1. 处理来自运动场景转换的onSwipe

  1. 处理来自运动布局

子类的onClick

  1. 在MotionLayout (自定义视图)的子类中,重写以下两个方法:

代码语言:javascript
复制
class MyMotionLayout @JvmOverloads constructor(context: Context):MotionLayout(context) {

       override fun onInterceptTouchEvent(ev: MotionEvent): Boolean {
           //Check if we have MotionEvent.ACTION_UP while MotionEvent.ACTION_MOVE<0.5
           // i.e ..this was a click
           if(ev==MotionEvent.ACTION_UP){
               return true
           }
           return false
       }
       override fun onTouchEvent(event: MotionEvent): Boolean {
           // Here we actually handle the touch event 
           // This method will only be called if the touch event was intercepted in
           // onInterceptTouchEvent
           callOnClick() 
      
       }
   }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63095440

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档