gpt4 book ai didi

android - MotionLayout 阻止所有 View 上的 ClickListener

转载 作者:行者123 更新时间:2023-12-03 20:25:50 26 4
gpt4 key购买 nike

我正在使用带有场景 xml 的 MotionLayout:

<Transition
motion:constraintSetStart="@+id/start"
motion:constraintSetEnd="@+id/end"
>
<OnSwipe
motion:touchAnchorId="@+id/v_top_sheet"
motion:touchRegionId="@+id/v_top_sheet_touch_region"
motion:touchAnchorSide="bottom"
motion:dragDirection="dragDown" />
</Transition>

2 ConstraintSets仅引用 2 个 View ID: v_notifications_containerv_top_sheet .

在我的 Activity 中,我想将一个普通的 ClickListener 设置为此 MotionLayout 中的其他 View 之一:
iv_notification_status.setOnClickListener { Timber.d("Hello") }

此行已执行,但从未触发 ClickListener。我搜索了其他帖子,但大多数都涉及在 motion:touchAnchorId 的同一 View 上设置 ClickListener .这不是这里的情况。 ClickListener 设置为在 MotionLayout 设置中未曾提及的 View。如果我删除 app:layoutDescription属性,点击有效。

我也尝试使用 setOnTouchListener ,但它也从未被调用。

如何在 MotionLayout 中设置点击监听器?

最佳答案

this great medium article 的帮助下我发现 MotionLayout 正在拦截点击事件,即使运动场景仅包含 OnSwipe 转换。

所以我写了一个自定义的 MotionLayout 来只处理 ACTION_MOVE并将所有其他触摸事件向下传递到 View 树。奇迹般有效:

/**
* MotionLayout will intercept all touch events and take control over them.
* That means that View on top of MotionLayout (i.e. children of MotionLayout) will not
* receive touch events.
*
* If the motion scene uses only a onSwipe transition, all click events are intercepted nevertheless.
* This is why we override onInterceptTouchEvent in this class and only let swipe actions be handled
* by MotionLayout. All other actions are passed down the View tree so that possible ClickListener can
* receive the touch/click events.
*/
class ClickableMotionLayout: MotionLayout {

constructor(context: Context) : super(context)

constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)

constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)

override fun onInterceptTouchEvent(event: MotionEvent?): Boolean {
if (event?.action == MotionEvent.ACTION_MOVE) {
return super.onInterceptTouchEvent(event)
}
return false
}

}

关于android - MotionLayout 阻止所有 View 上的 ClickListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61345198/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com