Last active
June 10, 2020 08:14
-
-
Save wafer-li/7f1050bbeb3322916e3c3de5b0870d15 to your computer and use it in GitHub Desktop.
TouchDelegateComposite to handle multiple view delegate their touch event to one specific view
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.graphics.Rect; | |
import android.view.MotionEvent; | |
import android.view.TouchDelegate; | |
import android.view.View; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class TouchDelegateComposite extends TouchDelegate { | |
private final List<TouchDelegate> delegates = new ArrayList<>(); | |
private static final Rect emptyRect = new Rect(); | |
public TouchDelegateComposite(View view) { | |
super(emptyRect, view); | |
} | |
public void addDelegate(TouchDelegate delegate) { | |
if (delegate != null) { | |
delegates.add(delegate); | |
} | |
} | |
@Override | |
public boolean onTouchEvent(MotionEvent event) { | |
boolean res = false; | |
float x = event.getX(); | |
float y = event.getY(); | |
for (TouchDelegate delegate : delegates) { | |
event.setLocation(x, y); | |
res = delegate.onTouchEvent(event) || res; | |
} | |
return res; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Copyright to
https://stackoverflow.com/a/20051314/5730641