我们可以在同一< Transition>中使用OnSwipe和OnClick吗?适用于Android MotionLayout? [英] Can we use OnSwipe and OnClick in the same <Transition> for Android MotionLayout?

查看:149
本文介绍了我们可以在同一< Transition>中使用OnSwipe和OnClick吗?适用于Android MotionLayout?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在用户点击滑动时播放动画.但是我们可以处理MotionLayout中的两种行为吗?它们可以完美地分开工作,但是如果我在同一场景中添加 OnClick OnSwipe ,则只有 OnClick 可以工作.有什么解决方法吗?

解决方案

当然,只需处理父类中的onTouch事件即可. 然后确定您要使用它的内容以及将其发送给什么.

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

伪代码,但是如果您先抓到并确定是谁得到的,则可以将其发送给双方.同样不要忘记从onTouch回调中返回已处理的布尔标志,以确保不会被两次处理.最后,如果其他项目在屏幕上是可触摸的,则可能必须检查触摸的x,y以确定它是否应转到motionLayout或仅返回"not handle",并让本机行为将触摸传递通过./p>

希望有帮助.同样,如果motionLayout类没有公开将正确的触摸事件发送到一个以上的位置所需的触摸方法,则仍然可以使用反射来获取它.当然,反射可能会稍慢一些,因此请谨慎使用,但是对于布局类,我必须这样做,因为该类具有我需要四处移动的子图像,并控制父级的触摸来确定是否需要image是否获得它,但是它不是公开可用的,因此我查看了代码并通过反射触摸了它,没有任何问题.不过,把它当作最后的手段,也许您需要的所有东西都暴露了.

还请注意,有些工程师不喜欢反射大声笑,因此请谨慎编写代码.

I want to play animation when user clicks or swipes. But can we handle both behaviours in MotionLayout? They work separately perfectly, but if I add OnClick and OnSwipe in the same scene, only OnClick works. Is there any workarounds?

解决方案

Sure, just handle the onTouch event in your parent class. 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. 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.

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

这篇关于我们可以在同一< Transition>中使用OnSwipe和OnClick吗?适用于Android MotionLayout?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆