简体   繁体   English

我们可以同时使用OnSwipe和OnClick吗 <Transition> 适用于Android MotionLayout?

[英]Can we use OnSwipe and OnClick in the same <Transition> for Android MotionLayout?

I want to play animation when user clicks or swipes . 我想在用户单击滑动时播放动画。 But can we handle both behaviours in MotionLayout? 但是我们可以处理MotionLayout中的两种行为吗? They work separately perfectly, but if I add OnClick and OnSwipe in the same scene, only OnClick works. 它们可以完美地分开工作,但是如果我在同一场景中添加OnClickOnSwipe ,则只有OnClick可以工作。 Is there any workarounds? 有什么解决方法吗?

Yes, you can work around this by removing the onClick behavior from your MotionScene and implement the click yourself by extending the MotionLayout and overriding dispatchTouchEvent . 是的,您可以通过从MotionScene中删除onClick行为来解决此问题,并通过扩展MotionLayout并覆盖dispatchTouchEvent自己实现单击。

In this function, you decide if the touch event is a click inside the 'target area' where you want the onClick behavior to occur. 在此功能中,您可以确定触摸事件是否是您希望onClick行为发生在“目标区域”内的单击。

override fun dispatchTouchEvent(ev: MotionEvent): Boolean {
        if (touchEventInsideTargetView(clickableArea, ev)) {
            when (ev.action) {
                MotionEvent.ACTION_DOWN -> {
                    startX = ev.x
                    startY = ev.y
                }
                MotionEvent.ACTION_UP   -> {
                    val endX = ev.x
                    val endY = ev.y
                    if (isAClick(startX!!, endX, startY!!, endY)) {
                        if (doClickTransition()) {
                            return true
                        }
                    }
                }
            }
        }
 return super.dispatchTouchEvent(ev)
}

A post on this and a full code example can be found here: https://medium.com/vrt-digital-studio/picture-in-picture-video-overlay-with-motionlayout-a9404663b9e7 可以在此处找到有关此帖子和完整代码示例的信息: https : //medium.com/vrt-digital-studio/picture-in-picture-video-overlay-with-motionlayout-a9404663b9e7

Sure, just handle the onTouch event in your parent class. 当然,只需在您的父类中处理onTouch事件。 Then decide what you are doing with it and what to send it to. 然后确定您正在使用它以及将其发送给什么。

onTouch(touchEvent){
    motionLayout.doClickIfYouWant(touchEvent)
    motionLayout.doSwipeIfYouWant(touchEvent)
}

Pseudo code, but you can send it to both if you catch it first and decide who gets it. 伪代码,但是如果您先捕获它并确定谁得到它,则可以将其发送给两个人。 Also don't forget to return handled boolean flag from onTouch callback to ensure it doesn't get handled twice. 同样不要忘记从onTouch回调返回已处理的布尔标志,以确保不会被两次处理。 Lastly, if other items are touchable on the screen you may have to check the x,y of your touch to determine if it should go to the motionLayout or just return "not handled" and let the native behavior pass the touch on through. 最后,如果其他项目在屏幕上是可触摸的,则可能必须检查触摸的x,y以确定它是否应转到motionLayout或仅返回“ not handle”,并让本机行为将触摸传递通过。

Hope that helps. 希望能有所帮助。 Also if the motionLayout class doesn't expose the methods you need to touch to send the correct touch event to more then one place, you can still get it using reflection. 同样,如果motionLayout类没有公开将正确的触摸事件发送到一个以上的位置所需的触摸方法,则仍然可以使用反射来获取它。 Of course reflection can be a little slower, so use with caution, but I've had to do it before for a layout class that had children images that I needed to move around, and controlled the touch at a parent level to decide if the image gets it or not, but it was not publicly available, so I looked at the code and touched it via reflection with no issues. 当然,反射可能会稍慢一些,因此请谨慎使用,但对于布局类,我必须这样做,该类具有需要移动的子图像,并在父级上控制触摸以确定是否需要image是否获得它,但是它不是公开可用的,所以我查看了代码并通过反射触摸了它,没有任何问题。 Treat that as a last resort though, and maybe everything you need is exposed. 不过,把它当作最后的手段,也许您需要的所有东西都暴露了。

Also be aware some engineer despise reflection lol, so code with caution. 还应注意一些工程师鄙视反射大声笑,因此请谨慎编写代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何设置多个OnSwipe Add Android MotionLayout Single Transition? - How to set multiple OnSwipe Add Android MotionLayout Single Transition? MotionLayout OnSwipe 转换被错误的锚点触发 - MotionLayout OnSwipe transition gets triggered by wrong anchor Android Kotlin onClickListener 似乎隐藏了 MotionLayout 的 OnSwipe - Android Kotlin onClickListener seems to hide OnSwipe of MotionLayout 当触摸区域包含 RecyclerView 时,Android MotionLayout OnSwipe 不起作用 - Android MotionLayout OnSwipe not working when touch region contains a RecyclerView MotionLayout onSwipe 自动完成速度 - MotionLayout onSwipe autocomplete speed 未调用 Android MotionLayout 转换监听器 - Android MotionLayout Transition Listener not called MotionLayout - OnSwipe 不适用于可点击的孩子 - MotionLayout - OnSwipe not working on clickable children 在 RecyclerView 的项目上使用 MotionLayout OnSwipe - Using MotionLayout OnSwipe on Item of a RecyclerView 是否可以将 MotionLayout 或 Transition 用于共享元素? - Is it possible to Use MotionLayout or Transition for Shared Element? 如何在同一视图上执行onClick动作和onswipe操作 - How to perform onClick action and onswipe operation on same view
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM