Skip to content

Instantly share code, notes, and snippets.

@wafer-li
Last active June 10, 2020 08:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wafer-li/7f1050bbeb3322916e3c3de5b0870d15 to your computer and use it in GitHub Desktop.
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
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;
}
}
@wafer-li
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment