Maragues 从 onLongClick() 发送 dispatchTouchEvent() 的想法似乎很有希望,但是您必须构建一个事件对象以发送到 dispatchTouchEvent() 以模拟 OnLongClickListener 使用的事件。
我翻转它以拦截父视图中的触摸事件,然后将其转发到子视图。我对父视图进行了子类化,然后添加了这个方法:
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
// this allows us to catch touch events on the list view if a child button is in the same location, then pass them on to child buttons so they can use them, too
// we don't have to worry about move events because currently none of the child buttons use them
int touchX = Math.round(event.getX());
int touchY = Math.round(event.getY());
Rect touchRect = new Rect(touchX, touchY, touchX, touchY);
Log.d("onInterceptTouchEvent", "got touch at " + touchRect);
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
for (View cell : ViewUtils.getSubviews(this)) {
int cellX = Math.round(cell.getX());
int cellY = Math.round(cell.getY());
Rect cellRect = new Rect(cellX, cellY, cellX + cell.getWidth(), cellY + cell.getHeight());
if (Rect.intersects(touchRect, cellRect)) {
for (View button : ViewUtils.getSubviews((ViewGroup) cell)) {
if (button instanceof ImageButton) {
int buttonX = Math.round(button.getX()) + cellX;
int buttonY = Math.round(button.getY()) + cellY;
Rect buttonRect = new Rect(buttonX, buttonY, buttonX + button.getWidth(), buttonY + button.getHeight());
Log.d("onInterceptTouchEvent", "found button at " + buttonRect);
if (Rect.intersects(touchRect, buttonRect)) {
Log.d("onInterceptTouchEvent", "forward touch to button");
button.dispatchTouchEvent(event);
break;
}
}
}
break;
}
}
break;
}
return true;
}
在我的例子中,父视图是 ListView,子视图是表格单元格内的 ImageButtons。因此,此代码遍历表格单元格,然后遍历每个单元格中的按钮,以找到与触摸位置匹配的按钮,并将触摸转发到该按钮。我的按钮都是使用 OnClickListener 或 OnLongClickListener 的 ImageButton,所以我不转发 ACTION_MOVE 事件,但如果需要,您可以转发。
这是上面使用的 getSubViews() 方法:
public static ArrayList<View> getSubviews(ViewGroup viewGroup) {
ArrayList<View> subviews = new ArrayList<View>();
for (int i=0; i<viewGroup.getChildCount(); i++) {
subviews.add(viewGroup.getChildAt(i));
}
return subviews;
}
更新:更简单的版本
上面的代码应该适用于在父视图上接收触摸和在子视图上长按的特定情况。但是我发现这不支持子视图中的常规点击。我认为这与 onInterceptTouchEvent() 处理 ACTION_DOWN 事件的方式不同于处理其他事件的方式有关。此方法的文档非常混乱。
但是,这里有一个更简单的方法,它应该支持父视图或子视图中的各种触摸和单击事件。如上所述,这需要对父视图进行子类化。它还需要直接在该类中设置 onTouch 方法,而不是在另一个类中使用 setOnTouchListener():
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
this.onTouch(this, event); // send the touch to the onTouch method below
return false; // then let the touch proceed to child buttons
// return true = this method and this view's onTouch receives events; return false = this method and children's onTouch receive events; remove this method = only children's onTouch receive events
}
@Override
public boolean onTouch(View v, MotionEvent event) {
// work with this touch event here
return false; // then let the touch continue to other applicable views
// return true = only this view receives events; return false = this view and other applicable views receive events
}