简体   繁体   English

如何将单击事件从父(ViewGroup)委托给子(View)android?

[英]How to delegate click event from parent (ViewGroup) to child (View) android?

You can, of course, assign onclick to all child views through a loop, but is there such an opportunity to hang the click event on the parent element, and then receive the event on the desired child view, as, for example, in html/js through the event target?当然,您可以通过循环将 onclick 分配给所有子视图,但是是否有机会将 click 事件挂在父元素上,然后在所需的子视图上接收事件,例如在 html /js 通过事件目标?

You can setup an onClickListener() for the parent and then inside that you can delegate the click using child.performClick() .您可以为父级设置 onClickListener( onClickListener() ,然后在其中可以使用child.performClick()委派点击。 if you cant to know more, you can reference https://developer.android.com/reference/android/view/View.html#performClick()如果您无法了解更多信息,可以参考https://developer.android.com/reference/android/view/View.html#performClick()

The code would look something like this:代码看起来像这样:

parent.setOnClickListener {
  child.performClick()
}

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

相关问题 Android,在 ViewGroup 的子级上捕获单点事件,但在父级上捕获双点 - Android, catch singletap event on a child of ViewGroup but doubletap on the parent 父级和子级视图的单击事件 - On Click event for Parent and Child view Android:父视图组ontouch被忽略,有利于子视图onclick - Android: parent Viewgroup ontouch being ignored in favour of child view onclick 如果事件来自Android中的同一视图组,如何将事件从一个孩子转移到另一个孩子? - How can I transfer event from one child to another child if they are from same viewgroup in android? 如何在调整其父 ViewGroup 后调整子视图高度? - How to adjust child view height after resizing its parent ViewGroup? 如何从视图组onclicklistener上获取具有ID的子视图? - How to get child view with id from a viewgroup onclicklistener? Android:从视图中获取Viewgroup? - Android: get Viewgroup from the view? 从父自定义viewGroup设置子参数 - set child params from parent custom viewGroup Android viewGroup侦听子视图的可见性更改 - Android viewGroup listen to child view visibility changes 在 Android XML 布局中,我可以使用子视图或视图组为父视图的一部分背景着色吗? - In an Android XML layout, can I use a child view or viewgroup to tint part of the parent's background?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM