如何为工具栏弹出(溢出)菜单设置 OnTouchListener

How to set OnTouchListener for toolbar popup (overflow) menu

如果是图片按钮(app:showAsAction="always"),我可以这样做:

findViewById(R.id.action_settings).setOnTouchListener(listener)

但是如果它在溢出菜单中 (app:showAsAction="never"),findViewById 将 return null。最简单和最便携的解决方案是什么? (而不是大量的反思)

P.S。我想获得在 OnTouchListener 中传递给 MotionEvent 的触摸位置,所以我绝对不想知道如何使用 onOptionItemsSelectedOnClickListener

在我的演示应用程序中,我能够使用以下方法触发触摸事件:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    boolean result = false;
    int id = item.getItemId();
    if (id == R.id.action_settings) {

        //Returning false here allows the touch event to bubble up.
        result = false;
    } else {
        result = super.onOptionsItemSelected(item);
    }
    return result;
}


@Override
public boolean dispatchGenericMotionEvent(MotionEvent ev) {
    Log.d(TAG, "dispatchGenericMotionEvent");
    // do some other stuff
    return super.dispatchGenericMotionEvent(ev);
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    Log.d(TAG, "dispatchTouchEvent");
    // do stuff here....
    return super.dispatchTouchEvent(ev);
}