MotionLayout:MotionScene OnClick会覆盖setOnClickListener [英] MotionLayout: MotionScene OnClick overrides setOnClickListener

查看:888
本文介绍了MotionLayout:MotionScene OnClick会覆盖setOnClickListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始玩 MotionLayout 。我使用 MotionLayout 定义了一个活动布局,该布局使用 MotionScene 隐藏和显示视图。

I'm just starting to play with MotionLayout. I have defined an activity layout using MotionLayout that uses a MotionScene to hide and show a view.

MotionScene 过渡看起来像这样:

<Transition
    app:constraintSetStart="@id/collapsed"
    app:constraintSetEnd="@id/expanded">

    <OnClick app:target="@id/nextButton"  />

</Transition>

麻烦的是,当我以编程方式向按钮添加ClickListener时,什么也没发生:

Troublie is, nothing happens when I programmatically add a ClickListener to the button:

nextButton.setOnClickListener {
        //do some stuff
    }

此侦听器将被完全忽略,但每次单击都会触发转换(视图扩展/折叠)。我见过有人扩展 MotionLayout 来处理单击事件,但是似乎可以为该按钮添加另一个单击侦听器的简便方法。

This listener is completely ignored but the transition (view expanding/collapsing) is triggered with every click. I have seen where someone extends MotionLayout to handle click events but it seems like there might be an easier way to add another click listener for the button.

问题1:是否可以在MotionLayout过渡中将ClickListener添加到OnClick的目标?

问题2:有没有一种方法可以使转换成为一次性事件?
期望的结果是,如果在单击按钮时折叠了视图,则视图会扩展,但是如果已经

Question 2: Is there a way to make the transition a one time only event? The desired result is that if the view is collapsed when the button is clicked, then the view expands, but if it's already expanded then it stays expanded.

最后,我正在使用命名空间 http://schemas.android.com/apk/res-自动 和文档清楚说明 target mode 是OnClick的属性。但是,当我使用 mode 时,该项目将无法编译,因为在该命名空间中找不到该项目。

Lastly, I'm using the namespace "http://schemas.android.com/apk/res-auto" and the docs clearly state that target and mode are attributes for OnClick. But the project won't compile when I use mode because it can't be found in that namespace.

问题3:我使用的是正确的名称空间吗?

推荐答案


  1. 我找不到。

  2. 我使用带有 transitionToEnd值的clickAction属性成功。这样一来,动作布局就不能返回到startConstraintSet。



<OnClick
      motion:targetId="@+id/rateUsButton"
      motion:clickAction="transitionToEnd"/>




  1. 这是我正在使用的名称空间以及我所看到的示例中使用的命名空间。

今天刚遇到同一问题。通过在代码中使用 setOnTouchListener 而不是 setOnClickListener ,我能够拦截点击。

Just ran into the same issue today. I was able to intercept the click by using setOnTouchListener instead of setOnClickListener in my code.

rateUsButton.setOnTouchListener { _, event ->
    if (event.action == MotionEvent.ACTION_UP) {
        // handle the click
    }
    false
}

我知道这个解决方案不是最好的,但是我没有找到其他选择。返回false表示此处未处理触摸,因此将由运动布局处理。

I know this solution isn't the best but I didn't find another option. Returning false means the touch was not handled here and thus will be handled by the motion layout.

这篇关于MotionLayout:MotionScene OnClick会覆盖setOnClickListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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