开发者

Android route / bubble events because a control is consuming the onClick event

开发者 https://www.devze.com 2023-02-05 02:37 出处:网络
I have a custom view with an ImageView and a TextView on it and implemented the onClickListener for my custom view.

I have a custom view with an ImageView and a TextView on it and implemented the onClickListener for my custom view. The problem is, that the ImageView is consuming the onClick-event (I just want the user be able to click on my control, no matter where). I could listen to the onClick of the Image/TextView too, but it seems开发者_JAVA百科 dirty to me.

Is there a way to bubble / route Events in Android? Or possible another good solution?


View.onClick() event does not bubble. Two possible solutions:

  1. Register OnCLickListener on you child views and then pass on the event by calling performClick() on parent.

  2. Use OnTouchListener which bubbles up: just return false in child view's onTouch() method. This is more work as you have to account for touch-down & lift-up in order to emulate click.


Have you set the onClickListener in your custom view?

Set your custom view as clickable.

I don't recommend on setting any click listener in the child views.

Does it work now?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号