Android 如何检测导航抽屉外的触摸事件
我已经在我的应用程序中实现了Android导航抽屉。当用户触摸导航抽屉的外侧时,我能够打开/关闭抽屉。当用户触摸/点击导航抽屉外侧时,你们中的任何人都可以帮助我检测触摸/点击事件。在这种情况下,我需要执行一些功能。 请检查附带的屏幕截图。Android 如何检测导航抽屉外的触摸事件,android,navigation-drawer,toolbar,Android,Navigation Drawer,Toolbar,我已经在我的应用程序中实现了Android导航抽屉。当用户触摸导航抽屉的外侧时,我能够打开/关闭抽屉。当用户触摸/点击导航抽屉外侧时,你们中的任何人都可以帮助我检测触摸/点击事件。在这种情况下,我需要执行一些功能。 请检查附带的屏幕截图。 欢迎提供任何帮助。您可以使用onDrawerClosed 当您触摸外部屏幕时,导航关闭后,DrawerClosed将调用 抽屉 您必须在dispatchTouchEvent()方法中处理触摸位置。查看有关触摸层次的更多信息 你可能会花一些时间来检查这个 我希
欢迎提供任何帮助。您可以使用
onDrawerClosed
当您触摸外部屏幕时,导航关闭后,DrawerClosed将调用
抽屉
您必须在
dispatchTouchEvent()
方法中处理触摸位置。查看有关触摸层次的更多信息
你可能会花一些时间来检查这个 我希望它能像帮助我一样帮助你 例如:
DrawerLayout drawerLayout = activity.findViewById(R.id.drawer_layout);
drawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {
@Override
public void onDrawerSlide(@NonNull View view, float v) {
}
@Override
public void onDrawerOpened(@NonNull View view) {
}
@Override
public void onDrawerClosed(@NonNull View view) {
}
@Override
public void onDrawerStateChanged(int i) {
}
});
祝你好运。谢谢你的回答。onDrawerClosed(视图)将在许多场景中调用。那对我不起作用,也许会有帮助
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
if (mDrawerLayout.isDrawerOpen(mRightDrawerListView)) {
View content = findViewById(R.id.right_drawer);
int[] contentLocation = new int[2];
content.getLocationOnScreen(contentLocation);
Rect rect = new Rect(contentLocation[0],
contentLocation[1],
contentLocation[0] + content.getWidth(),
contentLocation[1] + content.getHeight());
View toolbarView = findViewById(R.id.toolbar);
int[] toolbarLocation = new int[2];
toolbarView.getLocationOnScreen(toolbarLocation);
Rect toolbarViewRect = new Rect(toolbarLocation[0],
toolbarLocation[1],
toolbarLocation[0] + toolbarView.getWidth(),
toolbarLocation[1] + toolbarView.getHeight());
if (!(rect.contains((int) event.getX(), (int) event.getY())) && !toolbarViewRect.contains((int) event.getX(), (int) event.getY())) {
isOutSideClicked = true;
} else {
isOutSideClicked = false;
}
} else {
return super.dispatchTouchEvent(event);
}
} else if (event.getAction() == MotionEvent.ACTION_DOWN && isOutSideClicked) {
isOutSideClicked = false;
return super.dispatchTouchEvent(event);
} else if (event.getAction() == MotionEvent.ACTION_MOVE && isOutSideClicked) {
return super.dispatchTouchEvent(event);
}
if (isOutSideClicked) {
//make http call/db request
Toast.makeText(this, "Hello..", Toast.LENGTH_SHORT).show();
}
return super.dispatchTouchEvent(event);
}
DrawerLayout drawerLayout = activity.findViewById(R.id.drawer_layout);
drawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {
@Override
public void onDrawerSlide(@NonNull View view, float v) {
}
@Override
public void onDrawerOpened(@NonNull View view) {
}
@Override
public void onDrawerClosed(@NonNull View view) {
}
@Override
public void onDrawerStateChanged(int i) {
}
});