Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does making parent clickable make all child element clickable as well?

Tags:

There is a LinearLayout with a lot of child elements. When a user touches any of those child elements, the same method will be invoked. In order not to implement the same onClickListener for each element, I implemented the onClickListener for the parent LinearLayout ONLY.

Now, when I click anywhere within the parent layout's borders, the desired method is being invoked just as I have implemented the listener for all child elements.

Q: Can I rely that anytime I implement onClickListener for the parent, all of its child elements will react to the click event?

Q: What would happen if any child element has its own onClickListener? Would there be a collision or clicking on that element would fire its own click event only?

like image 648
sandalone Avatar asked Nov 15 '11 10:11

sandalone


People also ask

How do you only trigger parent click event when a child is clicked?

To only trigger parent click event when a child is clicked with JavaScript, we call stopPropagation . parent. addEventListener( "click", (e) => { e. stopPropagation(); console.

How do I prevent a parent's onclick event from firing when a child anchor is clicked?

event.stopPropagation() This will stop any parent component's event from firing.


1 Answers

You answered your first question with your second question. A clickEvent will be delivered to the lowest child element in the layout hierarchy. If this element does not have an onClick behaviour it will pass the event up to its parent until the event gets handled.

Therefore you can treat the LinearLayout as one single block for your onClick behaviour. If you create another clickable element inside the layout be sure to make it big enough to reduce the chance of the user missing the correct item.

like image 175
Janusz Avatar answered Sep 30 '22 13:09

Janusz