Android route / bubble events because control consumes onClick event

I have a custom view with ImageView and TextView on it and implemented onClickListener for my custom view. The problem is that ImageView consumes an onClick event (I just want the user to be able to click on my control, no matter where). I could also listen to onClick from Image / TextView, but it seems dirty to me.

Is there any way to bubble / route events in Android? Or maybe another good solution?

+4
source share
2 answers

Have you onClickListener in your custom view?

Set your custom view as interactive.

I do not recommend installing any click listener in child views.

Does it work now?

+3
source

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

  • Register OnCLickListener in your child views, and then pass the event by calling performClick() on the parent.

  • Use the OnTouchListener that bubbles up: just return false to the child onTouch() view method. This works more because you have to consider touch and elevation to imitate a click.

+9
source

Source: https://habr.com/ru/post/1335985/


All Articles