我的任务是接收具有良好嵌入(库变体)的 android 应用程序中的所有运动事件。我创建了我的 Window.Callback 包装器。
public class WindowCallback implements Window.Callback {
private final Window.Callback wrapper;
public WindowCallback(Window.Callback callback) {
this.wrapper = callback;
}
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
return wrapper.dispatchKeyEvent(event);
}
@Override
public boolean dispatchKeyShortcutEvent(KeyEvent event) {
return wrapper.dispatchKeyShortcutEvent(event);
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
return wrapper.dispatchTouchEvent(event);
}
// other methods omitted
}
然后,实例化它Activity
:
final Window window = getWindow();
final Window.Callback windowCallback = window.getCallback();
final WindowCallback callbackWrapper = new WindowCallback(windowCallback);
window.setCallback(interceptCallback);
但是当我有Toolbar
in时Activity
,这个Toolbar
捕获Window.Callback
in 方法setActionBar(Toolbar)
还是setSupportActionBar(Toolbar)
. 来自框架的代码片段Activity
:
if (toolbar != null) {
final ToolbarActionBar tbab = new ToolbarActionBar(toolbar, getTitle(), this);
mActionBar = tbab;
mWindow.setCallback(tbab.getWrappedWindowCallback());
并ToolbarActionBar
来自支持库:
ToolbarActionBar(Toolbar toolbar, CharSequence title, Callback windowCallback) {
this.mDecorToolbar = new ToolbarWidgetWrapper(toolbar, false);
this.mWindowCallback = new ToolbarActionBar.ToolbarCallbackWrapper(windowCallback);
this.mDecorToolbar.setWindowCallback(this.mWindowCallback);
toolbar.setOnMenuItemClickListener(this.mMenuClicker);
this.mDecorToolbar.setWindowTitle(title);
}
问题是 - 如何检查何时Toolbar
catch Window.Callback
,但不创建基本活动并从中扩展。此检查不应该非常频繁地执行。(OnGlobalLayoutListener
不是我们的情况)