You can create a third invisible View and place it on top of the overlapping part. Then in its TouchEvent perform the actions of those two TouchEvents
Android make two overlapping views receive and handle the same touch event
-
14-06-2023 - |
Frage
I'm using a RelativeLayout
to overlap two different views. Both Views fill the entire Screen.
If the user touches the screen, I want both views to receive the TouchEvent
and to excecute their own onTouch
-Methods. At the moment, only one view receives the TouchEvent
How can I make both views receive the TouchEvent?
Thank you in advance for your help.
EDIT: Here's the code
touchView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
view1.onTouchEvent(event);
view2.onTouchEvent(event);
return false;
}
});
Lösung
Andere Tipps
For top view assign clickable=false
it will allow the touch to intercept through it
You could try to get to check if the rawX & rawY of the TouchEvent
are within the hitRect of the view that didn't get the event. If it did, you can use dispatchTouchEvent(MotionEvent)
to send the TouchEvent
tot the view that didn't get the event
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow