How can I transfer event from one child to another

2019-09-08 08:43发布

问题:

So far I was able to transfer the event from the parent (viewgroup) to the child. ie When I click child 1, the viewgroup sends the event to child 1 and when I click child 2 (which is under child 1), the viewgroup sends the event to child 2.

Problem: What I wanted to do was, When app is in certain state and child 2 is clicked I want the viewgroup to send the event to child 1 instead of child 2.

So far : This link kind of touches the issue but is not in android.

回答1:

Something like this : In main viewgroup

child2.setOnClickListener(new....{

and inside this event handler call an appropriate method in child1 

child1.onSomethingInChild2()


回答2:

What i did was inside dispatchTouchEvent of child 2 I test for the state. If the the state is true I called child1.dispatchTouchEvent(event) and passed the event to child 1.

In child 2

public boolean dispatchTouchEvent(MotionEvent event) {
   if(cond == true){
       child1.dispatchTouchEvent(event);
       return false;
   }

}